1use crossterm::Command;
2use std::fmt;
3
4#[macro_export]
5#[doc(hidden)]
6macro_rules! csi {
7 ($( $l:expr ),*) => { concat!("\x1b", $( $l ),*) };
8}
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub struct Home;
12impl Command for Home {
13 fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
14 f.write_str(csi!("1~"))
15 }
16 #[cfg(windows)]
17 fn execute_winapi(&self) -> std::io::Result<()> {
18 Ok(())
19 }
20}
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub struct Insert;
24impl Command for Insert {
25 fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
26 f.write_str(csi!("2~"))
27 }
28 #[cfg(windows)]
29 fn execute_winapi(&self) -> std::io::Result<()> {
30 Ok(())
31 }
32}
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35pub struct Del;
36impl Command for Del {
37 fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
38 f.write_str(csi!("3~"))
39 }
40 #[cfg(windows)]
41 fn execute_winapi(&self) -> std::io::Result<()> {
42 Ok(())
43 }
44}
45
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47pub struct End;
48impl Command for End {
49 fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
50 f.write_str(csi!("4~"))
51 }
52 #[cfg(windows)]
53 fn execute_winapi(&self) -> std::io::Result<()> {
54 Ok(())
55 }
56}
57
58#[derive(Debug, Clone, Copy, PartialEq, Eq)]
59pub struct PgUp;
60impl Command for PgUp {
61 fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
62 f.write_str(csi!("5~"))
63 }
64 #[cfg(windows)]
65 fn execute_winapi(&self) -> std::io::Result<()> {
66 Ok(())
67 }
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71pub struct PgDn;
72impl Command for PgDn {
73 fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
74 f.write_str(csi!("6~"))
75 }
76 #[cfg(windows)]
77 fn execute_winapi(&self) -> std::io::Result<()> {
78 Ok(())
79 }
80}
81
82#[derive(Debug, Clone, Copy, PartialEq, Eq)]
83pub struct F(pub u8);
84impl Command for F {
85 fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
86 let index = match self.0 {
87 0 => "10", 1 => "11", 2 => "12", 3 => "13", 4 => "14", 5 => "15", 6 => "17", 7 => "18", 8 => "19", 9 => "20", 10 => "21", 11 => "23", 12 => "24", 13 => "25", 14 => "26", 15 => "28", 16 => "29", 17 => "31", 18 => "32", 19 => "33", 20 => "34", _ => panic!("Unsupported F key"),
109 };
110 write!(f, csi!("{}~"), index)
111 }
112 #[cfg(windows)]
113 fn execute_winapi(&self) -> std::io::Result<()> {
114 Ok(())
115 }
116}
117
118#[derive(Debug, Clone, Copy, PartialEq, Eq)]
119pub struct Backspace;
120impl Command for Backspace {
121 fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
122 f.write_str(csi!("D"))?;
123 f.write_str(csi!("P"))
124 }
125 #[cfg(windows)]
126 fn execute_winapi(&self) -> std::io::Result<()> {
127 Ok(())
128 }
129}
130
131#[derive(Debug, Clone, Copy, PartialEq, Eq)]
132pub struct Enter;
133impl Command for Enter {
134 fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
135 f.write_str("\n")
136 }
137 #[cfg(windows)]
138 fn execute_winapi(&self) -> std::io::Result<()> {
139 Ok(())
140 }
141}
142
143#[derive(Debug, Clone, Copy, PartialEq, Eq)]
144pub struct Up;
145impl Command for Up {
146 fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
147 f.write_str(csi!("OA"))
148 }
149 #[cfg(windows)]
150 fn execute_winapi(&self) -> std::io::Result<()> {
151 Ok(())
152 }
153}
154#[derive(Debug, Clone, Copy, PartialEq, Eq)]
155pub struct Down;
156impl Command for Down {
157 fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
158 f.write_str(csi!("OB"))
159 }
160 #[cfg(windows)]
161 fn execute_winapi(&self) -> std::io::Result<()> {
162 Ok(())
163 }
164}
165
166#[derive(Debug, Clone, Copy, PartialEq, Eq)]
167pub struct Left;
168impl Command for Left {
169 fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
170 f.write_str(csi!("0D"))
171 }
172 #[cfg(windows)]
173 fn execute_winapi(&self) -> std::io::Result<()> {
174 Ok(())
175 }
176}
177
178#[derive(Debug, Clone, Copy, PartialEq, Eq)]
179pub struct Right;
180impl Command for Right {
181 fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
182 f.write_str(csi!("0C"))
183 }
184 #[cfg(windows)]
185 fn execute_winapi(&self) -> std::io::Result<()> {
186 Ok(())
187 }
188}
189
190#[derive(Debug, Clone, Copy, PartialEq, Eq)]
191pub struct Esc;
192impl Command for Esc {
193 fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
194 f.write_str("\x1b")
195 }
196 #[cfg(windows)]
197 fn execute_winapi(&self) -> std::io::Result<()> {
198 Ok(())
199 }
200}