Improve crossterm demo

* Use AlternateScreen
* Handle input events
pull/165/head
Florian Dehau 5 years ago
parent 8fbb764c9e
commit 9a73ead88d

@ -7,7 +7,7 @@ use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use crossterm;
use crossterm::{input, AlternateScreen, InputEvent, KeyEvent};
use structopt::StructOpt;
use tui::backend::CrosstermBackend;
use tui::Terminal;
@ -31,7 +31,8 @@ fn main() -> Result<(), failure::Error> {
let cli = Cli::from_args();
stderrlog::new().quiet(!cli.log).verbosity(4).init()?;
let backend = CrosstermBackend::new();
let screen = AlternateScreen::to_alternate(true)?;
let backend = CrosstermBackend::with_alternate_screen(screen)?;
let mut terminal = Terminal::new(backend)?;
terminal.hide_cursor()?;
@ -40,18 +41,19 @@ fn main() -> Result<(), failure::Error> {
{
let tx = tx.clone();
thread::spawn(move || {
let input = crossterm::input();
loop {
match input.read_char() {
Ok(key) => {
let input = input();
let reader = input.read_sync();
for event in reader {
match event {
InputEvent::Keyboard(key) => {
if let Err(_) = tx.send(Event::Input(key)) {
return;
}
if key == 'q' {
if key == KeyEvent::Char('q') {
return;
}
}
Err(_) => {}
_ => {}
}
}
});
@ -74,10 +76,14 @@ fn main() -> Result<(), failure::Error> {
loop {
ui::draw(&mut terminal, &app)?;
match rx.recv()? {
Event::Input(key) => {
// TODO: handle key events once they are supported by crossterm
app.on_key(key);
}
Event::Input(event) => match event {
KeyEvent::Char(c) => app.on_key(c),
KeyEvent::Left => app.on_left(),
KeyEvent::Up => app.on_up(),
KeyEvent::Right => app.on_right(),
KeyEvent::Down => app.on_down(),
_ => {}
},
Event::Tick => {
app.on_tick();
}

Loading…
Cancel
Save