Skip to main content

indigo_term/
lib.rs

1mod cursor;
2mod event;
3mod key;
4mod parser;
5mod reader;
6mod style;
7mod tty;
8
9use std::fmt::{self, Display};
10
11pub use crate::{cursor::*, event::Event, key::*, reader::Reader, style::*, tty::Tty};
12
13// Clearing
14
15pub const CLEAR_TO_END_OF_LINE: &str = "\x1b[K";
16pub const CLEAR_TO_START_OF_LINE: &str = "\x1b[1K";
17pub const CLEAR_LINE: &str = "\x1b[2K";
18pub const CLEAR_TO_END_OF_SCREEN: &str = "\x1b[J";
19pub const CLEAR_TO_START_OF_SCREEN: &str = "\x1b[1J";
20pub const CLEAR_SCREEN: &str = "\x1b[2J";
21
22// Modes
23
24pub struct ModeQuery(pub u16);
25
26impl Display for ModeQuery {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        write!(f, "\x1b[?{}$p", self.0)
29    }
30}
31
32pub struct ModeSet(pub u16);
33
34impl Display for ModeSet {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        write!(f, "\x1b[?{}h", self.0)
37    }
38}
39
40pub struct ModeReset(pub u16);
41
42impl Display for ModeReset {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        write!(f, "\x1b[?{}l", self.0)
45    }
46}
47
48#[derive(Debug, PartialEq)]
49pub enum ModeSetting {
50    NotRecognized,
51    Set,
52    Reset,
53    PermanentlySet,
54    PermanentlyReset,
55}
56
57impl ModeSetting {
58    #[must_use]
59    pub fn is_recognized(&self) -> bool {
60        !matches!(self, Self::NotRecognized)
61    }
62
63    #[must_use]
64    pub fn is_enabled(&self) -> bool {
65        matches!(self, Self::Set | Self::PermanentlySet)
66    }
67
68    #[must_use]
69    pub fn is_disabled(&self) -> bool {
70        matches!(
71            self,
72            Self::NotRecognized | Self::Reset | Self::PermanentlyReset
73        )
74    }
75
76    #[must_use]
77    pub fn is_configurable(&self) -> bool {
78        matches!(self, Self::Set | Self::Reset)
79    }
80}
81
82// Primary Device Attributes (DA1)
83// https://vt100.net/docs/vt510-rm/DA1.html
84
85pub const DA1_QUERY: &str = "\x1b[c";
86
87// Bracketed paste
88// https://en.wikipedia.org/wiki/Bracketed-paste
89
90pub const BRACKETED_PASTE_MODE: u16 = 2004;
91pub const BRACKETED_PASTE_QUERY: ModeQuery = ModeQuery(BRACKETED_PASTE_MODE);
92pub const BRACKETED_PASTE_SET: ModeSet = ModeSet(BRACKETED_PASTE_MODE);
93pub const BRACKETED_PASTE_RESET: ModeReset = ModeReset(BRACKETED_PASTE_MODE);
94pub const BRACKETED_PASTE_BEGIN: &str = "\x1b[200~";
95pub const BRACKETED_PASTE_END: &str = "\x1b[201~";
96
97// In-band resize
98// https://gist.github.com/rockorager/e695fb2924d36b2bcf1fff4a3704bd83
99
100pub const IN_BAND_RESIZE_MODE: u16 = 2048;
101pub const IN_BAND_RESIZE_QUERY: ModeQuery = ModeQuery(IN_BAND_RESIZE_MODE);
102pub const IN_BAND_RESIZE_SET: ModeSet = ModeSet(IN_BAND_RESIZE_MODE);
103pub const IN_BAND_RESIZE_RESET: ModeReset = ModeReset(IN_BAND_RESIZE_MODE);
104
105// Synchronized output
106// https://github.com/contour-terminal/vt-extensions/blob/master/synchronized-output.md
107
108pub const SYNC_UPDATE_MODE: u16 = 2026;
109pub const SYNC_UPDATE_QUERY: ModeQuery = ModeQuery(SYNC_UPDATE_MODE);
110pub const SYNC_UPDATE_SET: ModeSet = ModeSet(SYNC_UPDATE_MODE);
111pub const SYNC_UPDATE_RESET: ModeReset = ModeReset(SYNC_UPDATE_MODE);