Ratatui Has a Free API That Most Developers Dont Know About

# rust# terminal# tui# devtools
Ratatui Has a Free API That Most Developers Dont Know AboutAlex Spinov

Ratatui is a Rust library for building terminal user interfaces (TUI). Build dashboards, file...

Ratatui is a Rust library for building terminal user interfaces (TUI). Build dashboards, file managers, and interactive tools for the terminal.

Hello World

use ratatui::{prelude::*, widgets::Paragraph};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut terminal = ratatui::init();
    terminal.draw(|frame| {
        frame.render_widget(
            Paragraph::new("Hello, Ratatui!")
                .block(Block::default().title("Demo").borders(Borders::ALL)),
            frame.area()
        );
    })?;
    std::thread::sleep(std::time::Duration::from_secs(3));
    ratatui::restore();
    Ok(())
}
Enter fullscreen mode Exit fullscreen mode

Layout System

let [header, body, footer] = Layout::vertical([
    Constraint::Length(3),
    Constraint::Min(0),
    Constraint::Length(1),
]).areas(frame.area());

let [left, right] = Layout::horizontal([
    Constraint::Percentage(30),
    Constraint::Percentage(70),
]).areas(body);
Enter fullscreen mode Exit fullscreen mode

Widgets

// Table
let table = Table::new(
    vec![Row::new(vec!["Alice", "30"]), Row::new(vec!["Bob", "25"])],
    [Constraint::Length(10), Constraint::Length(5)]
).header(Row::new(vec!["Name", "Age"]).style(Style::default().bold()));

// Bar Chart
let chart = BarChart::default()
    .data(&[("Mon", 5), ("Tue", 8), ("Wed", 12)])
    .bar_width(5);

// Gauge
let gauge = Gauge::default()
    .percent(75)
    .label("75%")
    .gauge_style(Style::default().fg(Color::Green));
Enter fullscreen mode Exit fullscreen mode

Key Features

  • Flexible layout system
  • Rich widget library
  • Cross-platform terminal rendering
  • Event handling
  • Used in gitui, bottom, and more

Need to scrape or monitor web data at scale? Check out my web scraping actors on Apify or email spinov001@gmail.com for custom solutions.