Related: TUI
A Rust package for creating terminal user applications (TUIs).
Terminal Setup
When a TUI is spun up, a few things have to happen:
- An alternate screen is spun up—so that the output of your original terminal isn’t affected
- Raw mode is enabled—this turns off input and output processing by the terminal, allowing the TUI control over when to print characters to the screen
At teardown, the opposite happens.
Basic Setup
- Implement your own
Widgettrait (this assumes you need state)- Implement
.renderrequired by the trait - Define a
.runmethod for the widget that- call
terminal.draw - handle key events (powered by
crossterm)
- call
- Implement
- In the
mainfn,- initialize a ratatui terminal
- upon completion, restore the original terminal
fn main() {
let mut terminal = ratatui::init();
...
ratatui.restore();
}Thoughts
Was surprised that I have to manage spacing as part of a text (e.g. the ' ' prefix and suffix in " My Title ").