tuirealm 4.0.0

A tui-rs framework to build tui interfaces, inspired by React and Elm.
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! `events` contains all the types related to [`Event`].

use bitflags::bitflags;
#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};

// -- event

/// An event raised by a port.
#[derive(Debug, Eq, PartialEq, Clone, PartialOrd)]
pub enum Event<UserEvent>
where
    UserEvent: Eq + PartialEq + Clone,
{
    /// A keyboard event
    Keyboard(KeyEvent),
    /// A Mouse event
    Mouse(MouseEvent),
    /// This event is raised after the terminal window is resized.
    ///
    /// Contains (width, height).
    WindowResize(u16, u16),
    /// Window focus gained
    FocusGained,
    /// Window focus lost
    FocusLost,
    /// Clipboard content pasted
    Paste(String),
    /// A ui tick event (should be configurable)
    Tick,
    /// Unhandled event; Empty event
    None,
    /// User event; won't be used by standard library or by default input event listener;
    /// but can be used in user defined ports
    User(UserEvent),
}

impl<UserEvent> Event<UserEvent>
where
    UserEvent: Eq + PartialEq + Clone,
{
    /// Get the inner [`Keyboard`](Self::Keyboard) event as `Some`, otherwise `None`.
    pub fn as_keyboard(&self) -> Option<&KeyEvent> {
        if let Event::Keyboard(k) = self {
            Some(k)
        } else {
            None
        }
    }

    /// Get the inner [`Mouse`](Self::Mouse) event as `Some`, otherwise `None`.
    pub fn as_mouse(&self) -> Option<&MouseEvent> {
        if let Event::Mouse(m) = self {
            Some(m)
        } else {
            None
        }
    }

    pub(crate) fn as_window_resize(&self) -> bool {
        matches!(self, Self::WindowResize(_, _))
    }

    pub(crate) fn as_tick(&self) -> bool {
        matches!(self, Self::Tick)
    }

    /// Get the inner [`User`](Self::User) event as `Some`, otherwise `None`.
    pub fn as_user(&self) -> Option<&UserEvent> {
        if let Event::User(u) = self {
            Some(u)
        } else {
            None
        }
    }
}

/// When using [`Event`], but dont need custom `UserEvent`s, you can use this as type parameter instead of defining a custom empty type.
#[derive(Debug, Eq, PartialEq, Copy, Clone, PartialOrd)]
pub enum NoUserEvent {}

// -- keyboard

/// A keyboard event
#[derive(Debug, Eq, PartialEq, Copy, Clone, PartialOrd, Hash)]
#[cfg_attr(
    feature = "serialize",
    derive(Deserialize, Serialize),
    serde(tag = "type")
)]
pub struct KeyEvent {
    pub code: Key,
    pub modifiers: KeyModifiers,
}

/// Defines the possible Keyboard keys.
#[derive(Debug, Eq, PartialEq, Copy, Clone, PartialOrd, Hash)]
#[cfg_attr(
    feature = "serialize",
    derive(Deserialize, Serialize),
    serde(tag = "type", content = "args")
)]
pub enum Key {
    /// 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 dow key.
    PageDown,
    /// Tab key.
    Tab,
    /// Shift + Tab key. (sugar)
    BackTab,
    /// Delete key.
    Delete,
    /// Insert key.
    Insert,
    /// Function key followed by index (F1 => `Key::Function(1)`)
    Function(u8),
    /// A character.
    ///
    /// `KeyCode::Char('c')` represents `c` character, etc.
    Char(char),
    /// Null.
    Null,
    /// Caps lock pressed
    CapsLock,
    /// Scroll lock pressed
    ScrollLock,
    /// Num lock pressed
    NumLock,
    /// Print screen key
    PrintScreen,
    /// Pause key
    Pause,
    /// Menu key
    Menu,
    /// keypad begin
    KeypadBegin,
    /// Media key
    Media(MediaKeyCode),
    /// Escape key.
    Esc,

