//! # [Ratatui] Docs.rs example
//! The latest version of this example is available in the [examples] folder in the repository.
//! Please note that the examples are designed to be run against the `main` branch of the Github
//! repository. This means that you may not be able to compile with the latest release version on
//! crates.io, or the one that you have installed locally.
//! See the [examples readme] for more information on finding examples that match the version of the
//! library you are using.
//! [Ratatui]: https://github.com/ratatui-org/ratatui
//! [examples]: https://github.com/ratatui-org/ratatui/blob/main/examples
//! [examples readme]: https://github.com/ratatui-org/ratatui/blob/main/examples/README.md
use std::io::{self, stdout};
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
widgets::{Block, Borders, Paragraph},
/// Example code for lib.rs
/// When cargo-rdme supports doc comments that import from code, this will be imported
/// rather than copied to the lib.rs file.
fn main() -> io::Result<()> {
let arg = std::env::args().nth(1).unwrap_or_default();
stdout().execute(EnterAlternateScreen)?;
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
let mut should_quit = false;
terminal.draw(match arg.as_str() {
should_quit = handle_events()?;
stdout().execute(LeaveAlternateScreen)?;
fn hello_world(frame: &mut Frame) {
Paragraph::new("Hello World!").block(Block::bordered().title("Greeting")),
use crossterm::event::{self, Event, KeyCode};
fn handle_events() -> io::Result<bool> {
if event::poll(std::time::Duration::from_millis(50))? {
if let Event::Key(key) = event::read()? {
if key.kind == event::KeyEventKind::Press && key.code == KeyCode::Char('q') {
fn layout(frame: &mut Frame) {
let vertical = Layout::vertical([
let horizontal = Layout::horizontal([Constraint::Ratio(1, 2); 2]);
let [title_bar, main_area, status_bar] = vertical.areas(frame.size());
let [left, right] = horizontal.areas(main_area);
Block::new().borders(Borders::TOP).title("Title Bar"),
Block::new().borders(Borders::TOP).title("Status Bar"),
frame.render_widget(Block::bordered().title("Left"), left);
frame.render_widget(Block::bordered().title("Right"), right);
fn styling(frame: &mut Frame) {
let areas = Layout::vertical([
let span1 = Span::raw("Hello ");
let span2 = Span::styled(
.add_modifier(Modifier::BOLD),
let span3 = "!".red().on_light_yellow().italic();
let line = Line::from(vec![span1, span2, span3]);
let text: Text = Text::from(vec![line]);
frame.render_widget(Paragraph::new(text), areas[0]);
// or using the short-hand syntax and implicit conversions
Paragraph::new("Hello World!".red().on_white().bold()),
// to style the whole widget instead of just the text
Paragraph::new("Hello World!").style(Style::new().red().on_white()),
// or using the short-hand syntax
frame.render_widget(Paragraph::new("Hello World!").blue().on_yellow(), areas[3]);