Skip to main content

indigo_term/
output.rs

1//! What you send to the terminal
2
3use bitflags::bitflags;
4use std::fmt::{self, Display};
5
6// Cursor
7
8pub struct CursorUp(pub u16);
9
10impl Display for CursorUp {
11    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12        write!(f, "\x1b[{}A", self.0)
13    }
14}
15
16pub struct CursorDown(pub u16);
17
18impl Display for CursorDown {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        write!(f, "\x1b[{}B", self.0)
21    }
22}
23
24pub struct CursorForward(pub u16);
25
26impl Display for CursorForward {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        write!(f, "\x1b[{}C", self.0)
29    }
30}
31
32pub struct CursorBackward(pub u16);
33
34impl Display for CursorBackward {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        write!(f, "\x1b[{}D", self.0)
37    }
38}
39
40pub struct CursorNextLine(pub u16);
41
42impl Display for CursorNextLine {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        write!(f, "\x1b[{}E", self.0)
45    }
46}
47
48pub struct CursorPrevLine(pub u16);
49
50impl Display for CursorPrevLine {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        write!(f, "\x1b[{}F", self.0)
53    }
54}
55
56pub struct CursorColumn(pub u16);
57
58impl Display for CursorColumn {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        write!(f, "\x1b[{}G", self.0)
61    }
62}
63
64pub struct CursorPosition(pub u16, pub u16);
65
66impl Display for CursorPosition {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68        write!(f, "\x1b[{};{}H", self.0, self.1)
69    }
70}
71
72pub const CURSOR_SAVE: &str = "\x1b[s";
73pub const CURSOR_RESTORE: &str = "\x1b[u";
74pub const CURSOR_HIDE: &str = "\x1b[?25l";
75pub const CURSOR_SHOW: &str = "\x1b[?25h";
76
77// Device Status Report (DSR), answered with a Cursor Position Report (CPR)
78// https://vt100.net/docs/vt510-rm/DSR-CPR.html
79pub const CURSOR_POSITION_QUERY: &str = "\x1b[6n";
80
81// Style
82
83pub const BOLD: &str = "\x1b[1m";
84pub const ITALIC: &str = "\x1b[3m";
85pub const UNDERLINE: &str = "\x1b[4m";
86pub const RESET: &str = "\x1b[0m";
87
88pub enum Color {
89    Black,
90    Red,
91    Green,
92    Yellow,
93    Blue,
94    Magenta,
95    Cyan,
96    White,
97    /// A color from the terminal's 256-color palette
98    Indexed(u8),
99    /// A 24-bit color
100    Rgb(u8, u8, u8),
101}
102
103pub struct Fg(pub Color);
104
105impl Display for Fg {
106    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107        match self.0 {
108            Color::Black => write!(f, "\x1b[30m"),
109            Color::Red => write!(f, "\x1b[31m"),
110            Color::Green => write!(f, "\x1b[32m"),
111            Color::Yellow => write!(f, "\x1b[33m"),
112            Color::Blue => write!(f, "\x1b[34m"),
113            Color::Magenta => write!(f, "\x1b[35m"),
114            Color::Cyan => write!(f, "\x1b[36m"),
115            Color::White => write!(f, "\x1b[37m"),
116            Color::Indexed(index) => write!(f, "\x1b[38;5;{index}m"),
117            Color::Rgb(r, g, b) => write!(f, "\x1b[38;2;{r};{g};{b}m"),
118        }
119    }
120}
121
122pub struct Bg(pub Color);
123
124impl Display for Bg {
125    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126        match self.0 {
127            Color::Black => write!(f, "\x1b[40m"),
128            Color::Red => write!(f, "\x1b[41m"),
129            Color::Green => write!(f, "\x1b[42m"),
130            Color::Yellow => write!(f, "\x1b[43m"),
131            Color::Blue => write!(f, "\x1b[44m"),
132            Color::Magenta => write!(f, "\x1b[45m"),
133            Color::Cyan => write!(f, "\x1b[46m"),
134            Color::White => write!(f, "\x1b[47m"),
135            Color::Indexed(index) => write!(f, "\x1b[48;5;{index}m"),
136            Color::Rgb(r, g, b) => write!(f, "\x1b[48;2;{r};{g};{b}m"),
137        }
138    }
139}
140
141// Clearing
142
143pub const CLEAR_TO_END_OF_LINE: &str = "\x1b[K";
144pub const CLEAR_TO_START_OF_LINE: &str = "\x1b[1K";
145pub const CLEAR_LINE: &str = "\x1b[2K";
146pub const CLEAR_TO_END_OF_SCREEN: &str = "\x1b[J";
147pub const CLEAR_TO_START_OF_SCREEN: &str = "\x1b[1J";
148pub const CLEAR_SCREEN: &str = "\x1b[2J";
149
150// Modes
151
152pub struct ModeQuery(pub u16);
153
154impl Display for ModeQuery {
155    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156        write!(f, "\x1b[?{}$p", self.0)
157    }
158}
159
160pub struct ModeSet(pub u16);
161
162impl Display for ModeSet {
163    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
164        write!(f, "\x1b[?{}h", self.0)
165    }
166}
167
168pub struct ModeReset(pub u16);
169
170impl Display for ModeReset {
171    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172        write!(f, "\x1b[?{}l", self.0)
173    }
174}
175
176// Primary Device Attributes (DA1)
177// https://vt100.net/docs/vt510-rm/DA1.html
178
179pub const DA1_QUERY: &str = "\x1b[c";
180
181// Alternate screen
182// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-The-Alternate-Screen-Buffer
183
184pub const ALTERNATE_SCREEN_MODE: u16 = 1049;
185pub const ALTERNATE_SCREEN_QUERY: ModeQuery = ModeQuery(ALTERNATE_SCREEN_MODE);
186pub const ALTERNATE_SCREEN_SET: ModeSet = ModeSet(ALTERNATE_SCREEN_MODE);
187pub const ALTERNATE_SCREEN_RESET: ModeReset = ModeReset(ALTERNATE_SCREEN_MODE);
188
189// Bracketed paste
190// https://en.wikipedia.org/wiki/Bracketed-paste
191
192pub const BRACKETED_PASTE_MODE: u16 = 2004;
193pub const BRACKETED_PASTE_QUERY: ModeQuery = ModeQuery(BRACKETED_PASTE_MODE);
194pub const BRACKETED_PASTE_SET: ModeSet = ModeSet(BRACKETED_PASTE_MODE);
195pub const BRACKETED_PASTE_RESET: ModeReset = ModeReset(BRACKETED_PASTE_MODE);
196
197// In-band resize
198// https://gist.github.com/rockorager/e695fb2924d36b2bcf1fff4a3704bd83
199
200pub const IN_BAND_RESIZE_MODE: u16 = 2048;
201pub const IN_BAND_RESIZE_QUERY: ModeQuery = ModeQuery(IN_BAND_RESIZE_MODE);
202pub const IN_BAND_RESIZE_SET: ModeSet = ModeSet(IN_BAND_RESIZE_MODE);
203pub const IN_BAND_RESIZE_RESET: ModeReset = ModeReset(IN_BAND_RESIZE_MODE);
204
205// Synchronized output
206// https://github.com/contour-terminal/vt-extensions/blob/master/synchronized-output.md
207
208pub const SYNC_UPDATE_MODE: u16 = 2026;
209pub const SYNC_UPDATE_QUERY: ModeQuery = ModeQuery(SYNC_UPDATE_MODE);
210pub const SYNC_UPDATE_SET: ModeSet = ModeSet(SYNC_UPDATE_MODE);
211pub const SYNC_UPDATE_RESET: ModeReset = ModeReset(SYNC_UPDATE_MODE);
212
213// Color scheme
214// https://github.com/contour-terminal/contour/blob/master/docs/vt-extensions/color-palette-update-notifications.md
215
216pub const COLOR_SCHEME_QUERY: &str = "\x1b[?996n";
217pub const COLOR_SCHEME_UPDATES_MODE: u16 = 2031;
218pub const COLOR_SCHEME_UPDATES_QUERY: ModeQuery = ModeQuery(COLOR_SCHEME_UPDATES_MODE);
219pub const COLOR_SCHEME_UPDATES_SET: ModeSet = ModeSet(COLOR_SCHEME_UPDATES_MODE);
220pub const COLOR_SCHEME_UPDATES_RESET: ModeReset = ModeReset(COLOR_SCHEME_UPDATES_MODE);
221
222// Kitty keyboard protocol
223// https://sw.kovidgoyal.net/kitty/keyboard-protocol/
224
225bitflags! {
226    #[derive(Debug, PartialEq)]
227    pub struct KeyboardEnhancementFlags: u8 {
228        const DISAMBIGUATE = 1;
229        const REPORT_EVENTS = 2;
230        const REPORT_ALTERNATES = 4; // Unsupported
231        const REPORT_ALL_KEYS = 8;   // Supported, but only for keys we represent
232        const REPORT_TEXT = 16;      // Ignored, but safe to enable
233    }
234}
235
236impl Display for KeyboardEnhancementFlags {
237    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
238        write!(f, "{}", self.bits())
239    }
240}
241
242pub struct KeyboardEnhancementFlagsPush(pub KeyboardEnhancementFlags);
243
244impl Display for KeyboardEnhancementFlagsPush {
245    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
246        write!(f, "\x1b[>{}u", self.0)
247    }
248}
249
250pub struct KeyboardEnhancementFlagsPop;
251
252impl Display for KeyboardEnhancementFlagsPop {
253    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
254        write!(f, "\x1b[<1u")
255    }
256}
257
258pub const KEYBOARD_ENHANCEMENT_FLAGS_QUERY: &str = "\x1b[?u";
259
260// Pointer shapes
261// https://sw.kovidgoyal.net/kitty/pointer-shapes/
262
263// TODO: Add query for current pointer shape.
264// TODO: Add methods to `Terminal`. Needs to handle separate stacks for main and alternate screens.
265
266#[derive(Clone, Copy, PartialEq)]
267pub enum PointerShape {
268    Alias,
269    Cell,
270    Copy,
271    Crosshair,
272    Default,
273    EResize,
274    EwResize,
275    Grab,
276    Grabbing,
277    Help,
278    Move,
279    NResize,
280    NeResize,
281    NeswResize,
282    NoDrop,
283    NotAllowed,
284    NsResize,
285    NwResize,
286    NwseResize,
287    Pointer,
288    Progress,
289    SResize,
290    SeResize,
291    SwResize,
292    Text,
293    VerticalText,
294    WResize,
295    Wait,
296    ZoomIn,
297    ZoomOut,
298}
299
300impl PointerShape {
301    pub const ALL: [Self; 30] = [
302        Self::Alias,
303        Self::Cell,
304        Self::Copy,
305        Self::Crosshair,
306        Self::Default,
307        Self::EResize,
308        Self::EwResize,
309        Self::Grab,
310        Self::Grabbing,
311        Self::Help,
312        Self::Move,
313        Self::NResize,
314        Self::NeResize,
315        Self::NeswResize,
316        Self::NoDrop,
317        Self::NotAllowed,
318        Self::NsResize,
319        Self::NwResize,
320        Self::NwseResize,
321        Self::Pointer,
322        Self::Progress,
323        Self::SResize,
324        Self::SeResize,
325        Self::SwResize,
326        Self::Text,
327        Self::VerticalText,
328        Self::WResize,
329        Self::Wait,
330        Self::ZoomIn,
331        Self::ZoomOut,
332    ];
333}
334
335impl Display for PointerShape {
336    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
337        let name = match self {
338            Self::Alias => "alias",
339            Self::Cell => "cell",
340            Self::Copy => "copy",
341            Self::Crosshair => "crosshair",
342            Self::Default => "default",
343            Self::EResize => "e-resize",
344            Self::EwResize => "ew-resize",
345            Self::Grab => "grab",
346            Self::Grabbing => "grabbing",
347            Self::Help => "help",
348            Self::Move => "move",
349            Self::NResize => "n-resize",
350            Self::NeResize => "ne-resize",
351            Self::NeswResize => "nesw-resize",
352            Self::NoDrop => "no-drop",
353            Self::NotAllowed => "not-allowed",
354            Self::NsResize => "ns-resize",
355            Self::NwResize => "nw-resize",
356            Self::NwseResize => "nwse-resize",
357            Self::Pointer => "pointer",
358            Self::Progress => "progress",
359            Self::SResize => "s-resize",
360            Self::SeResize => "se-resize",
361            Self::SwResize => "sw-resize",
362            Self::Text => "text",
363            Self::VerticalText => "vertical-text",
364            Self::WResize => "w-resize",
365            Self::Wait => "wait",
366            Self::ZoomIn => "zoom-in",
367            Self::ZoomOut => "zoom-out",
368        };
369        write!(f, "{name}")
370    }
371}
372
373/// Replaces the shape at the top of the stack. Omits the optional `=` for xterm compatibility.
374pub struct PointerShapeSet(pub PointerShape);
375
376impl Display for PointerShapeSet {
377    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
378        write!(f, "\x1b]22;{}\x1b\\", self.0)
379    }
380}
381
382pub struct PointerShapePush(pub PointerShape);
383
384impl Display for PointerShapePush {
385    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
386        write!(f, "\x1b]22;>{}\x1b\\", self.0)
387    }
388}
389
390pub struct PointerShapePop;
391
392impl Display for PointerShapePop {
393    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
394        write!(f, "\x1b]22;<\x1b\\")
395    }
396}