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
use std::sync::mpsc;
use std::thread;
use crossterm::event::{Event as CrosstermEvent, KeyCode, KeyEvent, KeyModifiers};
use crate::app::Config;
use crate::Result;
#[derive(Debug, Eq, PartialEq)]
pub enum Event {
Exit,
NextTab,
PreviousTab,
Tick,
}
#[derive(Debug)]
pub struct EventHandler {
rx: mpsc::Receiver<Event>,
_input_handle: thread::JoinHandle<()>,
_tick_handle: thread::JoinHandle<()>,
}
impl EventHandler {
pub fn from_config(config: &Config) -> EventHandler {
let (tx, rx) = mpsc::channel();
let timeout = *config.delay();
// Thread than will handle user input and send events to receiver
let input_handle = {
let tx = tx.clone();
thread::spawn(move || {
trace!("Input thread spawned");
while crossterm::event::poll(timeout).is_ok() {
if let Ok(CrosstermEvent::Key(key)) = crossterm::event::read() {
let event = match key {
KeyEvent {
code: KeyCode::Left,
..
} => Event::PreviousTab,
KeyEvent {
code: KeyCode::Right,
..
} => Event::NextTab,
KeyEvent {
code: KeyCode::Char('q'),
..
} => Event::Exit,
KeyEvent {
code: KeyCode::Char('c'),
modifiers: KeyModifiers::CONTROL,
..
} => Event::Exit,
KeyEvent {
code: KeyCode::Esc, ..
} => Event::Exit,
_ => continue,
};
let is_exit = event == Event::Exit;
if let Err(e) = tx.send(event) {
// Now that's just terrible thing to do with poor thread :(
warn!(
"Input thread failed to send event and will be terminated: {:?}",
e
);
return;
}
// User had requested an exit, closing this thread too
if is_exit {
trace!(
"Input thread just sent the Exit event and going to terminate now"
);
return;
}
}
}
})
};
// Thread that will "tick" with some user-defined interval.
// Application might update state and re-draw UI on that event
let interval = *config.delay();
let tick_handle = {
thread::spawn(move || {
let tx = tx.clone();
trace!("Tick thread is spawned with {:?} interval", interval);
loop {
tx.send(Event::Tick).expect("Tick receiver is dead");
thread::sleep(interval);
}
})
};
EventHandler {
rx,
_input_handle: input_handle,
_tick_handle: tick_handle,
}
}
pub fn next(&self) -> Result<Event> {
match self.rx.recv() {
Ok(event) => {
trace!("UI thread had received an event: {:?}", event);
Ok(event)
}
Err(e) => Err(e.into()),
}
}
}