    /// Shift left
    ShiftLeft,
    /// Alt left; warning: it is supported only on termion
    AltLeft,
    /// warning: it is supported only on termion
    CtrlLeft,
    /// warning: it is supported only on termion
    ShiftRight,
    /// warning: it is supported only on termion
    AltRight,
    /// warning: it is supported only on termion
    CtrlRight,
    /// warning: it is supported only on termion
    ShiftUp,
    /// warning: it is supported only on termion
    AltUp,
    /// warning: it is supported only on termion
    CtrlUp,
    /// warning: it is supported only on termion
    ShiftDown,
    /// warning: it is supported only on termion
    AltDown,
    /// warning: it is supported only on termion
    CtrlDown,
    /// warning: it is supported only on termion
    CtrlHome,
    /// warning: it is supported only on termion
    CtrlEnd,
}

/// Defines the modifier states, such as shift, control and alt.
#[derive(Clone, Copy, Hash, Eq, PartialEq, Debug, PartialOrd, Ord)]
#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
pub struct KeyModifiers(u8);

bitflags! {
    impl KeyModifiers: u8 {
        const NONE = 0b0000_0000;
        const SHIFT = 0b0000_0001;
        const CONTROL = 0b0000_0010;
        const ALT = 0b0000_0100;
    }
}

impl KeyEvent {
    pub fn new(code: Key, modifiers: KeyModifiers) -> Self {
        Self { code, modifiers }
    }
}

impl From<Key> for KeyEvent {
    fn from(k: Key) -> Self {
        Self::new(k, KeyModifiers::NONE)
    }
}

#[derive(Debug, Eq, PartialEq, Copy, Clone, PartialOrd, Hash)]
#[cfg_attr(
    feature = "serialize",
    derive(Deserialize, Serialize),
    serde(tag = "type", content = "args")
)]
/// Defines Possible Media-related keys.
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 Mouse event
#[derive(Debug, Eq, PartialEq, Copy, Clone, PartialOrd, Hash)]
#[cfg_attr(
    feature = "serialize",
    derive(Deserialize, Serialize),
    serde(tag = "type")
)]
pub struct MouseEvent {
    /// The kind of mouse event that was caused
    pub kind: MouseEventKind,
    /// The key modifiers active when the event occurred
    pub modifiers: KeyModifiers,
    /// The column that the event occurred on
    pub column: u16,
    /// The row that the event occurred on
    pub row: u16,
}

/// Defines what kind of [`MouseEvent`] is possible
#[derive(Debug, Eq, PartialEq, Copy, Clone, PartialOrd, Hash)]
#[cfg_attr(
    feature = "serialize",
    derive(Deserialize, Serialize),
    serde(tag = "type", content = "args")
)]
pub enum MouseEventKind {
    /// Pressed mouse button. Contains the button that was pressed
    Down(MouseButton),
    /// Released mouse button. Contains the button that was released
    Up(MouseButton),
    /// Moved the mouse cursor while pressing the contained mouse button
    Drag(MouseButton),
    /// Moved / Hover changed without pressing any buttons
    Moved,
    /// Scrolled mouse wheel downwards
    ScrollDown,
    /// Scrolled mouse wheel upwards
    ScrollUp,
    /// Scrolled mouse wheel left
    ScrollLeft,
    /// Scrolled mouse wheel right
    ScrollRight,
}

/// Defines all possible mouse buttons
#[derive(Debug, Eq, PartialEq, Copy, Clone, PartialOrd, Hash)]
#[cfg_attr(
    feature = "serialize",
    derive(Deserialize, Serialize),
    serde(tag = "type", content = "args")
)]
pub enum MouseButton {
    /// Left mouse button.
    Left,
    /// Right mouse button.
    Right,
    /// Middle mouse button.
    Middle,
}

#[cfg(test)]
mod test {

    use pretty_assertions::assert_eq;

    use super::*;
    use crate::mock::MockEvent;

    #[test]
    fn new_key_event() {
        let k = KeyEvent::new(Key::Down, KeyModifiers::CONTROL);
        assert_eq!(k.code, Key::Down);
        assert_eq!(k.modifiers, KeyModifiers::CONTROL);
    }

