-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevent.rs
52 lines (44 loc) · 1.35 KB
/
event.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
use bitflags::_core::time::Duration;
use terminal::{error, stdout, Action, Event, KeyCode, KeyEvent, Retrieved, Value};
fn main() {
with_duration_read();
}
/// Block read indefinitely for events.
fn block_read() -> error::Result<()> {
let terminal = stdout();
terminal.act(Action::EnableRawMode)?;
loop {
if let Retrieved::Event(event) = terminal.get(Value::Event(None))? {
match event {
Some(Event::Key(KeyEvent {
code: KeyCode::Esc, ..
})) => return Ok(()),
Some(event) => {
println!("{:?}\r", event);
}
_ => {}
}
}
}
}
/// Reads events withing a certain duration.
fn with_duration_read() -> error::Result<()> {
let terminal = stdout();
terminal.act(Action::EnableRawMode)?;
terminal.act(Action::EnableMouseCapture)?;
loop {
if let Retrieved::Event(event) =
terminal.get(Value::Event(Some(Duration::from_millis(500))))?
{
match event {
Some(Event::Key(KeyEvent {
code: KeyCode::Esc, ..
})) => return Ok(()),
Some(event) => {
println!("{:?}\r", event);
}
None => println!("...\r"),
}
}
}
}