2023-06-12 05:07:15 +00:00
|
|
|
use std::{
|
|
|
|
error::Error,
|
|
|
|
io,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
|
|
|
|
2019-12-15 20:38:18 +00:00
|
|
|
use crossterm::{
|
2023-04-13 20:21:35 +00:00
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
|
2019-12-15 20:38:18 +00:00
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
};
|
2023-07-10 22:59:01 +00:00
|
|
|
use ratatui::prelude::*;
|
2023-06-12 05:07:15 +00:00
|
|
|
|
|
|
|
use crate::{app::App, ui};
|
2019-02-10 21:35:44 +00:00
|
|
|
|
2021-11-11 14:56:37 +00:00
|
|
|
pub fn run(tick_rate: Duration, enhanced_graphics: bool) -> Result<(), Box<dyn Error>> {
|
2021-11-01 21:27:46 +00:00
|
|
|
// setup terminal
|
2019-12-15 22:03:02 +00:00
|
|
|
enable_raw_mode()?;
|
2021-11-01 21:27:46 +00:00
|
|
|
let mut stdout = io::stdout();
|
2020-05-27 21:50:45 +00:00
|
|
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
2019-12-15 22:03:02 +00:00
|
|
|
let backend = CrosstermBackend::new(stdout);
|
2019-02-10 21:35:44 +00:00
|
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// create app and run it
|
2021-11-11 14:56:37 +00:00
|
|
|
let app = App::new("Crossterm Demo", enhanced_graphics);
|
2021-11-01 21:27:46 +00:00
|
|
|
let res = run_app(&mut terminal, app, tick_rate);
|
|
|
|
|
|
|
|
// restore terminal
|
|
|
|
disable_raw_mode()?;
|
|
|
|
execute!(
|
|
|
|
terminal.backend_mut(),
|
|
|
|
LeaveAlternateScreen,
|
|
|
|
DisableMouseCapture
|
|
|
|
)?;
|
|
|
|
terminal.show_cursor()?;
|
2019-02-10 21:35:44 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
if let Err(err) = res {
|
2023-05-22 03:46:02 +00:00
|
|
|
println!("{err:?}");
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
2019-02-10 21:35:44 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-02-10 21:35:44 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
fn run_app<B: Backend>(
|
|
|
|
terminal: &mut Terminal<B>,
|
|
|
|
mut app: App,
|
|
|
|
tick_rate: Duration,
|
|
|
|
) -> io::Result<()> {
|
|
|
|
let mut last_tick = Instant::now();
|
2019-02-10 21:35:44 +00:00
|
|
|
loop {
|
2020-06-15 20:57:23 +00:00
|
|
|
terminal.draw(|f| ui::draw(f, &mut app))?;
|
2021-11-01 21:27:46 +00:00
|
|
|
|
2023-10-20 19:31:52 +00:00
|
|
|
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
|
2021-11-01 21:27:46 +00:00
|
|
|
if crossterm::event::poll(timeout)? {
|
|
|
|
if let Event::Key(key) = event::read()? {
|
2023-04-13 20:21:35 +00:00
|
|
|
if key.kind == KeyEventKind::Press {
|
|
|
|
match key.code {
|
2023-12-15 03:11:48 +00:00
|
|
|
KeyCode::Left | KeyCode::Char('h') => app.on_left(),
|
|
|
|
KeyCode::Up | KeyCode::Char('k') => app.on_up(),
|
|
|
|
KeyCode::Right | KeyCode::Char('l') => app.on_right(),
|
|
|
|
KeyCode::Down | KeyCode::Char('j') => app.on_down(),
|
2023-04-13 20:21:35 +00:00
|
|
|
KeyCode::Char(c) => app.on_key(c),
|
|
|
|
_ => {}
|
|
|
|
}
|
2019-12-15 22:03:02 +00:00
|
|
|
}
|
2019-02-10 21:35:44 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
if last_tick.elapsed() >= tick_rate {
|
|
|
|
app.on_tick();
|
|
|
|
last_tick = Instant::now();
|
|
|
|
}
|
2019-02-10 21:35:44 +00:00
|
|
|
if app.should_quit {
|
2021-11-01 21:27:46 +00:00
|
|
|
return Ok(());
|
2019-02-10 21:35:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|