From a7fe16b0077850601668dc0f3cd316a9cb94e2eb Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 28 Jan 2021 15:53:09 -0500 Subject: [PATCH] Read keys instead of bytes --- src/main.rs | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index 380325d..6fe2cb8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; -fn to_ctrl_byte(c: char) -> u8 { - let byte = c as u8; - byte & 0b0001_1111 -} - fn die(e: std::io::Error) { panic!(e); } fn main() { let _stdout = stdout().into_raw_mode().unwrap(); - - for b in io::stdin().bytes() { - match b { - Ok(b) => { - let b = b.unwrap(); - let c = b as char; - if c.is_control() { - println!("{:?} \r", b); - } else { - println!("{:?} ({})\r", b, c); - } - - if b == to_ctrl_byte('q') { - break; + + for key in io::stdin().keys() { + match key { + Ok(key) => match key { + Key::Char(c) => { + if c.is_control() { + println!("{:?}\r", c as u8); + } else { + println!("{:?} ({})\r", c as u8, c); + } } + Key::Ctrl('q') => break, + _ => println!("{:?}\r", key), } Err(err) => die(err), }