#terminal #build #user #interfaces

tortoise

Build terminal user interfaces in Rust

1 unstable release

0.1.0 Jun 24, 2021

45 downloads per month

MIT license

53KB
1.5K SLoC

Tortoise

Build terminal user interfaces in Rust.


lib.rs:

tortoise is a crate for building terminal user interfaces in Rust.

use tortoise::{event::Event, screen::Screen, style::Style};

let mut screen = Screen::open().unwrap();
screen.hide_cursor().unwrap();
loop {
let event = screen.listen().unwrap();
match event {
Event::Char('q') => break,
_ => {
screen.clear_row(0);
screen.put_str(0, 0, Style::default(), &format!("{}", event));
screen.flush().unwrap();
}
}
}

term-ui uses /dev/tty on Unix and $CONOUT$ on Windows instead of stdin/stdout so that your programs can still use standard I/O to do something useful. For example, you could write an interactive text editor that could be invoked like this.

echo "hello" | your-editor | # use the output...

Instead of interpreting "hello" as keystrokes in the interface, the text editor could use "hello" as the base text, and write the resulting text to stdout.

A Screen is a two dimensional grid of Cells, each of which stores a single character and associated style information such as color, bold, etc. Your application can manipulate that grid, and then call "flush" on the Screen to write any changes to the terminal. Nothing is written to the terminal until flush() is called, and the library keeps track of which cells have changed to minimize the amount of data to write. This means your code can be less clever in keeping track of what parts of the screen need to be changed but your application will remain reasonably fast.

Dependencies

~7MB
~150K SLoC