xref: /NovaShell/src/keycode.rs (revision 6c1ca14da718ee67c3dc1e2d2ffdef3d0115fad6)
1001f2a75SMemoryShore use num_enum::TryFromPrimitive;
2001f2a75SMemoryShore 
3001f2a75SMemoryShore #[repr(u8)]
4001f2a75SMemoryShore #[derive(Debug, FromPrimitive, TryFromPrimitive, ToPrimitive, PartialEq, Eq, Clone)]
5001f2a75SMemoryShore #[allow(dead_code)]
6001f2a75SMemoryShore pub enum SpecialKeycode {
7001f2a75SMemoryShore     LF = b'\n',
8001f2a75SMemoryShore     CR = b'\r',
9*6c1ca14dSLoGin     BackSpace = b'\x7f',
10001f2a75SMemoryShore     Tab = b'\t',
11001f2a75SMemoryShore 
12730e0306SLoGin     ESC = 0x1B,
13001f2a75SMemoryShore     PauseBreak = 0xE1,
14001f2a75SMemoryShore }
15001f2a75SMemoryShore 
16730e0306SLoGin impl Into<u8> for SpecialKeycode {
into(self) -> u817730e0306SLoGin     fn into(self) -> u8 {
18730e0306SLoGin         self as u8
19730e0306SLoGin     }
20730e0306SLoGin }
21730e0306SLoGin 
22730e0306SLoGin #[derive(Debug, PartialEq, Eq, Clone, Copy)]
23001f2a75SMemoryShore #[allow(dead_code)]
24001f2a75SMemoryShore pub enum FunctionKeySuffix {
25*6c1ca14dSLoGin     Up,
26*6c1ca14dSLoGin     Down,
27*6c1ca14dSLoGin     Left,
28*6c1ca14dSLoGin     Right,
29001f2a75SMemoryShore 
30*6c1ca14dSLoGin     Home,
31*6c1ca14dSLoGin     End,
32*6c1ca14dSLoGin     Delete,
33001f2a75SMemoryShore }
34001f2a75SMemoryShore 
35730e0306SLoGin impl FunctionKeySuffix {
36730e0306SLoGin     pub const SUFFIX_0: u8 = 0x5b;
bytes(self) -> &'static [u8]37730e0306SLoGin     pub fn bytes(self) -> &'static [u8] {
38730e0306SLoGin         match self {
39730e0306SLoGin             FunctionKeySuffix::Up => &[0x5b, 0x41],
40730e0306SLoGin             FunctionKeySuffix::Down => &[0x5b, 0x42],
41730e0306SLoGin             FunctionKeySuffix::Left => &[0x5b, 0x44],
42730e0306SLoGin             FunctionKeySuffix::Right => &[0x5b, 0x43],
43730e0306SLoGin             FunctionKeySuffix::Home => &[0x5b, 0x48],
44730e0306SLoGin             FunctionKeySuffix::End => &[0x5b, 0x46],
45*6c1ca14dSLoGin             FunctionKeySuffix::Delete => &[0x5b, 0x33, 0x7e],
46730e0306SLoGin         }
47730e0306SLoGin     }
48730e0306SLoGin 
try_from(value: &[u8]) -> Option<Self>49730e0306SLoGin     pub fn try_from(value: &[u8]) -> Option<Self> {
50730e0306SLoGin         match value {
51730e0306SLoGin             [0x5b, 0x41] => Some(FunctionKeySuffix::Up),
52730e0306SLoGin             [0x5b, 0x42] => Some(FunctionKeySuffix::Down),
53730e0306SLoGin             [0x5b, 0x44] => Some(FunctionKeySuffix::Left),
54730e0306SLoGin             [0x5b, 0x43] => Some(FunctionKeySuffix::Right),
55730e0306SLoGin             [0x5b, 0x48] => Some(FunctionKeySuffix::Home),
56730e0306SLoGin             [0x5b, 0x46] => Some(FunctionKeySuffix::End),
57*6c1ca14dSLoGin             [0x5b, 0x33, 0x7e] => Some(FunctionKeySuffix::Delete),
58730e0306SLoGin             _ => None,
59730e0306SLoGin         }
60730e0306SLoGin     }
61*6c1ca14dSLoGin 
should_read_more(value: &[u8]) -> bool62*6c1ca14dSLoGin     pub fn should_read_more(value: &[u8]) -> bool {
63*6c1ca14dSLoGin         match value.len() {
64*6c1ca14dSLoGin             0 => true,
65*6c1ca14dSLoGin             1 => value[0] == Self::SUFFIX_0,
66*6c1ca14dSLoGin             2 => value[0] == Self::SUFFIX_0 && value[1] == 0x33,
67*6c1ca14dSLoGin             _ => false,
68*6c1ca14dSLoGin         }
69*6c1ca14dSLoGin     }
70730e0306SLoGin }
71730e0306SLoGin 
72730e0306SLoGin impl Into<&[u8]> for FunctionKeySuffix {
into(self) -> &'static [u8]73730e0306SLoGin     fn into(self) -> &'static [u8] {
74730e0306SLoGin         self.bytes()
75001f2a75SMemoryShore     }
76001f2a75SMemoryShore }
77