2019-02-10 21:35:44 +00:00
|
|
|
mod demo;
|
|
|
|
#[allow(dead_code)]
|
|
|
|
mod util;
|
|
|
|
|
2019-12-15 20:38:18 +00:00
|
|
|
use crate::{
|
|
|
|
demo::{ui, App},
|
|
|
|
util::event::{Config, Event, Events},
|
|
|
|
};
|
2020-03-13 01:02:51 +00:00
|
|
|
use argh::FromArgs;
|
|
|
|
use std::{error::Error, io, time::Duration};
|
|
|
|
use termion::{event::Key, input::MouseTerminal, raw::IntoRawMode, screen::AlternateScreen};
|
|
|
|
use tui::{backend::TermionBackend, Terminal};
|
2019-02-10 21:35:44 +00:00
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
/// Termion demo
|
|
|
|
#[derive(Debug, FromArgs)]
|
2019-02-10 21:35:44 +00:00
|
|
|
struct Cli {
|
2020-03-13 01:02:51 +00:00
|
|
|
/// time in ms between two ticks.
|
|
|
|
#[argh(option, default = "250")]
|
2019-02-10 21:35:44 +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-02-10 21:35:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
let cli: Cli = argh::from_env();
|
2019-02-10 21:35:44 +00:00
|
|
|
|
|
|
|
let events = Events::with_config(Config {
|
|
|
|
tick_rate: Duration::from_millis(cli.tick_rate),
|
|
|
|
..Config::default()
|
|
|
|
});
|
|
|
|
|
|
|
|
let stdout = io::stdout().into_raw_mode()?;
|
|
|
|
let stdout = MouseTerminal::from(stdout);
|
|
|
|
let stdout = AlternateScreen::from(stdout);
|
|
|
|
let backend = TermionBackend::new(stdout);
|
|
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
terminal.hide_cursor()?;
|
|
|
|
|
2020-04-13 23:04:07 +00:00
|
|
|
let mut app = App::new("Termion demo", cli.enhanced_graphics);
|
2019-02-10 21:35:44 +00:00
|
|
|
loop {
|
2019-12-15 20:38:18 +00:00
|
|
|
terminal.draw(|mut f| ui::draw(&mut f, &mut app))?;
|
|
|
|
|
2019-02-10 21:35:44 +00:00
|
|
|
match events.next()? {
|
|
|
|
Event::Input(key) => match key {
|
|
|
|
Key::Char(c) => {
|
|
|
|
app.on_key(c);
|
|
|
|
}
|
|
|
|
Key::Up => {
|
|
|
|
app.on_up();
|
|
|
|
}
|
|
|
|
Key::Down => {
|
|
|
|
app.on_down();
|
|
|
|
}
|
|
|
|
Key::Left => {
|
|
|
|
app.on_left();
|
|
|
|
}
|
|
|
|
Key::Right => {
|
|
|
|
app.on_right();
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
Event::Tick => {
|
|
|
|
app.on_tick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if app.should_quit {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|