-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
54 lines (49 loc) · 1.38 KB
/
main.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
mod structure;
use crossterm::event::{self, Event as CEvent};
use std::{
sync::mpsc,
thread,
time::{Duration, Instant},
};
#[macro_use]
extern crate smart_default;
mod services;
mod ui;
mod utils;
enum Event<I> {
Input(I),
Tick,
}
fn main() {
utils::create_working_folder_if_not_exist();
let (tx, rx) = mpsc::channel();
let tick_rate = Duration::from_millis(250);
thread::spawn(move || {
let mut last_tick = Instant::now();
loop {
// poll for tick rate duration, if no events, sent tick event.
let timeout = tick_rate
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
if event::poll(timeout).unwrap() {
if let CEvent::Key(key) = event::read().unwrap() {
tx.send(Event::Input(key)).unwrap();
}
}
if last_tick.elapsed() >= tick_rate {
tx.send(Event::Tick).unwrap();
last_tick = Instant::now();
}
}
});
let mut app = structure::Application::new(utils::get_working_folder());
while app.is_running {
app.update();
match rx.recv().unwrap() {
Event::Input(event) => match event.code {
_ => app.handle_inputs(event.code),
},
Event::Tick => {}
}
}
}