xref: /Held/src/utils/input.rs (revision 984a200e159c143eb730a2df362731fe1a62fe01)
1*984a200eSGnoCiYeH use std::io::{self, Read};
2*984a200eSGnoCiYeH 
3*984a200eSGnoCiYeH pub struct Input;
4*984a200eSGnoCiYeH 
5*984a200eSGnoCiYeH impl Input {
6*984a200eSGnoCiYeH     pub fn wait_keydown() -> io::Result<KeyEventType> {
7*984a200eSGnoCiYeH         let buf: &mut [u8] = &mut [0; 8];
8*984a200eSGnoCiYeH         let count = io::stdin().read(buf)?;
9*984a200eSGnoCiYeH         Ok(KeyCodeParser::parse(&buf[0..count]))
10*984a200eSGnoCiYeH     }
11*984a200eSGnoCiYeH }
12*984a200eSGnoCiYeH 
13*984a200eSGnoCiYeH struct KeyCodeParser;
14*984a200eSGnoCiYeH 
15*984a200eSGnoCiYeH impl KeyCodeParser {
16*984a200eSGnoCiYeH     pub fn parse(bytes: &[u8]) -> KeyEventType {
17*984a200eSGnoCiYeH         if bytes[0] == 224 {
18*984a200eSGnoCiYeH             // 控制字符
19*984a200eSGnoCiYeH             return Self::parse_ctrl(&bytes[1..]);
20*984a200eSGnoCiYeH         }
21*984a200eSGnoCiYeH         match bytes {
22*984a200eSGnoCiYeH             // Enter key
23*984a200eSGnoCiYeH             b"\n" => KeyEventType::Enter,
24*984a200eSGnoCiYeH             // Tab key
25*984a200eSGnoCiYeH             b"\t" => KeyEventType::Tab,
26*984a200eSGnoCiYeH             // Esc
27*984a200eSGnoCiYeH             [0] => KeyEventType::Esc,
28*984a200eSGnoCiYeH 
29*984a200eSGnoCiYeH             [8] => KeyEventType::Backspace,
30*984a200eSGnoCiYeH 
31*984a200eSGnoCiYeH             // ASCII 字符
32*984a200eSGnoCiYeH             [byte] if *byte >= 32 && *byte <= 126 => KeyEventType::Common(bytes[0]),
33*984a200eSGnoCiYeH             // Unknown bytes
34*984a200eSGnoCiYeH             bytes => {
35*984a200eSGnoCiYeH                 error!("unknown bytes {bytes:?}");
36*984a200eSGnoCiYeH                 KeyEventType::Unknown(bytes.to_vec())
37*984a200eSGnoCiYeH             }
38*984a200eSGnoCiYeH         }
39*984a200eSGnoCiYeH     }
40*984a200eSGnoCiYeH 
41*984a200eSGnoCiYeH     fn parse_ctrl(bytes: &[u8]) -> KeyEventType {
42*984a200eSGnoCiYeH         match bytes {
43*984a200eSGnoCiYeH             [72] => KeyEventType::Up,
44*984a200eSGnoCiYeH             [80] => KeyEventType::Down,
45*984a200eSGnoCiYeH             [75] => KeyEventType::Left,
46*984a200eSGnoCiYeH             [77] => KeyEventType::Right,
47*984a200eSGnoCiYeH             bytes => {
48*984a200eSGnoCiYeH                 error!("unknown ctrl bytes {bytes:?}");
49*984a200eSGnoCiYeH                 KeyEventType::Unknown(bytes.to_vec())
50*984a200eSGnoCiYeH             }
51*984a200eSGnoCiYeH         }
52*984a200eSGnoCiYeH     }
53*984a200eSGnoCiYeH }
54*984a200eSGnoCiYeH 
55*984a200eSGnoCiYeH #[derive(Debug, Clone)]
56*984a200eSGnoCiYeH pub enum KeyEventType {
57*984a200eSGnoCiYeH     Common(u8),
58*984a200eSGnoCiYeH 
59*984a200eSGnoCiYeH     Up,
60*984a200eSGnoCiYeH     Down,
61*984a200eSGnoCiYeH     Right,
62*984a200eSGnoCiYeH     Left,
63*984a200eSGnoCiYeH 
64*984a200eSGnoCiYeH     Enter,
65*984a200eSGnoCiYeH     Tab,
66*984a200eSGnoCiYeH     Backspace,
67*984a200eSGnoCiYeH 
68*984a200eSGnoCiYeH     Esc,
69*984a200eSGnoCiYeH 
70*984a200eSGnoCiYeH     Unknown(Vec<u8>),
71*984a200eSGnoCiYeH }
72