xref: /NovaShell/src/keycode.rs (revision 730e0306a1346ea7a86d0237bb024f4dab17e758)
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',
9001f2a75SMemoryShore     Delete = b'\x7f',
10001f2a75SMemoryShore     BackSpace = b'\x08',
11001f2a75SMemoryShore     Tab = b'\t',
12001f2a75SMemoryShore 
13*730e0306SLoGin     ESC = 0x1B,
14001f2a75SMemoryShore     PauseBreak = 0xE1,
15001f2a75SMemoryShore }
16001f2a75SMemoryShore 
17*730e0306SLoGin impl Into<u8> for SpecialKeycode {
18*730e0306SLoGin     fn into(self) -> u8 {
19*730e0306SLoGin         self as u8
20*730e0306SLoGin     }
21*730e0306SLoGin }
22*730e0306SLoGin 
23*730e0306SLoGin #[derive(Debug, PartialEq, Eq, Clone, Copy)]
24001f2a75SMemoryShore #[allow(dead_code)]
25001f2a75SMemoryShore pub enum FunctionKeySuffix {
26001f2a75SMemoryShore     Up = 0x48,
27001f2a75SMemoryShore     Down = 0x50,
28001f2a75SMemoryShore     Left = 0x4B,
29001f2a75SMemoryShore     Right = 0x4D,
30001f2a75SMemoryShore 
31001f2a75SMemoryShore     Home = 0x47,
32001f2a75SMemoryShore     End = 0x4F,
33001f2a75SMemoryShore }
34001f2a75SMemoryShore 
35*730e0306SLoGin impl FunctionKeySuffix {
36*730e0306SLoGin     pub const SUFFIX_0: u8 = 0x5b;
37*730e0306SLoGin     pub fn bytes(self) -> &'static [u8] {
38*730e0306SLoGin         match self {
39*730e0306SLoGin             FunctionKeySuffix::Up => &[0x5b, 0x41],
40*730e0306SLoGin             FunctionKeySuffix::Down => &[0x5b, 0x42],
41*730e0306SLoGin             FunctionKeySuffix::Left => &[0x5b, 0x44],
42*730e0306SLoGin             FunctionKeySuffix::Right => &[0x5b, 0x43],
43*730e0306SLoGin             FunctionKeySuffix::Home => &[0x5b, 0x48],
44*730e0306SLoGin             FunctionKeySuffix::End => &[0x5b, 0x46],
45*730e0306SLoGin         }
46*730e0306SLoGin     }
47*730e0306SLoGin 
48*730e0306SLoGin     pub fn try_from(value: &[u8]) -> Option<Self> {
49*730e0306SLoGin         match value {
50*730e0306SLoGin             [0x5b, 0x41] => Some(FunctionKeySuffix::Up),
51*730e0306SLoGin             [0x5b, 0x42] => Some(FunctionKeySuffix::Down),
52*730e0306SLoGin             [0x5b, 0x44] => Some(FunctionKeySuffix::Left),
53*730e0306SLoGin             [0x5b, 0x43] => Some(FunctionKeySuffix::Right),
54*730e0306SLoGin             [0x5b, 0x48] => Some(FunctionKeySuffix::Home),
55*730e0306SLoGin             [0x5b, 0x46] => Some(FunctionKeySuffix::End),
56*730e0306SLoGin             _ => None,
57*730e0306SLoGin         }
58*730e0306SLoGin     }
59*730e0306SLoGin }
60*730e0306SLoGin 
61*730e0306SLoGin impl Into<&[u8]> for FunctionKeySuffix {
62*730e0306SLoGin     fn into(self) -> &'static [u8] {
63*730e0306SLoGin         self.bytes()
64001f2a75SMemoryShore     }
65001f2a75SMemoryShore }
66