Read keys instead of bytes

This commit is contained in:
Timothy Warren 2021-01-28 15:53:09 -05:00
parent ad0b0c5db2
commit a7fe16b007
1 changed files with 15 additions and 20 deletions

View File

@ -1,32 +1,27 @@
use std::io::{self, stdout, Read}; use std::io::{self, stdout};
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::IntoRawMode; use termion::raw::IntoRawMode;
fn to_ctrl_byte(c: char) -> u8 {
let byte = c as u8;
byte & 0b0001_1111
}
fn die(e: std::io::Error) { fn die(e: std::io::Error) {
panic!(e); panic!(e);
} }
fn main() { fn main() {
let _stdout = stdout().into_raw_mode().unwrap(); let _stdout = stdout().into_raw_mode().unwrap();
for b in io::stdin().bytes() { for key in io::stdin().keys() {
match b { match key {
Ok(b) => { Ok(key) => match key {
let b = b.unwrap(); Key::Char(c) => {
let c = b as char; if c.is_control() {
if c.is_control() { println!("{:?}\r", c as u8);
println!("{:?} \r", b); } else {
} else { println!("{:?} ({})\r", c as u8, c);
println!("{:?} ({})\r", b, c); }
}
if b == to_ctrl_byte('q') {
break;
} }
Key::Ctrl('q') => break,
_ => println!("{:?}\r", key),
} }
Err(err) => die(err), Err(err) => die(err),
} }