1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#[cfg(feature = "arbitrary")]
use arbitrary::Arbitrary;
#[cfg(feature = "crossterm")]
use crossterm::event::{
Event as CrosstermEvent, KeyCode, KeyEvent, KeyModifiers, MouseEvent as CrosstermMouseEvent,
MouseEventKind as CrosstermMouseEventKind,
};
#[cfg(feature = "termion")]
use termion::event::{Event as TermionEvent, Key as TermionKey, MouseEvent as TermionMouseEvent};
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
pub enum Key {
Char(char),
F(u8),
Backspace,
Enter,
Left,
Right,
Up,
Down,
Tab,
Delete,
Home,
End,
PageUp,
PageDown,
Esc,
MouseScrollDown,
MouseScrollUp,
Null,
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
pub struct Input {
pub key: Key,
pub ctrl: bool,
pub alt: bool,
}
impl Default for Input {
fn default() -> Self {
Input {
key: Key::Null,
ctrl: false,
alt: false,
}
}
}
#[cfg(feature = "crossterm")]
impl From<CrosstermEvent> for Input {
fn from(event: CrosstermEvent) -> Self {
match event {
CrosstermEvent::Key(key) => Self::from(key),
CrosstermEvent::Mouse(mouse) => Self::from(mouse),
_ => Self::default(),
}
}
}
#[cfg(feature = "crossterm")]
impl From<KeyEvent> for Input {
fn from(key: KeyEvent) -> Self {
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
let alt = key.modifiers.contains(KeyModifiers::ALT);
let key = match key.code {
KeyCode::Char(c) => Key::Char(c),
KeyCode::Backspace => Key::Backspace,
KeyCode::Enter => Key::Enter,
KeyCode::Left => Key::Left,
KeyCode::Right => Key::Right,
KeyCode::Up => Key::Up,
KeyCode::Down => Key::Down,
KeyCode::Tab => Key::Tab,
KeyCode::Delete => Key::Delete,
KeyCode::Home => Key::Home,
KeyCode::End => Key::End,
KeyCode::PageUp => Key::PageUp,
KeyCode::PageDown => Key::PageDown,
KeyCode::Esc => Key::Esc,
KeyCode::F(x) => Key::F(x),
_ => Key::Null,
};
Self { key, ctrl, alt }
}
}
#[cfg(feature = "crossterm")]
impl From<CrosstermMouseEvent> for Input {
fn from(mouse: CrosstermMouseEvent) -> Self {
let key = match mouse.kind {
CrosstermMouseEventKind::ScrollDown => Key::MouseScrollDown,
CrosstermMouseEventKind::ScrollUp => Key::MouseScrollUp,
_ => return Self::default(),
};
let ctrl = mouse.modifiers.contains(KeyModifiers::CONTROL);
let alt = mouse.modifiers.contains(KeyModifiers::ALT);
Self { key, ctrl, alt }
}
}
#[cfg(feature = "termion")]
impl From<TermionEvent> for Input {
fn from(event: TermionEvent) -> Self {
match event {
TermionEvent::Key(key) => Self::from(key),
TermionEvent::Mouse(mouse) => Self::from(mouse),
_ => Self::default(),
}
}
}
#[cfg(feature = "termion")]
impl From<TermionKey> for Input {
fn from(key: TermionKey) -> Self {
use TermionKey::*;
let mut ctrl = false;
let mut alt = false;
let key = match key {
Char('\n' | '\r') => Key::Enter,
Char(c) => Key::Char(c),
Ctrl(c) => {
ctrl = true;
Key::Char(c)
}
Alt(c) => {
alt = true;
Key::Char(c)
}
Backspace => Key::Backspace,
Left => Key::Left,
Right => Key::Right,
Up => Key::Up,
Down => Key::Down,
Home => Key::Home,
End => Key::End,
PageUp => Key::PageUp,
PageDown => Key::PageDown,
BackTab => Key::Tab,
Delete => Key::Delete,
Esc => Key::Esc,
F(x) => Key::F(x),
_ => Key::Null,
};
Input { key, ctrl, alt }
}
}
#[cfg(feature = "termion")]
impl From<TermionMouseEvent> for Input {
fn from(mouse: TermionMouseEvent) -> Self {
use termion::event::MouseButton;
let key = match mouse {
TermionMouseEvent::Press(MouseButton::WheelUp, ..) => Key::MouseScrollUp,
TermionMouseEvent::Press(MouseButton::WheelDown, ..) => Key::MouseScrollDown,
_ => return Self::default(),
};
Self {
key,
ctrl: false,
alt: false,
}
}
}