DEV Community

Stacy Roll
Stacy Roll

Posted on • Updated on

Why I use k_board instead of termion 💻

I used to use Termion for managing my dynamic menus, but the truth is that the crate's weight was too much for my production projects.

While searching on the official crates.io website, I came across a crate that served the same purpose as Termion but was lighter and had a lower-level logic. I'm talking about k_board.

I compared them in terms of syntax, and I find k_board even more comfortable than Termion.

Official termion example in crates.io:

fn main() {
    let stdin = stdin();
    let mut stdout = MouseTerminal::from(stdout().into_raw_mode().unwrap());

    write!(stdout, "{}{}q to exit. Click, click, click!", termion::clear::All, termion::cursor::Goto(1, 1)).unwrap();
    stdout.flush().unwrap();

    for c in stdin.events() {
        let evt = c.unwrap();
        match evt {
            Event::Key(Key::Char('q')) => break,
            Event::Mouse(me) => {
                match me {
                    MouseEvent::Press(_, x, y) => {
                        write!(stdout, "{}x", termion::cursor::Goto(x, y)).unwrap();
                    },
                    _ => (),
                }
            }
            _ => {}
        }
        stdout.flush().unwrap();
    }
}

Enter fullscreen mode Exit fullscreen mode

Official k_board example in crates.io:

fn main() {
    std::process::Command::new("clear").status().unwrap();
    println!("[*] I use k_board lightweight software");
    println!("[ ] I use heavyweight software");
    for key in Keyboard::new() {
        match key {
            Keys::Up => {
                std::process::Command::new("clear").status().unwrap();
                println!("[*] I use k_board lightweight software");
                println!("[ ] I use heavyweight software");
            }
            Keys::Down => {
                std::process::Command::new("clear").status().unwrap();
                println!("[ ] I use k_board lightweight software");
                println!("[*] I use heavyweight software");
            }
            Keys::Enter => {
                break;
            }
            _ => {}
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

I've done a comparison between the two, and I'm making the information available to you.

Topic termion 2.0.1 k_board 1.1.3
Size 22.9 KiB 15.9 KiB
"hello world" size compiled 19MB 4,7MB
Dependencies 5 0
License MIT GPLv3

Top comments (0)