getchar/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use std::io;
use std::io::{Read, Write};
use termion::raw::IntoRawMode;

pub fn getchar() -> Option<char> {
    let mut buffer = [0];
    let stdout = io::stdout().into_raw_mode().unwrap();
    let mut stdin = io::stdin();

    stdout.lock().flush().unwrap();

    if stdin.read_exact(&mut buffer).is_ok() {
        Some( buffer[0] as char)
    } else {
        None
    }
}