datafusion_rustyline/
consts.rs

1//! Key constants
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4pub enum KeyPress {
5    UnknownEscSeq,
6    Backspace,
7    Char(char),
8    ControlDown,
9    ControlLeft,
10    ControlRight,
11    ControlUp,
12    Ctrl(char),
13    Delete,
14    Down,
15    End,
16    Enter, // Ctrl('M')
17    Esc,
18    F(u8),
19    Home,
20    Insert,
21    Left,
22    Meta(char),
23    Null,
24    PageDown,
25    PageUp,
26    Right,
27    ShiftDown,
28    ShiftLeft,
29    ShiftRight,
30    ShiftUp,
31    Tab, // Ctrl('I')
32    Up,
33}
34
35#[allow(match_same_arms)]
36pub fn char_to_key_press(c: char) -> KeyPress {
37    if !c.is_control() {
38        return KeyPress::Char(c);
39    }
40    match c {
41        '\x00' => KeyPress::Ctrl(' '),
42        '\x01' => KeyPress::Ctrl('A'),
43        '\x02' => KeyPress::Ctrl('B'),
44        '\x03' => KeyPress::Ctrl('C'),
45        '\x04' => KeyPress::Ctrl('D'),
46        '\x05' => KeyPress::Ctrl('E'),
47        '\x06' => KeyPress::Ctrl('F'),
48        '\x07' => KeyPress::Ctrl('G'),
49        '\x08' => KeyPress::Backspace, // '\b'
50        '\x09' => KeyPress::Tab,       // '\t'
51        '\x0a' => KeyPress::Ctrl('J'), // '\n' (10)
52        '\x0b' => KeyPress::Ctrl('K'),
53        '\x0c' => KeyPress::Ctrl('L'),
54        '\x0d' => KeyPress::Enter, // '\r' (13)
55        '\x0e' => KeyPress::Ctrl('N'),
56        '\x0f' => KeyPress::Ctrl('O'),
57        '\x10' => KeyPress::Ctrl('P'),
58        '\x12' => KeyPress::Ctrl('R'),
59        '\x13' => KeyPress::Ctrl('S'),
60        '\x14' => KeyPress::Ctrl('T'),
61        '\x15' => KeyPress::Ctrl('U'),
62        '\x16' => KeyPress::Ctrl('V'),
63        '\x17' => KeyPress::Ctrl('W'),
64        '\x18' => KeyPress::Ctrl('X'),
65        '\x19' => KeyPress::Ctrl('Y'),
66        '\x1a' => KeyPress::Ctrl('Z'),
67        '\x1b' => KeyPress::Esc, // Ctrl-[
68        '\x1c' => KeyPress::Ctrl('\\'),
69        '\x1d' => KeyPress::Ctrl(']'),
70        '\x1e' => KeyPress::Ctrl('^'),
71        '\x1f' => KeyPress::Ctrl('_'),
72        '\x7f' => KeyPress::Backspace, // Rubout
73        _ => KeyPress::Null,
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::{char_to_key_press, KeyPress};
80
81    #[test]
82    fn char_to_key() {
83        assert_eq!(KeyPress::Esc, char_to_key_press('\x1b'));
84    }
85}