2019-02-14 19:15:54 +00:00
|
|
|
mod demo;
|
2019-01-24 18:45:59 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
mod util;
|
|
|
|
|
2019-02-14 19:15:54 +00:00
|
|
|
use crate::demo::{ui, App};
|
2020-03-13 01:02:51 +00:00
|
|
|
use argh::FromArgs;
|
|
|
|
use easycurses;
|
|
|
|
use std::{
|
|
|
|
error::Error,
|
|
|
|
io,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
|
|
|
use tui::{backend::CursesBackend, Terminal};
|
|
|
|
|
|
|
|
/// Curses demo
|
|
|
|
#[derive(Debug, FromArgs)]
|
2019-02-14 19:15:54 +00:00
|
|
|
struct Cli {
|
2020-03-13 01:02:51 +00:00
|
|
|
/// time in ms between two ticks.
|
|
|
|
#[argh(option, default = "250")]
|
2019-02-14 19:15:54 +00:00
|
|
|
tick_rate: u64,
|
2020-04-13 23:04:07 +00:00
|
|
|
/// whether unicode symbols are used to improve the overall look of the app
|
|
|
|
#[argh(option, default = "true")]
|
|
|
|
enhanced_graphics: bool,
|
2019-01-24 18:45:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
let cli: Cli = argh::from_env();
|
2019-01-24 18:45:59 +00:00
|
|
|
|
2019-02-14 20:15:32 +00:00
|
|
|
let mut backend = CursesBackend::new().ok_or(io::Error::new(io::ErrorKind::Other, ""))?;
|
|
|
|
let curses = backend.get_curses_mut();
|
|
|
|
curses.set_echo(false);
|
|
|
|
curses.set_input_timeout(easycurses::TimeoutMode::WaitUpTo(50));
|
|
|
|
curses.set_input_mode(easycurses::InputMode::RawCharacter);
|
|
|
|
curses.set_keypad_enabled(true);
|
|
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
terminal.hide_cursor()?;
|
2019-01-24 18:45:59 +00:00
|
|
|
|
2020-04-13 23:04:07 +00:00
|
|
|
let mut app = App::new("Curses demo", cli.enhanced_graphics);
|
2019-01-24 18:45:59 +00:00
|
|
|
|
2019-02-14 19:15:54 +00:00
|
|
|
let mut last_tick = Instant::now();
|
|
|
|
let tick_rate = Duration::from_millis(cli.tick_rate);
|
2019-01-24 18:45:59 +00:00
|
|
|
loop {
|
2019-12-15 20:38:18 +00:00
|
|
|
terminal.draw(|mut f| ui::draw(&mut f, &mut app))?;
|
2019-02-14 20:15:32 +00:00
|
|
|
match terminal.backend_mut().get_curses_mut().get_input() {
|
2019-01-24 18:45:59 +00:00
|
|
|
Some(input) => {
|
|
|
|
match input {
|
2019-02-14 19:15:54 +00:00
|
|
|
easycurses::Input::Character(c) => {
|
|
|
|
app.on_key(c);
|
|
|
|
}
|
2019-01-24 18:45:59 +00:00
|
|
|
easycurses::Input::KeyUp => {
|
2019-02-14 19:15:54 +00:00
|
|
|
app.on_up();
|
2019-01-24 18:45:59 +00:00
|
|
|
}
|
|
|
|
easycurses::Input::KeyDown => {
|
2019-02-14 19:15:54 +00:00
|
|
|
app.on_down();
|
2019-01-24 18:45:59 +00:00
|
|
|
}
|
|
|
|
easycurses::Input::KeyLeft => {
|
2019-02-14 19:15:54 +00:00
|
|
|
app.on_left();
|
2019-01-24 18:45:59 +00:00
|
|
|
}
|
|
|
|
easycurses::Input::KeyRight => {
|
2019-02-14 19:15:54 +00:00
|
|
|
app.on_right();
|
2019-01-24 18:45:59 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
};
|
2019-02-14 20:15:32 +00:00
|
|
|
terminal.backend_mut().get_curses_mut().flush_input();
|
2019-02-14 19:15:54 +00:00
|
|
|
if last_tick.elapsed() > tick_rate {
|
|
|
|
app.on_tick();
|
|
|
|
last_tick = Instant::now();
|
|
|
|
}
|
|
|
|
if app.should_quit {
|
|
|
|
break;
|
2019-01-24 18:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-14 19:15:54 +00:00
|
|
|
Ok(())
|
2019-01-24 18:45:59 +00:00
|
|
|
}
|