2021-11-11 14:56:37 +00:00
|
|
|
use crate::{app::App, ui};
|
2019-12-15 20:38:18 +00:00
|
|
|
use crossterm::{
|
2021-11-01 21:27:46 +00:00
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
|
2019-12-15 20:38:18 +00:00
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
};
|
2023-03-17 16:03:49 +00:00
|
|
|
use ratatui::{
|
|
|
|
backend::{Backend, CrosstermBackend},
|
|
|
|
Terminal,
|
|
|
|
};
|
2019-12-15 22:03:02 +00:00
|
|
|
use std::{
|
2020-03-13 01:02:51 +00:00
|
|
|
error::Error,
|
2021-11-01 21:27:46 +00:00
|
|
|
io,
|
2020-04-14 15:21:56 +00:00
|
|
|
time::{Duration, Instant},
|
2019-12-15 22:03:02 +00:00
|
|
|
};
|
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 {
|
|
|
|
println!("{:?}", err)
|
|
|
|
}
|
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
|
|
|
|
|
|
|
let timeout = tick_rate
|
|
|
|
.checked_sub(last_tick.elapsed())
|
|
|
|
.unwrap_or_else(|| Duration::from_secs(0));
|
|
|
|
if crossterm::event::poll(timeout)? {
|
|
|
|
if let Event::Key(key) = event::read()? {
|
|
|
|
match key.code {
|
|
|
|
KeyCode::Char(c) => app.on_key(c),
|
|
|
|
KeyCode::Left => app.on_left(),
|
|
|
|
KeyCode::Up => app.on_up(),
|
|
|
|
KeyCode::Right => app.on_right(),
|
|
|
|
KeyCode::Down => app.on_down(),
|
|
|
|
_ => {}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|