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;
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),
}