1use alacritty_terminal::term::TermMode;
2use gpui::keymap::Keystroke;
3
4#[derive(Debug)]
5pub enum Modifiers {
6 None,
7 Alt,
8 Ctrl,
9 Shift,
10 CtrlShift,
11 Other,
12}
13
14impl Modifiers {
15 fn new(ks: &Keystroke) -> Self {
16 match (ks.alt, ks.ctrl, ks.shift, ks.cmd) {
17 (false, false, false, false) => Modifiers::None,
18 (true, false, false, false) => Modifiers::Alt,
19 (false, true, false, false) => Modifiers::Ctrl,
20 (false, false, true, false) => Modifiers::Shift,
21 (false, true, true, false) => Modifiers::CtrlShift,
22 _ => Modifiers::Other,
23 }
24 }
25
26 fn any(&self) -> bool {
27 match &self {
28 Modifiers::None => false,
29 Modifiers::Alt => true,
30 Modifiers::Ctrl => true,
31 Modifiers::Shift => true,
32 Modifiers::CtrlShift => true,
33 Modifiers::Other => true,
34 }
35 }
36}
37
38///This function checks if to_esc_str would work, assuming all terminal settings are off.
39///Note that this function is conservative. It can fail in cases where the actual to_esc_str succeeds.
40///This is unavoidable for our use case. GPUI cannot wait until we acquire the terminal
41///lock to determine whether we could actually send the keystroke with the current settings. Therefore,
42///This conservative guess is used instead. Note that in practice the case where this method
43///Returns false when the actual terminal would consume the keystroke never happens. All keystrokes
44///that depend on terminal modes also have a mapping that doesn't depend on the terminal mode.
45///This is fragile, but as these mappings are locked up in legacy compatibility, it's probably good enough
46pub fn might_convert(keystroke: &Keystroke) -> bool {
47 to_esc_str(keystroke, &TermMode::NONE).is_some()
48}
49
50pub fn to_esc_str(keystroke: &Keystroke, mode: &TermMode) -> Option<String> {
51 let modifiers = Modifiers::new(keystroke);
52
53 // Manual Bindings including modifiers
54 let manual_esc_str = match (keystroke.key.as_ref(), &modifiers) {
55 //Basic special keys
56 ("tab", Modifiers::None) => Some("\x09".to_string()),
57 ("escape", Modifiers::None) => Some("\x1b".to_string()),
58 ("enter", Modifiers::None) => Some("\x0d".to_string()),
59 ("backspace", Modifiers::None) => Some("\x7f".to_string()),
60 //Interesting escape codes
61 ("tab", Modifiers::Shift) => Some("\x1b[Z".to_string()),
62 ("backspace", Modifiers::Alt) => Some("\x1b\x7f".to_string()),
63 ("backspace", Modifiers::Shift) => Some("\x7f".to_string()),
64 ("home", Modifiers::Shift) if mode.contains(TermMode::ALT_SCREEN) => {
65 Some("\x1b[1;2H".to_string())
66 }
67 ("end", Modifiers::Shift) if mode.contains(TermMode::ALT_SCREEN) => {
68 Some("\x1b[1;2F".to_string())
69 }
70 ("pageup", Modifiers::Shift) if mode.contains(TermMode::ALT_SCREEN) => {
71 Some("\x1b[5;2~".to_string())
72 }
73 ("pagedown", Modifiers::Shift) if mode.contains(TermMode::ALT_SCREEN) => {
74 Some("\x1b[6;2~".to_string())
75 }
76 ("home", Modifiers::None) if mode.contains(TermMode::APP_CURSOR) => {
77 Some("\x1bOH".to_string())
78 }
79 ("home", Modifiers::None) if !mode.contains(TermMode::APP_CURSOR) => {
80 Some("\x1b[H".to_string())
81 }
82 ("end", Modifiers::None) if mode.contains(TermMode::APP_CURSOR) => {
83 Some("\x1bOF".to_string())
84 }
85 ("end", Modifiers::None) if !mode.contains(TermMode::APP_CURSOR) => {
86 Some("\x1b[F".to_string())
87 }
88 ("up", Modifiers::None) if mode.contains(TermMode::APP_CURSOR) => {
89 Some("\x1bOA".to_string())
90 }
91 ("up", Modifiers::None) if !mode.contains(TermMode::APP_CURSOR) => {
92 Some("\x1b[A".to_string())
93 }
94 ("down", Modifiers::None) if mode.contains(TermMode::APP_CURSOR) => {
95 Some("\x1bOB".to_string())
96 }
97 ("down", Modifiers::None) if !mode.contains(TermMode::APP_CURSOR) => {
98 Some("\x1b[B".to_string())
99 }
100 ("right", Modifiers::None) if mode.contains(TermMode::APP_CURSOR) => {
101 Some("\x1bOC".to_string())
102 }
103 ("right", Modifiers::None) if !mode.contains(TermMode::APP_CURSOR) => {
104 Some("\x1b[C".to_string())
105 }
106 ("left", Modifiers::None) if mode.contains(TermMode::APP_CURSOR) => {
107 Some("\x1bOD".to_string())
108 }
109 ("left", Modifiers::None) if !mode.contains(TermMode::APP_CURSOR) => {
110 Some("\x1b[D".to_string())
111 }
112 ("back", Modifiers::None) => Some("\x7f".to_string()),
113 ("insert", Modifiers::None) => Some("\x1b[2~".to_string()),
114 ("delete", Modifiers::None) => Some("\x1b[3~".to_string()),
115 ("pageup", Modifiers::None) => Some("\x1b[5~".to_string()),
116 ("pagedown", Modifiers::None) => Some("\x1b[6~".to_string()),
117 ("f1", Modifiers::None) => Some("\x1bOP".to_string()),
118 ("f2", Modifiers::None) => Some("\x1bOQ".to_string()),
119 ("f3", Modifiers::None) => Some("\x1bOR".to_string()),
120 ("f4", Modifiers::None) => Some("\x1bOS".to_string()),
121 ("f5", Modifiers::None) => Some("\x1b[15~".to_string()),
122 ("f6", Modifiers::None) => Some("\x1b[17~".to_string()),
123 ("f7", Modifiers::None) => Some("\x1b[18~".to_string()),
124 ("f8", Modifiers::None) => Some("\x1b[19~".to_string()),
125 ("f9", Modifiers::None) => Some("\x1b[20~".to_string()),
126 ("f10", Modifiers::None) => Some("\x1b[21~".to_string()),
127 ("f11", Modifiers::None) => Some("\x1b[23~".to_string()),
128 ("f12", Modifiers::None) => Some("\x1b[24~".to_string()),
129 ("f13", Modifiers::None) => Some("\x1b[25~".to_string()),
130 ("f14", Modifiers::None) => Some("\x1b[26~".to_string()),
131 ("f15", Modifiers::None) => Some("\x1b[28~".to_string()),
132 ("f16", Modifiers::None) => Some("\x1b[29~".to_string()),
133 ("f17", Modifiers::None) => Some("\x1b[31~".to_string()),
134 ("f18", Modifiers::None) => Some("\x1b[32~".to_string()),
135 ("f19", Modifiers::None) => Some("\x1b[33~".to_string()),
136 ("f20", Modifiers::None) => Some("\x1b[34~".to_string()),
137 // NumpadEnter, Action::Esc("\n".into());
138 //Mappings for caret notation keys
139 ("a", Modifiers::Ctrl) => Some("\x01".to_string()), //1
140 ("A", Modifiers::CtrlShift) => Some("\x01".to_string()), //1
141 ("b", Modifiers::Ctrl) => Some("\x02".to_string()), //2
142 ("B", Modifiers::CtrlShift) => Some("\x02".to_string()), //2
143 ("c", Modifiers::Ctrl) => Some("\x03".to_string()), //3
144 ("C", Modifiers::CtrlShift) => Some("\x03".to_string()), //3
145 ("d", Modifiers::Ctrl) => Some("\x04".to_string()), //4
146 ("D", Modifiers::CtrlShift) => Some("\x04".to_string()), //4
147 ("e", Modifiers::Ctrl) => Some("\x05".to_string()), //5
148 ("E", Modifiers::CtrlShift) => Some("\x05".to_string()), //5
149 ("f", Modifiers::Ctrl) => Some("\x06".to_string()), //6
150 ("F", Modifiers::CtrlShift) => Some("\x06".to_string()), //6
151 ("g", Modifiers::Ctrl) => Some("\x07".to_string()), //7
152 ("G", Modifiers::CtrlShift) => Some("\x07".to_string()), //7
153 ("h", Modifiers::Ctrl) => Some("\x08".to_string()), //8
154 ("H", Modifiers::CtrlShift) => Some("\x08".to_string()), //8
155 ("i", Modifiers::Ctrl) => Some("\x09".to_string()), //9
156 ("I", Modifiers::CtrlShift) => Some("\x09".to_string()), //9
157 ("j", Modifiers::Ctrl) => Some("\x0a".to_string()), //10
158 ("J", Modifiers::CtrlShift) => Some("\x0a".to_string()), //10
159 ("k", Modifiers::Ctrl) => Some("\x0b".to_string()), //11
160 ("K", Modifiers::CtrlShift) => Some("\x0b".to_string()), //11
161 ("l", Modifiers::Ctrl) => Some("\x0c".to_string()), //12
162 ("L", Modifiers::CtrlShift) => Some("\x0c".to_string()), //12
163 ("m", Modifiers::Ctrl) => Some("\x0d".to_string()), //13
164 ("M", Modifiers::CtrlShift) => Some("\x0d".to_string()), //13
165 ("n", Modifiers::Ctrl) => Some("\x0e".to_string()), //14
166 ("N", Modifiers::CtrlShift) => Some("\x0e".to_string()), //14
167 ("o", Modifiers::Ctrl) => Some("\x0f".to_string()), //15
168 ("O", Modifiers::CtrlShift) => Some("\x0f".to_string()), //15
169 ("p", Modifiers::Ctrl) => Some("\x10".to_string()), //16
170 ("P", Modifiers::CtrlShift) => Some("\x10".to_string()), //16
171 ("q", Modifiers::Ctrl) => Some("\x11".to_string()), //17
172 ("Q", Modifiers::CtrlShift) => Some("\x11".to_string()), //17
173 ("r", Modifiers::Ctrl) => Some("\x12".to_string()), //18
174 ("R", Modifiers::CtrlShift) => Some("\x12".to_string()), //18
175 ("s", Modifiers::Ctrl) => Some("\x13".to_string()), //19
176 ("S", Modifiers::CtrlShift) => Some("\x13".to_string()), //19
177 ("t", Modifiers::Ctrl) => Some("\x14".to_string()), //20
178 ("T", Modifiers::CtrlShift) => Some("\x14".to_string()), //20
179 ("u", Modifiers::Ctrl) => Some("\x15".to_string()), //21
180 ("U", Modifiers::CtrlShift) => Some("\x15".to_string()), //21
181 ("v", Modifiers::Ctrl) => Some("\x16".to_string()), //22
182 ("V", Modifiers::CtrlShift) => Some("\x16".to_string()), //22
183 ("w", Modifiers::Ctrl) => Some("\x17".to_string()), //23
184 ("W", Modifiers::CtrlShift) => Some("\x17".to_string()), //23
185 ("x", Modifiers::Ctrl) => Some("\x18".to_string()), //24
186 ("X", Modifiers::CtrlShift) => Some("\x18".to_string()), //24
187 ("y", Modifiers::Ctrl) => Some("\x19".to_string()), //25
188 ("Y", Modifiers::CtrlShift) => Some("\x19".to_string()), //25
189 ("z", Modifiers::Ctrl) => Some("\x1a".to_string()), //26
190 ("Z", Modifiers::CtrlShift) => Some("\x1a".to_string()), //26
191 ("@", Modifiers::Ctrl) => Some("\x00".to_string()), //0
192 ("[", Modifiers::Ctrl) => Some("\x1b".to_string()), //27
193 ("\\", Modifiers::Ctrl) => Some("\x1c".to_string()), //28
194 ("]", Modifiers::Ctrl) => Some("\x1d".to_string()), //29
195 ("^", Modifiers::Ctrl) => Some("\x1e".to_string()), //30
196 ("_", Modifiers::Ctrl) => Some("\x1f".to_string()), //31
197 ("?", Modifiers::Ctrl) => Some("\x7f".to_string()), //127
198 _ => None,
199 };
200 if manual_esc_str.is_some() {
201 return manual_esc_str;
202 }
203
204 // Automated bindings applying modifiers
205 if modifiers.any() {
206 let modifier_code = modifier_code(keystroke);
207 let modified_esc_str = match keystroke.key.as_ref() {
208 "up" => Some(format!("\x1b[1;{}A", modifier_code)),
209 "down" => Some(format!("\x1b[1;{}B", modifier_code)),
210 "right" => Some(format!("\x1b[1;{}C", modifier_code)),
211 "left" => Some(format!("\x1b[1;{}D", modifier_code)),
212 "f1" => Some(format!("\x1b[1;{}P", modifier_code)),
213 "f2" => Some(format!("\x1b[1;{}Q", modifier_code)),
214 "f3" => Some(format!("\x1b[1;{}R", modifier_code)),
215 "f4" => Some(format!("\x1b[1;{}S", modifier_code)),
216 "F5" => Some(format!("\x1b[15;{}~", modifier_code)),
217 "f6" => Some(format!("\x1b[17;{}~", modifier_code)),
218 "f7" => Some(format!("\x1b[18;{}~", modifier_code)),
219 "f8" => Some(format!("\x1b[19;{}~", modifier_code)),
220 "f9" => Some(format!("\x1b[20;{}~", modifier_code)),
221 "f10" => Some(format!("\x1b[21;{}~", modifier_code)),
222 "f11" => Some(format!("\x1b[23;{}~", modifier_code)),
223 "f12" => Some(format!("\x1b[24;{}~", modifier_code)),
224 "f13" => Some(format!("\x1b[25;{}~", modifier_code)),
225 "f14" => Some(format!("\x1b[26;{}~", modifier_code)),
226 "f15" => Some(format!("\x1b[28;{}~", modifier_code)),
227 "f16" => Some(format!("\x1b[29;{}~", modifier_code)),
228 "f17" => Some(format!("\x1b[31;{}~", modifier_code)),
229 "f18" => Some(format!("\x1b[32;{}~", modifier_code)),
230 "f19" => Some(format!("\x1b[33;{}~", modifier_code)),
231 "f20" => Some(format!("\x1b[34;{}~", modifier_code)),
232 _ if modifier_code == 2 => None,
233 "insert" => Some(format!("\x1b[2;{}~", modifier_code)),
234 "pageup" => Some(format!("\x1b[5;{}~", modifier_code)),
235 "pagedown" => Some(format!("\x1b[6;{}~", modifier_code)),
236 "end" => Some(format!("\x1b[1;{}F", modifier_code)),
237 "home" => Some(format!("\x1b[1;{}H", modifier_code)),
238 _ => None,
239 };
240 if modified_esc_str.is_some() {
241 return modified_esc_str;
242 }
243 }
244
245 None
246}
247
248/// Code Modifiers
249/// ---------+---------------------------
250/// 2 | Shift
251/// 3 | Alt
252/// 4 | Shift + Alt
253/// 5 | Control
254/// 6 | Shift + Control
255/// 7 | Alt + Control
256/// 8 | Shift + Alt + Control
257/// ---------+---------------------------
258/// from: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-PC-Style-Function-Keys
259fn modifier_code(keystroke: &Keystroke) -> u32 {
260 let mut modifier_code = 0;
261 if keystroke.shift {
262 modifier_code |= 1;
263 }
264 if keystroke.alt {
265 modifier_code |= 1 << 1;
266 }
267 if keystroke.ctrl {
268 modifier_code |= 1 << 2;
269 }
270 modifier_code + 1
271}
272
273#[cfg(test)]
274mod test {
275 use super::*;
276
277 #[test]
278 fn test_scroll_keys() {
279 //These keys should be handled by the scrolling element directly
280 //Need to signify this by returning 'None'
281 let shift_pageup = Keystroke::parse("shift-pageup").unwrap();
282 let shift_pagedown = Keystroke::parse("shift-pagedown").unwrap();
283 let shift_home = Keystroke::parse("shift-home").unwrap();
284 let shift_end = Keystroke::parse("shift-end").unwrap();
285
286 let none = TermMode::NONE;
287 assert_eq!(to_esc_str(&shift_pageup, &none), None);
288 assert_eq!(to_esc_str(&shift_pagedown, &none), None);
289 assert_eq!(to_esc_str(&shift_home, &none), None);
290 assert_eq!(to_esc_str(&shift_end, &none), None);
291
292 let alt_screen = TermMode::ALT_SCREEN;
293 assert_eq!(
294 to_esc_str(&shift_pageup, &alt_screen),
295 Some("\x1b[5;2~".to_string())
296 );
297 assert_eq!(
298 to_esc_str(&shift_pagedown, &alt_screen),
299 Some("\x1b[6;2~".to_string())
300 );
301 assert_eq!(
302 to_esc_str(&shift_home, &alt_screen),
303 Some("\x1b[1;2H".to_string())
304 );
305 assert_eq!(
306 to_esc_str(&shift_end, &alt_screen),
307 Some("\x1b[1;2F".to_string())
308 );
309
310 let pageup = Keystroke::parse("pageup").unwrap();
311 let pagedown = Keystroke::parse("pagedown").unwrap();
312 let any = TermMode::ANY;
313
314 assert_eq!(to_esc_str(&pageup, &any), Some("\x1b[5~".to_string()));
315 assert_eq!(to_esc_str(&pagedown, &any), Some("\x1b[6~".to_string()));
316 }
317
318 #[test]
319 fn test_plain_inputs() {
320 let ks = Keystroke {
321 ctrl: false,
322 alt: false,
323 shift: false,
324 cmd: false,
325 function: false,
326 key: "🖖🏻".to_string(), //2 char string
327 };
328 assert_eq!(to_esc_str(&ks, &TermMode::NONE), None);
329 }
330
331 #[test]
332 fn test_application_mode() {
333 let app_cursor = TermMode::APP_CURSOR;
334 let none = TermMode::NONE;
335
336 let up = Keystroke::parse("up").unwrap();
337 let down = Keystroke::parse("down").unwrap();
338 let left = Keystroke::parse("left").unwrap();
339 let right = Keystroke::parse("right").unwrap();
340
341 assert_eq!(to_esc_str(&up, &none), Some("\x1b[A".to_string()));
342 assert_eq!(to_esc_str(&down, &none), Some("\x1b[B".to_string()));
343 assert_eq!(to_esc_str(&right, &none), Some("\x1b[C".to_string()));
344 assert_eq!(to_esc_str(&left, &none), Some("\x1b[D".to_string()));
345
346 assert_eq!(to_esc_str(&up, &app_cursor), Some("\x1bOA".to_string()));
347 assert_eq!(to_esc_str(&down, &app_cursor), Some("\x1bOB".to_string()));
348 assert_eq!(to_esc_str(&right, &app_cursor), Some("\x1bOC".to_string()));
349 assert_eq!(to_esc_str(&left, &app_cursor), Some("\x1bOD".to_string()));
350 }
351
352 #[test]
353 fn test_ctrl_codes() {
354 let letters_lower = 'a'..='z';
355 let letters_upper = 'A'..='Z';
356 let mode = TermMode::ANY;
357
358 for (lower, upper) in letters_lower.zip(letters_upper) {
359 assert_eq!(
360 to_esc_str(
361 &Keystroke::parse(&format!("ctrl-{}", lower)).unwrap(),
362 &mode
363 ),
364 to_esc_str(
365 &Keystroke::parse(&format!("ctrl-shift-{}", upper)).unwrap(),
366 &mode
367 ),
368 "On letter: {}/{}",
369 lower,
370 upper
371 )
372 }
373 }
374
375 #[test]
376 fn test_modifier_code_calc() {
377 // Code Modifiers
378 // ---------+---------------------------
379 // 2 | Shift
380 // 3 | Alt
381 // 4 | Shift + Alt
382 // 5 | Control
383 // 6 | Shift + Control
384 // 7 | Alt + Control
385 // 8 | Shift + Alt + Control
386 // ---------+---------------------------
387 // from: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-PC-Style-Function-Keys
388 assert_eq!(2, modifier_code(&Keystroke::parse("shift-A").unwrap()));
389 assert_eq!(3, modifier_code(&Keystroke::parse("alt-A").unwrap()));
390 assert_eq!(4, modifier_code(&Keystroke::parse("shift-alt-A").unwrap()));
391 assert_eq!(5, modifier_code(&Keystroke::parse("ctrl-A").unwrap()));
392 assert_eq!(6, modifier_code(&Keystroke::parse("shift-ctrl-A").unwrap()));
393 assert_eq!(7, modifier_code(&Keystroke::parse("alt-ctrl-A").unwrap()));
394 assert_eq!(
395 8,
396 modifier_code(&Keystroke::parse("shift-ctrl-alt-A").unwrap())
397 );
398 }
399}