    #[test]
    fn key_event_from_key() {
        let k = KeyEvent::from(Key::Up);
        assert_eq!(k.code, Key::Up);
        assert_eq!(k.modifiers, KeyModifiers::NONE);
    }

    #[test]
    fn check_events() {
        let e: Event<MockEvent> = Event::Keyboard(KeyEvent::new(Key::Down, KeyModifiers::CONTROL));
        assert!(e.as_keyboard().is_some());
        assert_eq!(e.as_window_resize(), false);
        assert_eq!(e.as_tick(), false);
        assert_eq!(e.as_mouse().is_some(), false);
        assert!(e.as_user().is_none());
        let e: Event<MockEvent> = Event::WindowResize(0, 24);
        assert!(e.as_window_resize());
        assert!(e.as_keyboard().is_none());
        let e: Event<MockEvent> = Event::Tick;
        assert!(e.as_tick());
        let e: Event<MockEvent> = Event::User(MockEvent::Bar);
        assert_eq!(e.as_user().unwrap(), &MockEvent::Bar);

        let e: Event<MockEvent> = Event::Mouse(MouseEvent {
            kind: MouseEventKind::Moved,
            modifiers: KeyModifiers::NONE,
            column: 0,
            row: 0,
        });
        assert!(e.as_mouse().is_some());
        assert_eq!(e.as_keyboard().is_some(), false);
        assert_eq!(e.as_tick(), false);
        assert_eq!(e.as_window_resize(), false);
    }

    // -- serde
    #[cfg(feature = "serialize")]
    use std::fs::File;
    #[cfg(feature = "serialize")]
    use std::io::{Read, Write};

    #[cfg(feature = "serialize")]
    use serde::de::DeserializeOwned;
    #[cfg(feature = "serialize")]
    use serde::{Deserialize, Serialize};
    #[cfg(feature = "serialize")]
    use tempfile::NamedTempFile;

    #[cfg(feature = "serialize")]
    fn deserialize<R, S>(mut readable: R) -> S
    where
        R: Read,
        S: DeserializeOwned + Sized + std::fmt::Debug,
    {
        // Read file content
        let mut data: String = String::new();
        if let Err(err) = readable.read_to_string(&mut data) {
            panic!("Error: {}", err);
        }
        // Deserialize
        match toml::de::from_str(data.as_str()) {
            Ok(deserialized) => deserialized,
            Err(err) => panic!("Error: {}", err),
        }
    }

    #[cfg(feature = "serialize")]
    fn serialize<S, W>(serializable: &S, mut writable: W)
    where
        S: Serialize + Sized,
        W: Write,
    {
        // Serialize content
        let data: String = match toml::ser::to_string(serializable) {
            Ok(dt) => dt,
            Err(err) => {
                panic!("Error: {}", err);
            }
        };
        // Write file
        if let Err(err) = writable.write_all(data.as_bytes()) {
            panic!("Error: {}", err)
        }
    }

    #[cfg(feature = "serialize")]
    #[derive(Debug, PartialEq, Deserialize, Serialize)]
    struct KeyBindings {
        pub quit: KeyEvent,
        pub open: KeyEvent,
    }

    #[cfg(feature = "serialize")]
    impl KeyBindings {
        pub fn new(quit: KeyEvent, open: KeyEvent) -> Self {
            Self { quit, open }
        }
    }

    #[test]
    #[cfg(feature = "serialize")]
    fn should_serialize_key_bindings() {
        let temp = NamedTempFile::new().expect("Failed to open tempfile");
        let keys = KeyBindings::new(
            KeyEvent::from(Key::Esc),
            KeyEvent::new(Key::Char('o'), KeyModifiers::CONTROL),
        );
        let mut config = File::create(temp.path()).expect("Failed to open file for write");
        serialize(&keys, &mut config);
        let mut readable = File::open(temp.path()).expect("Failed to open file for read");
        let r_keys: KeyBindings = deserialize(&mut readable);
        assert_eq!(keys, r_keys);
    }
}