xref: /NovaShell/src/keycode.rs (revision 730e0306a1346ea7a86d0237bb024f4dab17e758)
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     Delete = b'\x7f',
10     BackSpace = b'\x08',
11     Tab = b'\t',
12 
13     ESC = 0x1B,
14     PauseBreak = 0xE1,
15 }
16 
17 impl Into<u8> for SpecialKeycode {
18     fn into(self) -> u8 {
19         self as u8
20     }
21 }
22 
23 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
24 #[allow(dead_code)]
25 pub enum FunctionKeySuffix {
26     Up = 0x48,
27     Down = 0x50,
28     Left = 0x4B,
29     Right = 0x4D,
30 
31     Home = 0x47,
32     End = 0x4F,
33 }
34 
35 impl FunctionKeySuffix {
36     pub const SUFFIX_0: u8 = 0x5b;
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         }
46     }
47 
48     pub fn try_from(value: &[u8]) -> Option<Self> {
49         match value {
50             [0x5b, 0x41] => Some(FunctionKeySuffix::Up),
51             [0x5b, 0x42] => Some(FunctionKeySuffix::Down),
52             [0x5b, 0x44] => Some(FunctionKeySuffix::Left),
53             [0x5b, 0x43] => Some(FunctionKeySuffix::Right),
54             [0x5b, 0x48] => Some(FunctionKeySuffix::Home),
55             [0x5b, 0x46] => Some(FunctionKeySuffix::End),
56             _ => None,
57         }
58     }
59 }
60 
61 impl Into<&[u8]> for FunctionKeySuffix {
62     fn into(self) -> &'static [u8] {
63         self.bytes()
64     }
65 }
66