- DefaultTerminal
- render_widget
- KeyEventKind
- KeyCode
- Paragraph
- Event
Ratatui - Hello World
Taken from the Hello world tutorial
- This example turns the terminal blue and writes some text on it.
- It waits till the user presses q and then quits.
examples/ratatui/hello-world/Cargo.toml
[package] name = "hello-world" version = "0.1.0" edition = "2021" [dependencies] crossterm = "0.28.1" ratatui = "0.29.0"
examples/ratatui/hello-world/src/main.rs
use std::io; use ratatui::{ crossterm::event::{self, KeyCode, KeyEventKind}, style::Stylize, widgets::Paragraph, DefaultTerminal, }; fn main() -> io::Result<()> { let mut terminal = ratatui::init(); terminal.clear()?; let app_result = run(terminal); ratatui::restore(); app_result } fn run(mut terminal: DefaultTerminal) -> io::Result<()> { loop { terminal.draw(|frame| { let greeting = Paragraph::new("Hello Ratatui! (press 'q' to quit)") .white() .on_blue(); frame.render_widget(greeting, frame.area()); })?; if let event::Event::Key(key) = event::read()? { if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') { return Ok(()); } } } }