2019-02-10 21:35:44 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
mod demo;
|
|
|
|
#[allow(dead_code)]
|
|
|
|
mod util;
|
|
|
|
|
2019-12-15 20:38:18 +00:00
|
|
|
use crate::demo::{ui, App};
|
2020-03-13 01:02:51 +00:00
|
|
|
use argh::FromArgs;
|
2019-12-15 20:38:18 +00:00
|
|
|
use crossterm::{
|
2020-05-27 21:50:45 +00:00
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event as CEvent, KeyCode},
|
2019-12-15 20:38:18 +00:00
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
};
|
2019-12-15 22:03:02 +00:00
|
|
|
use std::{
|
2020-03-13 01:02:51 +00:00
|
|
|
error::Error,
|
2021-01-01 15:21:07 +00:00
|
|
|
io::stdout,
|
2019-12-15 22:03:02 +00:00
|
|
|
sync::mpsc,
|
|
|
|
thread,
|
2020-04-14 15:21:56 +00:00
|
|
|
time::{Duration, Instant},
|
2019-12-15 22:03:02 +00:00
|
|
|
};
|
|
|
|
use tui::{backend::CrosstermBackend, Terminal};
|
2019-02-10 21:35:44 +00:00
|
|
|
|
|
|
|
enum Event<I> {
|
|
|
|
Input(I),
|
|
|
|
Tick,
|
|
|
|
}
|
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
/// Crossterm 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
|
|
|
|
2019-12-15 22:03:02 +00:00
|
|
|
enable_raw_mode()?;
|
|
|
|
|
|
|
|
let mut stdout = 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)?;
|
|
|
|
|
|
|
|
// Setup input handling
|
|
|
|
let (tx, rx) = mpsc::channel();
|
2019-11-29 08:06:59 +00:00
|
|
|
|
2020-04-13 23:04:07 +00:00
|
|
|
let tick_rate = Duration::from_millis(cli.tick_rate);
|
2019-11-29 08:06:59 +00:00
|
|
|
thread::spawn(move || {
|
2020-04-14 15:21:56 +00:00
|
|
|
let mut last_tick = Instant::now();
|
2019-11-29 08:06:59 +00:00
|
|
|
loop {
|
2019-12-15 22:03:02 +00:00
|
|
|
// poll for tick rate duration, if no events, sent tick event.
|
2020-09-20 13:51:44 +00:00
|
|
|
let timeout = tick_rate
|
|
|
|
.checked_sub(last_tick.elapsed())
|
|
|
|
.unwrap_or_else(|| Duration::from_secs(0));
|
|
|
|
if event::poll(timeout).unwrap() {
|
2019-12-15 22:03:02 +00:00
|
|
|
if let CEvent::Key(key) = event::read().unwrap() {
|
|
|
|
tx.send(Event::Input(key)).unwrap();
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 15:21:56 +00:00
|
|
|
if last_tick.elapsed() >= tick_rate {
|
|
|
|
tx.send(Event::Tick).unwrap();
|
|
|
|
last_tick = Instant::now();
|
|
|
|
}
|
2019-11-29 08:06:59 +00:00
|
|
|
}
|
|
|
|
});
|
2019-02-10 21:35:44 +00:00
|
|
|
|
2020-04-13 23:04:07 +00:00
|
|
|
let mut app = App::new("Crossterm Demo", cli.enhanced_graphics);
|
2019-02-10 21:35:44 +00:00
|
|
|
|
|
|
|
terminal.clear()?;
|
|
|
|
|
|
|
|
loop {
|
2020-06-15 20:57:23 +00:00
|
|
|
terminal.draw(|f| ui::draw(f, &mut app))?;
|
2019-02-10 21:35:44 +00:00
|
|
|
match rx.recv()? {
|
2019-12-15 22:03:02 +00:00
|
|
|
Event::Input(event) => match event.code {
|
|
|
|
KeyCode::Char('q') => {
|
|
|
|
disable_raw_mode()?;
|
2020-05-27 21:50:45 +00:00
|
|
|
execute!(
|
|
|
|
terminal.backend_mut(),
|
|
|
|
LeaveAlternateScreen,
|
|
|
|
DisableMouseCapture
|
|
|
|
)?;
|
2019-12-15 22:03:02 +00:00
|
|
|
terminal.show_cursor()?;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
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-06-16 21:03:36 +00:00
|
|
|
_ => {}
|
|
|
|
},
|
2019-02-10 21:35:44 +00:00
|
|
|
Event::Tick => {
|
|
|
|
app.on_tick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if app.should_quit {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|