mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-22 20:53:19 +00:00
3f62ce9c19
* Remove log, stderrlog, structopt * Add argh
70 lines
1.7 KiB
Rust
70 lines
1.7 KiB
Rust
mod demo;
|
|
#[allow(dead_code)]
|
|
mod util;
|
|
|
|
use crate::{
|
|
demo::{ui, App},
|
|
util::event::{Config, Event, Events},
|
|
};
|
|
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};
|
|
|
|
/// Termion 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 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()?;
|
|
|
|
let mut app = App::new("Termion demo");
|
|
loop {
|
|
terminal.draw(|mut f| ui::draw(&mut f, &mut app))?;
|
|
|
|
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(())
|
|
}
|