1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//! The [`Action`] type
use std::io::Write;
use crate::{
AsyncInputReader, Image, ImageId, InputReader, NumberOrId, PlacementId, Verbosity,
delete::DeleteConfig, display::DisplayConfig, error::TransmitError, read_parse_response,
read_parse_response_async
};
/// The different actions one can take to interact with a terminal which supports the kitty image
/// protocol. This is the main interaction point with the terminal - one should construct an
/// [`Action`] and [`Action::execute`] it
#[derive(Debug, PartialEq)]
pub enum Action<'image, 'data> {
/// This simply sends the image data to the terminal, but does not display it. It also
/// transfers ownership of the image data to the terminal - for example, if a temp file is used
/// to send the data, that file will be deleted after it is transmitted to the terminal. Once a
/// `Transmit` is successfully sent, one can then display the sent image with
/// [`Action::Display`]
Transmit(Image<'data>),
/// Display an image which was already transmitted to (and is now owned by) the terminal
Display {
/// The image ID of the image which you want to display
image_id: ImageId,
/// A 'placement ID' for this display - see the documentation for [`PlacementId`] for more
/// info. This must be sent when displaying something that was already transmitted, but is
/// optional when transmitting and displaying in-one (e.g. with
/// [`Self::TransmitAndDisplay`]
placement_id: PlacementId,
/// The details about exactly how this image should be displayed - the location, cursor
/// movement, etc
config: DisplayConfig
},
/// Transmit and then display an image. Should act effectively the same as calling
/// [`Action::Transmit`] and then [`Action::Display`] with the returned image id.
TransmitAndDisplay {
/// The image which will be transferred to the terminal and then displayed immediately
/// after
image: Image<'data>,
/// The details about exactly how this image should be displayed - the location, cursor
/// movement, etc
config: DisplayConfig,
/// The placement ID for this display, if you'd like to use one. It is not necessary when
/// transmitting and displaying in-one
placement_id: Option<PlacementId>
},
/// Query the terminal to determine if a specific image can be transmitted & displayed. The
/// following is quoted from the spec:
///
/// Since a client has no a-priori knowledge of whether it shares a filesystem/shared memory with the terminal emulator, it can send an id with the control data, using the i key (which can be an arbitrary positive integer up to 4294967295, it must not be zero). If it does so, the terminal emulator will reply after trying to load the image, saying whether loading was successful or not. For example:
///
/// ```text
/// <ESC>_Gi=31,s=10,v=2,t=s;<encoded /some-shared-memory-name><ESC>\
/// ```
///
/// to which the terminal emulator will reply (after trying to load the data):
///
/// ```text
/// <ESC>_Gi=31;error message or OK<ESC>\
/// ```
///
/// Here the i value will be the same as was sent by the client in the original request. The message data will be a ASCII encoded string containing only printable characters and spaces. The string will be OK if reading the pixel data succeeded or an error message.
///
/// Sometimes, using an id is not appropriate, for example, if you do not want to replace a previously sent image with the same id, or if you are sending a dummy image and do not want it stored by the terminal emulator. In that case, you can use the query action, set a=q. Then the terminal emulator will try to load the image and respond with either OK or an error, as above, but it will not replace an existing image with the same id, nor will it store the image.
///
/// We intend that any terminal emulator that wishes to support it can do so. To check if a terminal emulator supports the graphics protocol the best way is to send the above query action followed by a request for the [primary device attributes](https://vt100.net/docs/vt510-rm/DA1.html). If you get back an answer for the device attributes without getting back an answer for the query action the terminal emulator does not support the graphics protocol.
///
/// This means that terminal emulators that support the graphics protocol, must reply to query actions immediately without processing other input. Most terminal emulators handle input in a FIFO manner, anyway.
///
/// So for example, you could send:
///
/// ```text
/// <ESC>_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA<ESC>\<ESC>[c
/// ```
///
/// If you get back a response to the graphics query, the terminal emulator supports the protocol, if you get back a response to the device attributes query without a response to the graphics query, it does not.
Query(&'image Image<'data>),
/// Delete a specific set of images. See the [`DeleteConfig`] documentation for more details
/// about how to use
Delete(DeleteConfig) // we'll do these at some point
// TransmitAnimationFrames,
// ControlAnimation,
// ComposeAnimationFrames
}
impl Action<'_, '_> {
/// Write the transmit code for this [`Action`] to `writer` - this is the first part of
/// [`Self::execute`] and only does a part of what is necessary to fully interact with a
/// terminal. The full details can be found at [`Self::execute`].
///
/// # Errors
///
/// This will error if writing to `writer` ever returns an error.
pub fn write_transmit_to<W: Write>(
&self,
writer: W,
verbosity: Verbosity
) -> Result<W, std::io::Error> {
fn inner_for_stdio<W: Write>(
img: &Action<'_, '_>,
mut writer: W,
verbosity: Verbosity
) -> Result<W, std::io::Error> {
write!(writer, "\x1b_Ga=")?;
let mut writer = match img {
Action::Transmit(image) => {
write!(writer, "t,")?;
image.write_transmit_to(writer, None, None, verbosity)?
}
Action::TransmitAndDisplay {
image,
config,
placement_id
} => {
write!(writer, "T,")?;
image.write_transmit_to(writer, *placement_id, Some(config), verbosity)?
}
Action::Query(image) => {
write!(writer, "q,")?;
image.write_transmit_to(writer, None, None, verbosity)?
}
Action::Display {
image_id,
placement_id,
config
} => {
write!(writer, "p,i={image_id},p={placement_id}")?;
writer = config.write_to(writer)?;
write!(writer, "\x1b\\")?;
writer
}
Action::Delete(del) => {
write!(writer, "d")?;
del.write_to(writer)?
}
};
writer.flush()?;
Ok(writer)
}
inner_for_stdio(self, writer, verbosity)
}
/// This pulls the [`NumberOrId`] and [`PlacementId`] out of self
fn extract_num_or_id_and_placement(&self) -> Option<(NumberOrId, Option<PlacementId>)> {
match self {
Self::Transmit(img) => Some((img.num_or_id, None)),
Self::TransmitAndDisplay {
image,
placement_id,
..
} => Some((image.num_or_id, *placement_id)),
Self::Query(img) => Some((img.num_or_id, None)),
Self::Display {
image_id,
placement_id,
..
} => Some((NumberOrId::Id(*image_id), Some(*placement_id))),
Self::Delete(_) => None
}
}
/// This is the main point of interaction with this library - to display an image, you need to
/// create an [`Action`] and then call this function on it.
///
/// This function does two main things:
/// 1. Writes the necessary escape codes to `writer`, then flushes it.
/// 2. Unlinks the shared memory object if necessary (see [`Medium::SharedMemObject`])
/// 3. Reads in the terminal's response via `reader`
/// 4. Parses the terminal's response and returns any errors that occur or are transmitted
///
/// Steps 1 & 2 are performed simply by calling [`Self::write_transmit_to`], so this library
/// can be used in a sans-io method by using that instead. (**TODO**: make the parse method pub
/// in a more ergonomic API)
///
/// For this function to work correctly, `writer` should be writing directly to something that
/// flushes directly to a kitty-supporting terminal. This function assumes that, once flushed
/// to `writer`, the terminal will respond and this response can be read by `reader`.
///
/// [`Medium::SharedMemObject`]: crate::medium::Medium::SharedMemObject
///
/// # Errors
///
/// This can return errors if:
/// - Writing to `writer` fails at any point
/// - The `reader` fails to read a response from the terminal
/// - The response from the terminal is unparseable
/// - The response from the terminal informs us that it ran into an error
pub fn execute<W: Write, I: InputReader>(
self,
writer: W,
reader: I
) -> Result<(W, Option<ImageId>), TransmitError<I::Error>> {
let id_and_p = self.extract_num_or_id_and_placement();
let writer = self
.write_transmit_to(writer, Verbosity::All)
.map_err(TransmitError::Writing)?;
let img_id = if let Some((id_or_num, placement_id)) = id_and_p {
Some(read_parse_response(reader, id_or_num, placement_id)?)
} else {
None
};
Ok((writer, img_id))
}
/// An async version of [`Self::execute`] - check its documentation for more details
///
/// # Errors
///
/// - This may return an error in the same situations that [`Self::execute`] may.
pub async fn execute_async<W: Write, I: AsyncInputReader>(
self,
writer: W,
reader: I
) -> Result<(W, Option<ImageId>), TransmitError<I::Error>> {
let id_and_p = self.extract_num_or_id_and_placement();
let writer = self
.write_transmit_to(writer, Verbosity::All)
.map_err(TransmitError::Writing)?;
let img_id = if let Some((id_or_num, placement_id)) = id_and_p {
Some(read_parse_response_async(reader, id_or_num, placement_id).await?)
} else {
None
};
Ok((writer, img_id))
}
}