terminput 0.5.14

TUI input parser/encoder and abstraction over input backends
Documentation
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use core::hash::{Hash, Hasher};

use bitflags::bitflags;

use crate::Event;

/// A key input event.
#[derive(Debug, PartialOrd, Ord, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct KeyEvent {
    /// The key code.
    pub code: KeyCode,
    /// Key modifiers.
    pub modifiers: KeyModifiers,
    /// Type of key event - press, release, or repeat.
    pub kind: KeyEventKind,
    /// Other keyboard properties.
    pub state: KeyEventState,
}

impl KeyEvent {
    /// Creates a new [`KeyEvent`] with the default state and no modifiers.
    pub const fn new(code: KeyCode) -> Self {
        Self {
            code,
            modifiers: KeyModifiers::empty(),
            kind: KeyEventKind::Press,
            state: KeyEventState::empty(),
        }
    }

    /// Sets the [`KeyModifiers`].
    pub const fn modifiers(mut self, modifiers: KeyModifiers) -> Self {
        self.modifiers = modifiers;
        self
    }

    /// Sets the [`KeyEventKind`].
    pub const fn kind(mut self, kind: KeyEventKind) -> Self {
        self.kind = kind;
        self
    }

    /// Sets the [`KeyEventState`].
    pub const fn state(mut self, state: KeyEventState) -> Self {
        self.state = state;
        self
    }

    /// Normalizes the event so the shift modifier is applied appropriately.
    pub fn normalize_case(mut self) -> Self {
        let c = match self.code {
            KeyCode::Char(c) => c,
            _ => return self,
        };

        if c.is_ascii_uppercase() {
            self.modifiers.insert(KeyModifiers::SHIFT);
        } else if self.modifiers.contains(KeyModifiers::SHIFT) {
            self.code = KeyCode::Char(c.to_ascii_uppercase());
        }
        self
    }
}

impl PartialEq for KeyEvent {
    fn eq(&self, other: &Self) -> bool {
        let Self {
            code: lhs_code,
            modifiers: lhs_modifiers,
            kind: lhs_kind,
            state: lhs_state,
        } = self.normalize_case();
        let Self {
            code: rhs_code,
            modifiers: rhs_modifiers,
            kind: rhs_kind,
            state: rhs_state,
        } = other.normalize_case();
        (lhs_code == rhs_code)
            && (lhs_modifiers == rhs_modifiers)
            && (lhs_kind == rhs_kind)
            && (lhs_state == rhs_state)
    }
}

impl Eq for KeyEvent {}

impl Hash for KeyEvent {
    fn hash<H: Hasher>(&self, hash_state: &mut H) {
        let Self {
            code,
            modifiers,
            kind,
            state,
        } = self.normalize_case();
        code.hash(hash_state);
        modifiers.hash(hash_state);
        kind.hash(hash_state);
        state.hash(hash_state);
    }
}

/// Represents whether the modifier came from the left or right side of the keyboard, where
/// applicable.
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ModifierDirection {
    /// Modifier came from the left side of the keyboard.
    Left,
    /// Modifier came from the right side of the keyboard.
    Right,
    /// Direction is unknown or not applicable.
    Unknown,
}

/// Represents a key.
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum KeyCode {
    /// Backspace key.
    Backspace,
    /// Enter key.
    Enter,
    /// Left arrow key.
    Left,
    /// Right arrow key.
    Right,
    /// Up arrow key.
    Up,
    /// Down arrow key.
    Down,
    /// Home key.
    Home,
    /// End key.
    End,
    /// Page up key.
    PageUp,
    /// Page down key.
    PageDown,
    /// Tab key.
    Tab,
    /// Delete key.
    Delete,
    /// Insert key.
    Insert,
    /// F key.
    ///
    /// `KeyCode::F(1)` represents F1 key, etc.
    F(u8),
    /// A character.
    ///
    /// `KeyCode::Char('c')` represents `c` character, etc.
    Char(char),
    /// Escape key.
    Esc,
    /// Caps Lock key.
    CapsLock,
    /// Scroll Lock key.
    ScrollLock,
    /// Num Lock key.
    NumLock,
    /// Print Screen key.
    PrintScreen,
    /// Pause key.
    Pause,
    /// Menu key.
    Menu,
    /// The "Begin" key (often mapped to the 5 key when Num Lock is turned on).
    KeypadBegin,
    /// A media key.
    Media(MediaKeyCode),
    /// A modifier key.
    Modifier(ModifierKeyCode, ModifierDirection),
}

bitflags! {
    /// Represents key modifiers (shift, control, alt, etc.).
    #[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Hash)]
    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(transparent))]
    pub struct KeyModifiers: u8 {
        /// No modifiers.
        const NONE = 0;
        /// Key was pressed with shift.
        const SHIFT = 1;
        /// Key was pressed with alt.
        const ALT = 1<<1;
        /// Key was pressed with control.
        const CTRL = 1<<2;
        /// Key was pressed with super.
        const SUPER = 1<<3;
        /// Key was pressed with hyper.
        const HYPER = 1<<4;
        /// Key was pressed with meta.
        const META = 1<<5;
    }
}

/// Shift modifier.
pub const SHIFT: KeyModifiers = KeyModifiers::SHIFT;
/// Alt modifier.
pub const ALT: KeyModifiers = KeyModifiers::ALT;
/// Ctrl modifier.
pub const CTRL: KeyModifiers = KeyModifiers::CTRL;
/// Super modifier.
pub const SUPER: KeyModifiers = KeyModifiers::SUPER;
/// Hyper modifier.
pub const HYPER: KeyModifiers = KeyModifiers::HYPER;
/// Meta modifier.
pub const META: KeyModifiers = KeyModifiers::META;

