Skip to main content

Crate petitcarnets

Crate petitcarnets 

Source
Expand description

§petitcarnets

Terminal user interfaces in the Elm architecture, with no dependencies: Bubble Tea for Rust, with the same names.

A program is a Model: its state, an update that turns each Msg (a key, a mouse event, a paste, the window size, or a message of your own) into the next state and a Cmd, and a view that draws the state and says which terminal modes it needs (mouse, focus reports, keyboard enhancements). A Program runs it: it reads and decodes input as Bubble Tea does, runs commands (work on other threads, timers) and redraws only what changed.

Import the crate as tea, and the code reads like its Go original:

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" || k == "+" => self.0 += 1,
            tea::Msg::KeyPress(k) if k == "down" || k == "-" => self.0 -= 1,
            _ => {}
        }
        tea::Cmd::none()
    }

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

let counter = tea::Program::new(Counter(0)).run()?;
println!("final count: {}", counter.0);

A program that should also work with a screen reader runs with Program::with_accessible, or in a terminal whose environment sets ACCESSIBLE: its frames are then printed as plain lines, with no redraws and no cursor movement.

Guides, in the repository’s docs directory: Bubble Tea’s names in Rust (go-to-rust.md), moving from the bubbletea-rs crate (migrating-from-bubbletea-rs.md), how the crate works (ARCHITECTURE.md), the Bubble Tea issues it addresses (issues.md), and Bubble Tea’s examples ported (examples.md).

Petits carnets: little notebooks. Linux, macOS and FreeBSD, each tested in CI; Windows is not supported yet.

Modules§

bubbles
Components: Bubbles v2, with the same names, default key bindings and behaviour, behind the bubbles feature (on by default, no dependencies).
style
Colour and emphasis, as ANSI escape codes, the visible width of styled text, and the few layout tools the components need: padding, borders, margins, joining blocks side by side or one above another, wrapping, truncating and cutting by display width.
testing
Testing models headless.

Macros§

printf
Prints a formatted line above the program’s frame: Bubble Tea’s tea.Printf, a println of format!’s result.

Structs§

Cmd
What an update asks the runtime to do next.
Cursor
The cursor: where it is, relative to the frame’s top left, and how it looks. Bubble Tea’s tea.Cursor.
Environment
Environment variables, as NAME, value pairs. Bubble Tea’s uv.Environ: give one to Program::with_environment to detect colours and terminal features from another environment than the process’s, as a server does for each SSH client.
Key
A key press or release: Bubble Tea’s Key.
KeyMod
Modifier keys and lock states, Bubble Tea’s KeyMod: combine them with | and test them with KeyMod::contains. The bits are Go’s, in Go’s order.
KeyboardEnhancements
The keyboard enhancements a view asks for, on top of the kitty protocol’s key disambiguation, which is always on. Bubble Tea’s KeyboardEnhancements: where Go sets a field, as in KeyboardEnhancements{ReportEventTypes: true}, chain the method of the same name, as in KeyboardEnhancements::new().report_event_types(true).
KeyboardEnhancementsReport
The terminal’s reply about its keyboard enhancements. Bubble Tea’s KeyboardEnhancementsMsg.
Mouse
Where the mouse was and what was held: Bubble Tea’s Mouse. The message it comes in says whether it is a click, a release, a wheel turn or motion.
Program
Runs a Model in a terminal. Bubble Tea’s tea.Program.
ProgressBar
A progress bar the terminal shows in its tab or title bar (OSC 9;4), where it has one. Bubble Tea’s tea.ProgressBar.
Rgb
A 24-bit colour.
Sender
Sends messages to a program from other threads: Bubble Tea’s Program.Send, Quit and Kill. Sending to a program that has stopped does nothing; Sender::send says so by returning false.
Size
A terminal size in cells.
Terminal
The process’s terminal.
View
What a model shows: Bubble Tea’s tea.View. Make one from a string with .into() or View::new, and set the rest with the with_ methods. Everything is declared per frame: after each frame the program changes whatever differs from the frame before, and puts the terminal back when it ends.

Enums§

ClipboardSelection
Which clipboard a Msg::Clipboard came from: the OSC 52 selection byte, as Bubble Tea’s ClipboardSelection.
ColorProfile
How many colours a terminal shows. Bubble Tea’s colorprofile.Profile, from fewest to most, so profiles compare.
ColorScheme
A terminal’s colour scheme.
CursorShape
The cursor’s shape. Bubble Tea’s CursorShape.
Error
Why a program stopped without returning its model. Bubble Tea’s ErrProgramKilled, ErrInterrupted and ErrProgramPanic.
Input
What a backend reports to the running program: what the terminal sent, decoded, and signals. Each terminal variant becomes the Msg of the same name.
KeyCode
Which key: a character, or a named key. Bubble Tea’s Key.Code.
ModeSetting
What a terminal says about a mode. Go’s ansi.ModeSetting.
MouseButton
A mouse button, as X11 numbers them. Bubble Tea’s MouseButton.
MouseMode
Which mouse events a view asks the terminal for. Bubble Tea’s MouseMode.
Msg
A message for Model::update: what the terminal reported, or one of the model’s own messages.
ProgressBarState
What a progress bar shows. Bubble Tea’s ProgressBarState.
ReportedMode
A mode a terminal reported on: a DEC private mode (CSI ? n $ y) or an ANSI one.
TerminalMode
A terminal mode a program can turn on and off. Bubble Tea turns them on and off from each View’s fields; a Backend writes them.

