86 lines
1.9 KiB
Rust
86 lines
1.9 KiB
Rust
use std::{error::Error, io, time::Duration};
|
|
|
|
use ratatui::{
|
|
Terminal,
|
|
backend::{Backend, CrosstermBackend},
|
|
crossterm::{
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event},
|
|
execute,
|
|
terminal::{
|
|
EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode,
|
|
enable_raw_mode,
|
|
},
|
|
},
|
|
};
|
|
|
|
use crate::{app::App, ui};
|
|
|
|
pub fn run(app: App) -> Result<(), Box<dyn Error>> {
|
|
// setup terminal
|
|
enable_raw_mode()?;
|
|
let mut stdout = io::stdout();
|
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
|
let backend = CrosstermBackend::new(stdout);
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
// create app and run it
|
|
let app_result = run_app(&mut terminal, app);
|
|
|
|
// restore terminal
|
|
disable_raw_mode()?;
|
|
execute!(
|
|
terminal.backend_mut(),
|
|
LeaveAlternateScreen,
|
|
DisableMouseCapture
|
|
)?;
|
|
terminal.show_cursor()?;
|
|
|
|
if let Err(err) = app_result {
|
|
println!("{err:?}");
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
// TODO: Implement save on quit
|
|
fn run_app<B: Backend>(
|
|
terminal: &mut Terminal<B>,
|
|
mut app: App,
|
|
) -> io::Result<()>
|
|
where
|
|
io::Error: From<B::Error>,
|
|
{
|
|
loop {
|
|
terminal.draw(|frame| ui::draw(frame, &mut app))?;
|
|
|
|
if event::poll(Duration::from_millis(10))? {
|
|
if let Event::Key(key) = event::read()? {
|
|
app.handle_key(key.code);
|
|
}
|
|
}
|
|
|
|
if app.should_quit {
|
|
return Ok(());
|
|
}
|
|
}
|
|
}
|
|
|
|
// pub fn run_app<B: Backend>(
|
|
// terminal: &mut Terminal<B>,
|
|
// mut app: App,
|
|
// ) -> io::Result<App>
|
|
// where
|
|
// io::Error: From<B::Error>,
|
|
// {
|
|
// loop {
|
|
// terminal.draw(|f| ui(f, &app))?;
|
|
//
|
|
// if let Event::Key(key) = event::read()? {
|
|
// app.handle_key(key.code);
|
|
// if app.should_quit {
|
|
// return Ok(app);
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|