termion/examples/async.rs

40 lines
1.1 KiB
Rust
Raw Normal View History

2016-03-15 20:36:33 +00:00
extern crate termion;
2016-03-15 19:32:25 +00:00
use termion::raw::IntoRawMode;
use termion::async_stdin;
2016-09-07 10:39:32 +01:00
use std::io::{Read, Write, stdout};
2016-03-15 19:32:25 +00:00
use std::thread;
use std::time::Duration;
fn main() {
let stdout = stdout();
let mut stdout = stdout.lock().into_raw_mode().unwrap();
let mut stdin = async_stdin().bytes();
2017-03-24 20:53:05 +00:00
write!(stdout,
"{}{}",
termion::clear::All,
termion::cursor::Goto(1, 1))
.unwrap();
2016-03-15 19:32:25 +00:00
loop {
write!(stdout, "{}", termion::clear::CurrentLine).unwrap();
2016-03-15 19:32:25 +00:00
let b = stdin.next();
write!(stdout, "\r{:?} <- This demonstrates the async read input char. Between each update a 100 ms. is waited, simply to demonstrate the async fashion. \n\r", b).unwrap();
if let Some(Ok(b'q')) = b {
break;
}
stdout.flush().unwrap();
thread::sleep(Duration::from_millis(50));
stdout.write_all(b"# ").unwrap();
2016-03-15 19:32:25 +00:00
stdout.flush().unwrap();
thread::sleep(Duration::from_millis(50));
stdout.write_all(b"\r #").unwrap();
write!(stdout, "{}", termion::cursor::Goto(1, 1)).unwrap();
2016-03-15 19:32:25 +00:00
stdout.flush().unwrap();
}
}