mirror of
https://github.com/ratatui/ratatui.git
synced 2025-09-27 13:01:13 +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))?; ```
Demo example
This is the original demo that was developed for Tui-rs (the library that Ratatui was forked from).
This example is available for each backend. To run it:
crossterm
cargo run -p demo
termion
cargo run -p demo --no-default-features --features termion
termwiz
cargo run -p demo --no-default-features --features termwiz