/// Type of key event. Repeat and release events may not be emitted if the input source is not
/// configured to do so.
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum KeyEventKind {
    /// Key press.
    Press,
    /// Key repeat.
    Repeat,
    /// Key release.
    Release,
}

/// Media keys.
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum MediaKeyCode {
    /// Play media key.
    Play,
    /// Pause media key.
    Pause,
    /// Play/Pause media key.
    PlayPause,
    /// Reverse media key.
    Reverse,
    /// Stop media key.
    Stop,
    /// Fast-forward media key.
    FastForward,
    /// Rewind media key.
    Rewind,
    /// Next-track media key.
    TrackNext,
    /// Previous-track media key.
    TrackPrevious,
    /// Record media key.
    Record,
    /// Lower-volume media key.
    LowerVolume,
    /// Raise-volume media key.
    RaiseVolume,
    /// Mute media key.
    MuteVolume,
}

/// A modifier key event.
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ModifierKeyCode {
    /// Left Shift key.
    Shift,
    /// Left Control key.
    Control,
    /// Left Alt key.
    Alt,
    /// Left Super key.
    Super,
    /// Left Hyper key.
    Hyper,
    /// Left Meta key.
    Meta,
    /// Iso Level3 Shift key.
    IsoLevel3Shift,
    /// Iso Level5 Shift key.
    IsoLevel5Shift,
}

bitflags! {
    /// Represents extra state about the key event.
    #[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Hash)]
    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(transparent))]
    pub struct KeyEventState: u8 {
        /// No extra state applicable.
        const NONE = 0;
        /// The key event came from the keypad.
        const KEYPAD = 1;
        /// Caps Lock was enabled for this key event.
        const CAPS_LOCK = 1<<1;
        /// Num Lock was enabled for this key event.
        const NUM_LOCK = 1<<2;

    }
}

/// Keypad event state.
pub const KEYPAD: KeyEventState = KeyEventState::KEYPAD;
/// Caps lock event state.
pub const CAPS_LOCK: KeyEventState = KeyEventState::CAPS_LOCK;
/// Num lock event state.
pub const NUM_LOCK: KeyEventState = KeyEventState::NUM_LOCK;

impl From<KeyCode> for KeyEvent {
    fn from(code: KeyCode) -> Self {
        Self {
            code,
            modifiers: KeyModifiers::empty(),
            kind: KeyEventKind::Press,
            state: KeyEventState::empty(),
        }
    }
}

impl From<KeyEvent> for Event {
    fn from(value: KeyEvent) -> Self {
        Self::Key(value)
    }
}

/// Pattern matching for key events.
///
/// There are three forms:
/// - `key!(keyCode)`
/// - `key!(modifiers, keyCode)`
/// - `key!(eventState, modifiers, keyCode)`
///
/// If `modifiers` is omitted, only events with no modifiers are matched.
///
/// If `eventState` is omitted, any event state will be matched.
///
/// # Example
///
/// ```
/// use terminput::KeyCode::*;
/// use terminput::{ALT, CTRL, Event, KeyModifiers, Repeats, key, modifiers};
///
/// fn handle_event(event: Event) {
///     if let Some(key_event) = event.as_key_press(Repeats::Include) {
///         const CTRL_ALT: KeyModifiers = modifiers!(CTRL, ALT);
///
///         match key_event {
///             key!(Char('a')) => {
///                 println!("'a' pressed");
///             }
///             key!(CTRL | ALT, Char('d')) => {
///                 println!("'ctrl+d' or 'alt+d' pressed");
///             }
///             key!(CTRL_ALT, Char('c')) => {
///                 println!("'ctrl+alt+c' pressed");
///             }
///             key!(CAPS_LOCK, KeyModifiers::NONE, Char('d')) => {
///                 println!("'d' pressed with caps lock");
///             }
///             _ => {}
///         }
///     }
/// }
/// ```
#[macro_export]
macro_rules! key {
    ($state:pat, $mods:pat, $code:pat) => {
        $crate::KeyEvent {
            code: $code,
            modifiers: $mods,
            state: $state,
            ..
        }
    };
    ($mods:pat, $code:pat) => {
        $crate::KeyEvent {
            code: $code,
            modifiers: $mods,
            ..
        }
    };
    ($code:pat) => {
        $crate::KeyEvent {
            code: $code,
            modifiers: $crate::KeyModifiers::NONE,
            ..
        }
    };
}

/// Macro for combining key modifiers in a way that's valid in `const` contexts. Useful for match
/// expressions.
///
/// # Example
///
/// ```
/// use terminput::{ALT, CTRL, KeyModifiers, modifiers};
///
/// const mods: KeyModifiers = modifiers!(CTRL, ALT);
/// assert_eq!(mods, CTRL | ALT);
/// ```
#[macro_export]
macro_rules! modifiers {
    ($($key_mod:tt),+) => {
        $crate::KeyModifiers::from_bits_retain($($key_mod.bits())|+)
    };
}

/// Macro for combining key states together in a way that's valid in `const` contexts. Useful for
/// match expressions.
///
/// # Example
///
/// ```
/// use terminput::{CAPS_LOCK, KeyEventState, NUM_LOCK, states};
///
/// const key_state: KeyEventState = states!(CAPS_LOCK, NUM_LOCK);
/// assert_eq!(key_state, CAPS_LOCK | NUM_LOCK);
/// ```
#[macro_export]
macro_rules! states {
    ($($state:tt),+) => {
        $crate::KeyEventState::from_bits_retain($($state.bits())|+)
    };
}