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
pub mod app;
pub mod image;
pub mod inputs;
pub mod io;
pub mod utils;
use crate::{
app::{ui, App, AppReturn},
io::IoEvent,
};
use crossterm::{
event::{DisableMouseCapture, EnableMouseCapture},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use eyre::Result;
use inputs::{events::Events, InputEvent};
use std::{io::stdout, sync::Arc, time::Duration};
use tui::{backend::CrosstermBackend, Terminal};
pub async fn start_ui<'a>(app: &Arc<tokio::sync::Mutex<App<'a>>>) -> Result<()> {
let mut stdout = stdout();
enable_raw_mode()?;
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
terminal.clear()?;
terminal.hide_cursor()?;
let tick_rate = Duration::from_millis(100);
let mut events = Events::new(tick_rate);
{
let mut app = app.lock().await;
app.dispatch(IoEvent::Initialize).await;
}
loop {
let mut app = app.lock().await;
terminal.draw(|rect| ui::draw(rect, &mut app))?;
let result = match events.next().await {
InputEvent::Input(key) => app.do_action(key).await,
InputEvent::Tick => app.update_on_tick().await,
};
if result == AppReturn::Exit {
events.close();
break;
}
}
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
terminal.show_cursor()?;
Ok(())
}