Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Ratatui - Hello World

  • DefaultTerminal
  • render_widget
  • KeyEventKind}
  • KeyCode
  • Paragraph
  • Event

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.
[package]
name = "hello-world"
version = "0.1.0"
edition = "2021"

[dependencies]
crossterm = "0.28.1"
ratatui = "0.29.0"
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(());
            }
        }
    }
}