use cross::term::TerminalInput;
use libterm::TermKey;

fn main() {
    println!("Press Ctrl+D to exit");

    let mut input = TerminalInput::open().expect("Couldn't open terminal input");
    let mut buffer = [0; 64];

    loop {
        let (key, len) = input.read_key_with_bytes(&mut buffer).unwrap();

        if len == 1 {
            println!(
                "{key:?}\t{}\t0o{:04o}\t0x{:02x}",
                buffer[0], buffer[0], buffer[0]
            );
        } else {
            println!("{key:?}");
            print!("\t");
            for &byte in &buffer[..len] {
                match byte {
                    b'\x1B' => print!(" ^["),
                    _ => print!(" {:?}", byte as char),
                }
            }
            println!();
            print!("\t");
            for &byte in &buffer[..len] {
                print!(" {byte:02x}");
            }
            println!();
        }

        if key == TermKey::Eof {
            break;
        }
    }
}
