mirror of
https://github.com/ratatui/ratatui.git
synced 2025-09-27 04:50:46 +00:00

This introduces a new `ratatui::run()` method which runs a closure with a terminal initialized with reasonable defaults for most applications. This calls `ratatui::init()` before running the closure and `ratatui::restore()` after the closure completes, and returns the result of the closure. A minimal hello world example using the new `ratatui::run()` method: ```rust fn main() -> Result<(), Box<dyn std::error::Error>> { ratatui::run(|terminal| { loop { terminal.draw(|frame| frame.render_widget("Hello World!", frame.area()))?; if crossterm::event::read()?.is_key_press() { break Ok(()); } } }) } ``` Of course, this also works both with apps that use free methods and structs: ```rust fn run(terminal: &mut DefaultTerminal) -> Result<(), AppError> { ... } ratatui::run(run)?; ``` ```rust struct App { ... } impl App { fn new() -> Self { ... } fn run(mut self, terminal: &mut DefaultTerminal) -> Result<(), AppError> { ... } } ratatui::run(|terminal| App::new().run(terminal))?; ```
Input Form example
This example demonstrates how to handle input across several form fields (2 strings and an number). It uses an enum to track the focused field, and sends keyboard events to one which is current.
Run this example with:
cargo run -p input-form
This example does not handle things like cursor movement within the line (just keys and backspace). Most apps would benefit from using the following crates for text input rather than directly using strings:
Some more ideas for handling focus can be found in:
focusable
(see also Ratatui forum post)rat-focus
- A useful
Bevy
discussion about focus more generally.