Read keys instead of bytes

This commit is contained in:
Timothy Warren 2021-01-28 15:53:09 -05:00
parent ad0b0c5db2
commit a7fe16b007

View File

@ -1,11 +1,8 @@
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);
}
@ -13,20 +10,18 @@ fn die(e: std::io::Error) {
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;
for key in io::stdin().keys() {
match key {
Ok(key) => match key {
Key::Char(c) => {
if c.is_control() {
println!("{:?} \r", b);
println!("{:?}\r", c as u8);
} else {
println!("{:?} ({})\r", b, c);
println!("{:?} ({})\r", c as u8, c);
}
if b == to_ctrl_byte('q') {
break;
}
Key::Ctrl('q') => break,
_ => println!("{:?}\r", key),
}
Err(err) => die(err),
}