mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-26 06:30:29 +00:00
3f62ce9c19
* Remove log, stderrlog, structopt * Add argh
65 lines
1.6 KiB
Rust
65 lines
1.6 KiB
Rust
mod demo;
|
|
#[allow(dead_code)]
|
|
mod util;
|
|
|
|
use crate::demo::{ui, App};
|
|
use argh::FromArgs;
|
|
use rustbox::keyboard::Key;
|
|
use std::{
|
|
error::Error,
|
|
time::{Duration, Instant},
|
|
};
|
|
use tui::{backend::RustboxBackend, Terminal};
|
|
|
|
/// Rustbox demo
|
|
#[derive(Debug, FromArgs)]
|
|
struct Cli {
|
|
/// time in ms between two ticks.
|
|
#[argh(option, default = "250")]
|
|
tick_rate: u64,
|
|
}
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
let cli: Cli = argh::from_env();
|
|
|
|
let backend = RustboxBackend::new()?;
|
|
let mut terminal = Terminal::new(backend)?;
|
|
terminal.hide_cursor()?;
|
|
|
|
let mut app = App::new("Rustbox demo");
|
|
|
|
let mut last_tick = Instant::now();
|
|
let tick_rate = Duration::from_millis(cli.tick_rate);
|
|
loop {
|
|
terminal.draw(|mut f| ui::draw(&mut f, &mut app))?;
|
|
match terminal.backend().rustbox().peek_event(tick_rate, false) {
|
|
Ok(rustbox::Event::KeyEvent(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();
|
|
}
|
|
_ => {}
|
|
},
|
|
_ => {}
|
|
}
|
|
if last_tick.elapsed() > tick_rate {
|
|
app.on_tick();
|
|
last_tick = Instant::now();
|
|
}
|
|
if app.should_quit {
|
|
break;
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|