1use crossterm::event::{KeyCode, KeyEvent as CTermKeyEvent, KeyModifiers};
2
3#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4pub enum KeyEvent {
5 Paste(String),
6 Char(char),
7 Ctrl(char),
8 Alt(char),
9 Shift(char),
10 CtrlAlt(char),
11 CtrlShift(char),
12 AltShift(char),
13 CtrlAltShift(char),
14 AltEnter,
15 CtrlEnter,
16 ShiftEnter,
17 Enter,
18 Tab,
19 BackTab,
20 Backspace,
21 CtrlBackspace,
22 Delete,
23 Escape,
24 Left,
25 Right,
26 Up,
27 Down,
28 Home,
29 End,
30 PageUp,
31 PageDown,
32 F(u8),
33 None,
34}
35
36impl From<CTermKeyEvent> for KeyEvent {
37 fn from(ev: CTermKeyEvent) -> Self {
38 let ctrl = ev.modifiers.contains(KeyModifiers::CONTROL);
39 let alt = ev.modifiers.contains(KeyModifiers::ALT);
40 let shift = ev.modifiers.contains(KeyModifiers::SHIFT);
41 match ev.code {
42 KeyCode::Char(c) => {
43 if ctrl && alt && shift {
44 return KeyEvent::CtrlAltShift(c);
45 } else if ctrl && alt {
46 return KeyEvent::CtrlAlt(c);
47 } else if ctrl && shift {
48 return KeyEvent::CtrlShift(c);
49 } else if alt && shift {
50 return KeyEvent::AltShift(c);
51 } else if ctrl && !alt && !shift {
52 return KeyEvent::Ctrl(c);
56 } else if alt && !ctrl {
58 return KeyEvent::Alt(c);
59 } else if shift && !ctrl && !alt {
60 return KeyEvent::Char(shift_char(
61 c,
62 ev.state == crossterm::event::KeyEventState::CAPS_LOCK,
63 ));
64 }
66 if ev.state == crossterm::event::KeyEventState::CAPS_LOCK {
72 KeyEvent::Char(c.to_ascii_uppercase())
73 } else {
74 KeyEvent::Char(c)
75 }
76 }
77 KeyCode::Enter => {
79 if alt {
80 KeyEvent::AltEnter
81 } else if ctrl {
82 KeyEvent::CtrlEnter
83 } else if shift {
84 KeyEvent::ShiftEnter
85 } else {
87 KeyEvent::Enter
88 }
89 }
90 KeyCode::Tab => {
91 if shift {
92 KeyEvent::BackTab
93 } else {
94 KeyEvent::Tab
95 }
96 }
97 KeyCode::BackTab => KeyEvent::BackTab,
98 KeyCode::Backspace => {
99 if ctrl {
100 KeyEvent::CtrlBackspace
101 } else {
102 KeyEvent::Backspace
103 }
104 }
105 KeyCode::Delete => KeyEvent::Delete,
106 KeyCode::Esc => KeyEvent::Escape,
107 KeyCode::Left => KeyEvent::Left,
108 KeyCode::Right => KeyEvent::Right,
109 KeyCode::Up => KeyEvent::Up,
110 KeyCode::Down => KeyEvent::Down,
111 KeyCode::Home => KeyEvent::Home,
112 KeyCode::End => KeyEvent::End,
113 KeyCode::PageUp => KeyEvent::PageUp,
114 KeyCode::PageDown => KeyEvent::PageDown,
115 KeyCode::F(n) => KeyEvent::F(n),
116 KeyCode::Modifier(_) => KeyEvent::None,
117 KeyCode::Media(_) => KeyEvent::None,
118 _ => KeyEvent::Escape,
119 }
120 }
121}
122
123#[derive(Debug, Clone)]
124pub enum Cmd {
125 Insert(char),
126 InsertStr(String),
127 InsertStrAtBeginning(String),
128 Backspace,
129 Delete,
130 MoveLeft,
131 MoveRight,
132 MoveUp,
133 MoveDown,
134 MoveToStart,
135 MoveToEnd,
136 MoveWordLeft,
137 MoveWordRight,
138 DeleteWordBefore,
139 DeleteWordAfter,
140 DeleteToStart,
141 DeleteToEnd,
142 DeleteToLineStart,
143 DeleteToLineEnd,
144 AcceptLine,
145 Cancel,
146 Complete,
147 HistoryPrevious,
148 HistoryNext,
149 HistorySearch,
150 Yank,
151 TransposeChars,
152 ClearScreen,
153 AcceptHint,
154 AcceptHintWord,
155 ClearBuffer,
156 ToggleSudo(String),
157 Noop,
158}
159
160#[inline]
161pub fn shift_char(c: char, is_cap: bool) -> char {
162 match c {
163 '1' => '!',
164 '2' => '@',
165 '3' => '#',
166 '4' => '$',
167 '5' => '%',
168 '6' => '^',
169 '7' => '&',
170 '8' => '*',
171 '9' => '(',
172 '0' => ')',
173 '-' => '_',
174 '=' => '+',
175 '[' => '{',
176 ']' => '}',
177 '\\' => '|',
178 ';' => ':',
179 '\'' => '"',
180 ',' => '<',
181 '.' => '>',
182 '/' => '?',
183 '`' => '~',
184 c if is_cap => c,
185 c => c.to_ascii_uppercase(),
186 }
187}