xref: /NovaShell/src/keycode.rs (revision 6c1ca14da718ee67c3dc1e2d2ffdef3d0115fad6)
1 use num_enum::TryFromPrimitive;
2 
3 #[repr(u8)]
4 #[derive(Debug, FromPrimitive, TryFromPrimitive, ToPrimitive, PartialEq, Eq, Clone)]
5 #[allow(dead_code)]
6 pub enum SpecialKeycode {
7     LF = b'\n',
8     CR = b'\r',
9     BackSpace = b'\x7f',
10     Tab = b'\t',
11 
12     ESC = 0x1B,
13     PauseBreak = 0xE1,
14 }
15 
16 impl Into<u8> for SpecialKeycode {
into(self) -> u817     fn into(self) -> u8 {
18         self as u8
19     }
20 }
21 
22 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
23 #[allow(dead_code)]
24 pub enum FunctionKeySuffix {
25     Up,
26     Down,
27     Left,
28     Right,
29 
30     Home,
31     End,
32     Delete,
33 }
34 
35 impl FunctionKeySuffix {
36     pub const SUFFIX_0: u8 = 0x5b;
bytes(self) -> &'static [u8]37     pub fn bytes(self) -> &'static [u8] {
38         match self {
39             FunctionKeySuffix::Up => &[0x5b, 0x41],
40             FunctionKeySuffix::Down => &[0x5b, 0x42],
41             FunctionKeySuffix::Left => &[0x5b, 0x44],
42             FunctionKeySuffix::Right => &[0x5b, 0x43],
43             FunctionKeySuffix::Home => &[0x5b, 0x48],
44             FunctionKeySuffix::End => &[0x5b, 0x46],
45             FunctionKeySuffix::Delete => &[0x5b, 0x33, 0x7e],
46         }
47     }
48 
try_from(value: &[u8]) -> Option<Self>49     pub fn try_from(value: &[u8]) -> Option<Self> {
50         match value {
51             [0x5b, 0x41] => Some(FunctionKeySuffix::Up),
52             [0x5b, 0x42] => Some(FunctionKeySuffix::Down),
53             [0x5b, 0x44] => Some(FunctionKeySuffix::Left),
54             [0x5b, 0x43] => Some(FunctionKeySuffix::Right),
55             [0x5b, 0x48] => Some(FunctionKeySuffix::Home),
56             [0x5b, 0x46] => Some(FunctionKeySuffix::End),
57             [0x5b, 0x33, 0x7e] => Some(FunctionKeySuffix::Delete),
58             _ => None,
59         }
60     }
61 
should_read_more(value: &[u8]) -> bool62     pub fn should_read_more(value: &[u8]) -> bool {
63         match value.len() {
64             0 => true,
65             1 => value[0] == Self::SUFFIX_0,
66             2 => value[0] == Self::SUFFIX_0 && value[1] == 0x33,
67             _ => false,
68         }
69     }
70 }
71 
72 impl Into<&[u8]> for FunctionKeySuffix {
into(self) -> &'static [u8]73     fn into(self) -> &'static [u8] {
74         self.bytes()
75     }
76 }
77