petitcarnets

Terminal user interfaces in the Elm architecture (model, update, view), for Rust, with no dependencies, following Bubble Tea v2.

The counter example: the up and down keys change the count, q quits and the program prints the final count

[dependencies]
petitcarnets = "0.0.11"

Why petitcarnets

A counter in thirty lines

use petitcarnets as tea;

struct Counter(i64);

impl tea::Model for Counter {
    type Msg = ();

    fn update(&mut self, msg: tea::Msg<()>) -> tea::Cmd<()> {
        match msg {
            tea::Msg::KeyPress(k) if k == "q" || k == "ctrl+c" => return tea::quit(),
            tea::Msg::KeyPress(k) if k == "up" => self.0 += 1,
            tea::Msg::KeyPress(k) if k == "down" => self.0 -= 1,
            _ => {}
        }
        tea::Cmd::none()
    }

    fn view(&self) -> tea::View {
        format!("Count: {}\n\nup/down to change, q to quit", self.0).into()
    }
}

fn main() -> Result<(), tea::Error> {
    tea::Program::new(Counter(0)).run()?;
    Ok(())
}

The model is your state, update turns a message into a new state and maybe a command, and view draws it.

Fewer bytes to the terminal

Bytes written per frame, mean over 100 frames on an 80x24 screen, inline. Byte counts do not depend on the machine.

ScenariopetitcarnetsOthers
log (24 lines, one new per frame)659Bubble Tea v2: 1,230; bubbletea-rs: 1,253
list (20 items, cursor moves)31.7Bubble Tea v2: 32.4; charmed-bubbletea: 241.0

Methodology and every scenario: Benchmarks.

When not to use it

No Windows yet, a smaller ecosystem than ratatui, and no Lip Gloss layers, Huh or Wish. The comparison has the evidence.

Read on