-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
demo.rs
77 lines (72 loc) · 2.08 KB
/
demo.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
//! ## Demo
//!
//! `Demo` shows how to use tui-realm in a real case
extern crate tuirealm;
use tuirealm::application::PollStrategy;
use tuirealm::{AttrValue, Attribute, Update};
// -- internal
mod app;
mod components;
use app::model::Model;
// Let's define the messages handled by our app. NOTE: it must derive `PartialEq`
#[derive(Debug, PartialEq)]
pub enum Msg {
AppClose,
Clock,
DigitCounterChanged(isize),
DigitCounterBlur,
LetterCounterChanged(isize),
LetterCounterBlur,
}
// Let's define the component ids for our application
#[derive(Debug, Eq, PartialEq, Clone, Hash)]
pub enum Id {
Clock,
DigitCounter,
LetterCounter,
Label,
}
fn main() {
// Setup model
let mut model = Model::default();
// Enter alternate screen
let _ = model.terminal.enter_alternate_screen();
let _ = model.terminal.enable_raw_mode();
// Main loop
// NOTE: loop until quit; quit is set in update if AppClose is received from counter
while !model.quit {
// Tick
match model.app.tick(PollStrategy::Once) {
Err(err) => {
assert!(model
.app
.attr(
&Id::Label,
Attribute::Text,
AttrValue::String(format!("Application error: {}", err)),
)
.is_ok());
}
Ok(messages) if messages.len() > 0 => {
// NOTE: redraw if at least one msg has been processed
model.redraw = true;
for msg in messages.into_iter() {
let mut msg = Some(msg);
while msg.is_some() {
msg = model.update(msg);
}
}
}
_ => {}
}
// Redraw
if model.redraw {
model.view();
model.redraw = false;
}
}
// Terminate terminal
let _ = model.terminal.leave_alternate_screen();
let _ = model.terminal.disable_raw_mode();
let _ = model.terminal.clear_screen();
}