返回 CodeWhale
keys.rs
根目录 / crates / tui / tests / support / qa_harness / keys.rs
1 //! Byte-sequence builders for keys and paste.
2 //!
3 //! These produce the raw bytes a real terminal would deliver to the child's
4 //! PTY slave. They match crossterm's input-decoding tables: legacy sequences
5 //! by default, CSI-u for the modified Enter chords the TUI opts into, mouse
6 //! capture off, and bracketed paste on.
7
8 /// Plain key press helpers.
9 pub mod key {
10 pub fn ch(c: char) -> Vec<u8> {
11 let mut buf = [0u8; 4];
12 c.encode_utf8(&mut buf).as_bytes().to_vec()
13 }
14
15 pub fn enter() -> Vec<u8> {
16 b"\r".to_vec()
17 }
18
19 /// Enhanced-keyboard (CSI-u) encodings used by the TUI for modified Enter.
20 pub fn shift_enter() -> Vec<u8> {
21 b"\x1b[13;2u".to_vec()
22 }
23
24 pub fn alt_enter() -> Vec<u8> {
25 b"\x1b[13;3u".to_vec()
26 }
27
28 pub fn ctrl_enter() -> Vec<u8> {
29 b"\x1b[13;5u".to_vec()
30 }
31
32 pub fn ctrl_j() -> Vec<u8> {
33 vec![0x0a]
34 }
35
36 pub fn ctrl_g() -> Vec<u8> {
37 vec![0x07]
38 }
39
40 pub fn ctrl_s() -> Vec<u8> {
41 vec![0x13]
42 }
43
44 pub fn esc() -> Vec<u8> {
45 vec![0x1b]
46 }
47
48 pub fn down() -> Vec<u8> {
49 b"\x1b[B".to_vec()
50 }
51
52 /// Shift+Tab as the xterm BackTab sequence accepted by crossterm.
53 pub fn backtab() -> Vec<u8> {
54 b"\x1b[Z".to_vec()
55 }
56
57 pub fn page_up() -> Vec<u8> {
58 b"\x1b[5~".to_vec()
59 }
60
61 pub fn text(s: &str) -> Vec<u8> {
62 s.as_bytes().to_vec()
63 }
64
65 pub fn alt(c: char) -> Vec<u8> {
66 let mut out = vec![0x1b];
67 out.extend(ch(c));
68 out
69 }
70
71 /// `Ctrl+<letter>` as the ASCII control byte a legacy terminal sends.
72 /// Panics on a non-alphabetic argument so a typo cannot silently become
73 /// a different key.
74 pub fn ctrl(c: char) -> Vec<u8> {
75 assert!(c.is_ascii_alphabetic(), "ctrl() takes an ASCII letter");
76 vec![(c.to_ascii_lowercase() as u8) - b'a' + 1]
77 }
78
79 pub fn ctrl_c() -> Vec<u8> {
80 vec![0x03]
81 }
82
83 pub fn ctrl_d() -> Vec<u8> {
84 vec![0x04]
85 }
86
87 pub fn tab() -> Vec<u8> {
88 b"\t".to_vec()
89 }
90
91 /// DEL (0x7f) — what every xterm-family terminal sends for Backspace.
92 pub fn backspace() -> Vec<u8> {
93 vec![0x7f]
94 }
95
96 pub fn backspaces(count: usize) -> Vec<u8> {
97 vec![0x7f; count]
98 }
99
100 pub fn up() -> Vec<u8> {
101 b"\x1b[A".to_vec()
102 }
103
104 pub fn right() -> Vec<u8> {
105 b"\x1b[C".to_vec()
106 }
107
108 pub fn left() -> Vec<u8> {
109 b"\x1b[D".to_vec()
110 }
111
112 pub fn page_down() -> Vec<u8> {
113 b"\x1b[6~".to_vec()
114 }
115
116 /// SS3-encoded function keys, the form xterm-family terminals send.
117 pub fn f1() -> Vec<u8> {
118 b"\x1bOP".to_vec()
119 }
120
121 pub fn f2() -> Vec<u8> {
122 b"\x1bOQ".to_vec()
123 }
124
125 pub fn f4() -> Vec<u8> {
126 b"\x1bOS".to_vec()
127 }
128 }
129
130 /// Focus-reporting sequences the terminal sends when the window gains or
131 /// loses focus (DEC private mode 1004). The TUI re-establishes its terminal
132 /// modes on `FocusGained`, so these are inputs, not decoration.
133 pub mod focus {
134 pub fn gained() -> Vec<u8> {
135 b"\x1b[I".to_vec()
136 }
137
138 pub fn lost() -> Vec<u8> {
139 b"\x1b[O".to_vec()
140 }
141 }
142
143 /// SGR mouse sequences use one-based terminal coordinates.
144 pub mod mouse {
145 pub fn down(row: u16, col: u16) -> Vec<u8> {
146 format!("\x1b[<0;{};{}M", col + 1, row + 1).into_bytes()
147 }
148
149 pub fn drag(row: u16, col: u16) -> Vec<u8> {
150 format!("\x1b[<32;{};{}M", col + 1, row + 1).into_bytes()
151 }
152
153 pub fn up(row: u16, col: u16) -> Vec<u8> {
154 format!("\x1b[<0;{};{}m", col + 1, row + 1).into_bytes()
155 }
156
157 pub fn click(row: u16, col: u16) -> Vec<u8> {
158 format!(
159 "\x1b[<0;{};{}M\x1b[<0;{};{}m",
160 col + 1,
161 row + 1,
162 col + 1,
163 row + 1
164 )
165 .into_bytes()
166 }
167
168 pub fn wheel_down(row: u16, col: u16) -> Vec<u8> {
169 format!("\x1b[<65;{};{}M", col + 1, row + 1).into_bytes()
170 }
171
172 pub fn wheel_up(row: u16, col: u16) -> Vec<u8> {
173 format!("\x1b[<64;{};{}M", col + 1, row + 1).into_bytes()
174 }
175 }
176
177 /// Bracketed-paste helpers.
178 ///
179 /// Wraps the payload in `ESC [ 2 0 0 ~` … `ESC [ 2 0 1 ~` so the receiver sees
180 /// a `crossterm::Event::Paste(text)` rather than a key-by-key stream.
181 pub mod paste {
182 pub fn bracketed(text: &str) -> Vec<u8> {
183 let mut out = b"\x1b[200~".to_vec();
184 out.extend_from_slice(text.as_bytes());
185 out.extend_from_slice(b"\x1b[201~");
186 out
187 }
188
189 /// Same as [`bracketed`] but does not wrap — simulates a terminal that
190 /// has bracketed paste disabled (e.g. some Windows PowerShell setups).
191 /// The child sees the bytes as ordinary keystrokes; an embedded `\n`
192 /// becomes an Enter press, which is what reproduces #1073.
193 pub fn unbracketed(text: &str) -> Vec<u8> {
194 text.replace('\n', "\r").as_bytes().to_vec()
195 }
196 }
197
197 lines RUST