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
bubblesfeature (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, aprintlnofformat!’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,valuepairs. Bubble Tea’suv.Environ: give one toProgram::with_environmentto 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 withKeyMod::contains. The bits are Go’s, in Go’s order. - Keyboard
Enhancements - 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 inKeyboardEnhancements{ReportEventTypes: true}, chain the method of the same name, as inKeyboardEnhancements::new().report_event_types(true). - Keyboard
Enhancements Report - 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
Modelin a terminal. Bubble Tea’stea.Program. - Progress
Bar - 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,QuitandKill. Sending to a program that has stopped does nothing;Sender::sendsays so by returningfalse. - 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()orView::new, and set the rest with thewith_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§
- Clipboard
Selection - Which clipboard a
Msg::Clipboardcame from: the OSC 52 selection byte, as Bubble Tea’sClipboardSelection. - Color
Profile - How many colours a terminal shows. Bubble Tea’s
colorprofile.Profile, from fewest to most, so profiles compare. - Color
Scheme - A terminal’s colour scheme.
- Cursor
Shape - The cursor’s shape. Bubble Tea’s
CursorShape. - Error
- Why a program stopped without returning its model.
Bubble Tea’s
ErrProgramKilled,ErrInterruptedandErrProgramPanic. - Input
- What a backend reports to the running program: what the terminal
sent, decoded, and signals. Each terminal variant becomes the
Msgof the same name. - KeyCode
- Which key: a character, or a named key. Bubble Tea’s
Key.Code. - Mode
Setting - What a terminal says about a mode. Go’s
ansi.ModeSetting. - Mouse
Button - A mouse button, as X11 numbers them. Bubble Tea’s
MouseButton. - Mouse
Mode - 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. - Progress
BarState - What a progress bar shows. Bubble Tea’s
ProgressBarState. - Reported
Mode - A mode a terminal reported on: a DEC private mode (
CSI ? n $ y) or an ANSI one. - Terminal
Mode - A terminal mode a program can turn on and off. Bubble Tea turns them
on and off from each
View’s fields; aBackendwrites them.
Traits§
- Backend
- A terminal a
Programcan run in. - Exec
Command - Something to run in the terminal with
exec_command: Bubble Tea’stea.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
cmdsat the same time, with no order between their messages. Bubble Tea’stea.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 ofperiodon the system clock: with a period of one second, on the second. Bubble Tea’stea.Every; return it again fromupdateto keep going. - exec
- Runs
commandin the terminal, as Bubble Tea’stea.ExecProcessdoes, then sendsdone(result): for an editor, a pager or a shell. - exec_
command - Runs
commandin the terminal asexecruns aCommand, then sendsdone(result): Bubble Tea’stea.Exec, for anything that needs the terminal to itself. - interrupt
- Stops the program with
Error::Interrupted, askill -INTdoes. Bubble Tea’stea.Interrupt. - println
- Prints
textabove the program’s frame, on lines of its own that stay in the terminal’s scrollback: Bubble Tea’stea.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. Useprintf!to format. - quit
- Stops the program;
Program::runreturns the model. Bubble Tea’stea.Quit. - raw
- Writes
bytesto the terminal as they are, before the next frame: Bubble Tea’stea.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’stea.ReadClipboard. - read_
primary_ clipboard - Asks for the primary selection’s text; the reply is
Msg::Clipboard. Bubble Tea’stea.ReadPrimaryClipboard. - release_
terminal - Hands the terminal over as
execdoes and keeps it handed over, while the program goes on updating the model, untilrestore_terminal. Nothing is drawn meanwhile, and output fromprintln,rawand the like waits. Bubble Tea’sProgram.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). Unlikeclear_screenit 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’stea.RequestBackgroundColor. - request_
capability - Asks for a terminfo capability by name (
XTGETTCAP), such asRGB,TcorMs; the reply isMsg::Capability, and none comes for a capability the terminal does not know. Bubble Tea’stea.RequestCapability. - request_
cursor_ color - Asks for the cursor’s colour; the reply is
Msg::CursorColor. Bubble Tea’stea.RequestCursorColor. - request_
cursor_ position - Asks where the cursor is; the reply is
Msg::CursorPosition. Bubble Tea’stea.RequestCursorPosition. - request_
foreground_ color - Asks for the terminal’s text colour; the reply is
Msg::ForegroundColor. Bubble Tea’stea.RequestForegroundColor. - request_
terminal_ version - Asks for the terminal’s name and version (
XTVERSION); the reply isMsg::TerminalVersion. Bubble Tea’stea.RequestTerminalVersion. - request_
window_ size - Sends the window’s size as
Msg::WindowSize, read from the terminal now. Bubble Tea’stea.RequestWindowSize. - restore_
terminal - Takes the terminal back after
release_terminal: raw mode, every mode on again, the whole frame drawn again. Bubble Tea’sProgram.RestoreTerminal. Does nothing if the terminal is not released. - sequence
- Runs
cmdsone after another: each starts once the one before has sent its message (or, for a batch or sequence, all of its messages). Bubble Tea’stea.Sequence. - set_
clipboard - Copies
textto the system clipboard (OSC 52), where the terminal allows it. Bubble Tea’stea.SetClipboard. - set_
primary_ clipboard - Copies
textto the primary selection (OSC 52). Bubble Tea’stea.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 sendsMsg::Resume. Bubble Tea’stea.Suspend. - task
- Runs
workon its own thread and sends its result as a message, asCmd::taskdoes. Go has notea.Task: a Bubble TeaCmdis afunc() tea.Msg, andtea::task(|| ...)is the equivalent of returning such a function. - tick
- Sends
message(now)oncedelayhas passed: for animation, polling or timeouts; return another tick fromupdateto keep ticking. Bubble Tea’stea.Tick. The timer starts when the command runs (in Go, whenTickis called; the difference only shows if a command is built long before it is returned).
Type Aliases§
- Input
Sink - Delivers input to the running program;
falseonce it has stopped.