42 lines
1,017 B
Rust
42 lines
1,017 B
Rust
//! TUI
|
|
|
|
pub mod keys;
|
|
pub mod state;
|
|
pub mod terminal;
|
|
pub mod ui;
|
|
|
|
use anyhow::Result;
|
|
|
|
use state::State;
|
|
use terminal::Event;
|
|
|
|
use crate::places::db_repository::DbPlacesRepository;
|
|
|
|
/// Fires up the UI
|
|
pub async fn tui(places_repository: DbPlacesRepository) -> Result<()> {
|
|
let (_, terminal_height) = crossterm::terminal::size()?;
|
|
let mut state = State::new(places_repository, terminal_height);
|
|
let mut tui = terminal::Tui::new()?;
|
|
|
|
let result = loop {
|
|
match tui.next().await? {
|
|
Event::Key(key_event) => keys::handle_key(&mut state, key_event).await,
|
|
Event::Render => {
|
|
tui.draw(|frame| ui::ui_draw(&mut state, frame))?;
|
|
}
|
|
Event::Tick => {
|
|
state.fetch_places().await;
|
|
}
|
|
Event::Resize(_, h) => state.height = h,
|
|
Event::Quit => state.quit = true,
|
|
_ => {}
|
|
}
|
|
if state.quit {
|
|
break Ok(());
|
|
}
|
|
};
|
|
|
|
tui.stop()?;
|
|
|
|
result
|
|
}
|