Traits§

Backend
A terminal a Program can run in.
ExecCommand
Something to run in the terminal with exec_command: Bubble Tea’s tea.ExecCommand. It runs on the program’s thread while the program has handed the terminal over, and reads and writes the terminal through the process’s standard streams.
Model
A program’s state and behaviour. Bubble Tea’s tea.Model.

Functions§

batch
Runs cmds at the same time, with no order between their messages. Bubble Tea’s tea.Batch.
clear_screen
Clears the screen and draws the next frame from the top left, with its modes written again: Bubble Tea’s tea.ClearScreen. Never needed for ordinary redraws.
every
Sends message(now) at the next whole multiple of period on the system clock: with a period of one second, on the second. Bubble Tea’s tea.Every; return it again from update to keep going.
exec
Runs command in the terminal, as Bubble Tea’s tea.ExecProcess does, then sends done(result): for an editor, a pager or a shell.
exec_command
Runs command in the terminal as exec runs a Command, then sends done(result): Bubble Tea’s tea.Exec, for anything that needs the terminal to itself.
interrupt
Stops the program with Error::Interrupted, as kill -INT does. Bubble Tea’s tea.Interrupt.
println
Prints text above the program’s frame, on lines of its own that stay in the terminal’s scrollback: Bubble Tea’s tea.Println. It is ordered with frames: the frame drawn before it stays below it. On the alternate screen it prints nothing, as in Bubble Tea. Use printf! to format.
quit
Stops the program; Program::run returns the model. Bubble Tea’s tea.Quit.
raw
Writes bytes to the terminal as they are, before the next frame: Bubble Tea’s tea.Raw, for queries and sequences petitcarnets does not send itself. Bytes that move the cursor or change the screen can confuse the renderer.
read_clipboard
Asks for the system clipboard’s text; the reply, where the terminal allows it, is Msg::Clipboard. Bubble Tea’s tea.ReadClipboard.
read_primary_clipboard
Asks for the primary selection’s text; the reply is Msg::Clipboard. Bubble Tea’s tea.ReadPrimaryClipboard.
release_terminal
Hands the terminal over as exec does and keeps it handed over, while the program goes on updating the model, until restore_terminal. Nothing is drawn meanwhile, and output from println, raw and the like waits. Bubble Tea’s Program.ReleaseTerminal.
repaint
Turns every mode the program set off and on again and draws the whole frame, for when something else wrote over the terminal: Bubble Tea’s tea.Repaint (its issue #1808). Unlike clear_screen it enters the alternate screen again if something left it, and redraws even when the view has not changed.
request_background_color
Asks for the terminal’s background colour; the reply is Msg::BackgroundColor. Bubble Tea’s tea.RequestBackgroundColor.
request_capability
Asks for a terminfo capability by name (XTGETTCAP), such as RGB, Tc or Ms; the reply is Msg::Capability, and none comes for a capability the terminal does not know. Bubble Tea’s tea.RequestCapability.
request_cursor_color
Asks for the cursor’s colour; the reply is Msg::CursorColor. Bubble Tea’s tea.RequestCursorColor.
request_cursor_position
Asks where the cursor is; the reply is Msg::CursorPosition. Bubble Tea’s tea.RequestCursorPosition.
request_foreground_color
Asks for the terminal’s text colour; the reply is Msg::ForegroundColor. Bubble Tea’s tea.RequestForegroundColor.
request_terminal_version
Asks for the terminal’s name and version (XTVERSION); the reply is Msg::TerminalVersion. Bubble Tea’s tea.RequestTerminalVersion.
request_window_size
Sends the window’s size as Msg::WindowSize, read from the terminal now. Bubble Tea’s tea.RequestWindowSize.
restore_terminal
Takes the terminal back after release_terminal: raw mode, every mode on again, the whole frame drawn again. Bubble Tea’s Program.RestoreTerminal. Does nothing if the terminal is not released.
sequence
Runs cmds one after another: each starts once the one before has sent its message (or, for a batch or sequence, all of its messages). Bubble Tea’s tea.Sequence.
set_clipboard
Copies text to the system clipboard (OSC 52), where the terminal allows it. Bubble Tea’s tea.SetClipboard.
set_primary_clipboard
Copies text to the primary selection (OSC 52). Bubble Tea’s tea.SetPrimaryClipboard.
suspend
Suspends the program as Ctrl-Z in a shell suspends one: the terminal is handed back as for exec, the process (and its process group) stops, and when the shell continues it (fg), the program takes the terminal back, draws the whole frame and sends Msg::Resume. Bubble Tea’s tea.Suspend.
task
Runs work on its own thread and sends its result as a message, as Cmd::task does. Go has no tea.Task: a Bubble Tea Cmd is a func() tea.Msg, and tea::task(|| ...) is the equivalent of returning such a function.
tick
Sends message(now) once delay has passed: for animation, polling or timeouts; return another tick from update to keep ticking. Bubble Tea’s tea.Tick. The timer starts when the command runs (in Go, when Tick is called; the difference only shows if a command is built long before it is returned).

Type Aliases§

InputSink
Delivers input to the running program; false once it has stopped.