async/
async.rs

1extern crate termion;
2
3use std::io::{stdout, Read, Write};
4use std::thread;
5use std::time::Duration;
6use termion::async_stdin;
7use termion::raw::IntoRawMode;
8
9fn main() {
10    let stdout = stdout();
11    let mut stdout = stdout.lock().into_raw_mode().unwrap();
12    let mut stdin = async_stdin().bytes();
13
14    write!(
15        stdout,
16        "{}{}",
17        termion::clear::All,
18        termion::cursor::Goto(1, 1)
19    )
20    .unwrap();
21
22    loop {
23        write!(stdout, "{}", termion::clear::CurrentLine).unwrap();
24
25        let b = stdin.next();
26        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();
27        if let Some(Ok(b'q')) = b {
28            break;
29        }
30
31        stdout.flush().unwrap();
32
33        thread::sleep(Duration::from_millis(50));
34        stdout.write_all(b"# ").unwrap();
35        stdout.flush().unwrap();
36        thread::sleep(Duration::from_millis(50));
37        stdout.write_all(b"\r #").unwrap();
38        write!(stdout, "{}", termion::cursor::Goto(1, 1)).unwrap();
39        stdout.flush().unwrap();
40    }
41}