ratatui_eventInput/
lib.rs

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
#[cfg(feature = "crossterm")]
mod crossterm;

#[cfg(feature = "termion")]
mod termion;

#[cfg(feature = "termwiz")]
mod termwiz;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Input {
    pub key: Key,
    pub modifier: Modifier,
}

impl Input {
    pub fn new(key: Key, modifier: Modifier) -> Input {
        Input { key, modifier }
    }
    pub fn new_key(key: Key) -> Input {
        Input {
            key,
            modifier: Modifier::None,
        }
    }
    pub fn keys(keys: &[Key]) -> Vec<Input> {
        keys.iter().map(|key| Self::new_key(*key)).collect()
    }
    pub fn with_key(&mut self, key: Key) -> &mut Self {
        self.key = key;
        self
    }
    pub fn with_modifier(&mut self, modifier: Modifier) -> &mut Self {
        self.modifier = modifier;
        self
    }
}

impl Default for Input {
    fn default() -> Self {
        Self {
            key: Key::None,
            modifier: Modifier::None,
        }
    }
}

/// Input enum to represent all keys you may get
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Key {
    Up,
    Down,
    Left,
    Right,
    /// Character key
    /// like `a` key, `h` key
    Char(char),
    Esc,
    Backspace,
    Enter,
    Tab,
    CapsLock,
    // home island
    Home,
    End,
    PageUp,
    PageDown,
    Delete,
    Insert,
    /// Function keys
    Func(u8),
    ScrollLock,
    NumLock,
    PrintScreen,
    Pause,
    Menu,
    BackTab,
    // media
    MediaPlay,
    MediaPause,
    MediaPlayPause,
    MediaStop,
    MediaNext,
    MediaPrevious,
    RaisVolume,
    LowerVolume,
    MuteVolume,
    /// If a modifier is pressed on it's own
    Modifier(Modifier),
    /// Nothing was pressed
    /// Or it was not implemented
    None,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Modifier {
    Shift(Side),
    Control(Side),
    Alt(Side),
    /// Command on macOS, Windows on Windows, Super on other platforms
    Super(Side),
    Meta(Side),
    Hyper(Side),
    /// No modifier
    None,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
/// If there is no side reported; Left will used as default
pub enum Side {
    Left,
    Right,
}