From Bubble Tea (Go) to petitcarnets
This guide is for people who know Bubble Tea v2 and want to write the same program in Rust. petitcarnets keeps Bubble Tea's loop, its command and message names, and its key strings, so most of a port is mechanical. The parts that are not are the ones Rust's type system changes: messages are an enum instead of any, a model is changed in place instead of returned, and options are methods instead of functional arguments.
The names below are those of charm.land/bubbletea/v2 v2.1.1, of teatest v2 and of Bubbles v2.2.2, against petitcarnets 0.0.9. Import the crate as tea and the code reads like its Go original:
use petitcarnets as tea;
More of Bubble Tea's own examples, ported one by one, are listed in docs/examples.md.
The idioms
The model is a trait, changed in place
Go's tea.Model is an interface of three methods on a value; Update returns the next model and a command. In Rust, tea::Model is a trait whose update takes &mut self, changes the state where it is, and returns only the command.
.Cmd
.View
;
initis optional (it returnsCmd::none()unless you write one) and takes&mut self, so it can set state as well as start work.- You build the first model yourself and hand it to
Program::new.rungives it back with its own type when the program ends: there is nofinalModel.(model)assertion. - There is no way to return a different model from
update. A program that swaps whole screens keeps them in an enum field of one model.
Messages are a typed enum
Go's tea.Msg is any, read with a type switch. petitcarnets' message is tea::Msg<A>, an enum of everything the runtime sends (keys, mouse, paste, focus, the window size, the terminal's replies) plus one variant, tea::Msg::App(A), for your own messages. A is the trait's associated type Msg: your own enum, or () when you have none.
switch msg.
match msg
- The match is checked by the compiler: a message the model does not expect cannot arrive, and there is no downcasting. You can no longer send a stray
stringas a message, as Go's tests sometimes do. tea::Msgis#[non_exhaustive]: keep a_arm, since more of the terminal's replies arrive as the runtime grows.- A key compares with its Bubble Tea name directly (
k == "ctrl+c",k.matches_any(&["q", "esc"]));k.to_string()is Go'smsg.String(). - The runtime's own control messages (
QuitMsg,SuspendMsg,BatchMsg,ClearScreenMsgand the like) never reach the model: they are commands, handled by the runtime.
Commands are values, often closures
A Go tea.Cmd is func() tea.Msg, run on a goroutine, with nil for nothing. A petitcarnets tea::Cmd<A> is an opaque value:
| Go | petitcarnets |
|---|---|
return m, nil | tea::Cmd::none() |
func() tea.Msg { return fetch() } | tea::task(|| Msg::Fetched(fetch())), or tea::Cmd::task(...) |
return m, tea.Quit | return tea::quit() |
tea.Tick(d, func(t time.Time) tea.Msg { return tickMsg(t) }) | tea::tick(d, Msg::Tick) with Tick(SystemTime) |
tea.Batch(a, b) | tea::batch([a, b]) |
- Go passes most built-in commands as function values (
tea.Quit,tea.ClearScreen,tea.RequestWindowSize); in Rust each is a function you call (tea::quit(),tea::clear_screen(),tea::request_window_size()) that returns the command. - Go has no
tea.Task: a Go command is anyfunc() tea.Msg, andtea::task(|| ...)(the same astea::Cmd::task(|| ...)) is the equivalent of returning such a function. tea::taskruns its closure on a thread of its own and sends what it returns astea::Msg::App. It must return a message: where Go returnsnilfrom a command, return a variant yourupdateignores.tickandeveryare timers on the event loop, not sleeping goroutines: a thousand of them cost no threads, and inTestProgramthey follow the virtual clock. Prefer them to a task that sleeps.batchandsequencetake anything iterable (an array, aVec), and dropCmd::none()s as Go dropsnils.
Returning commands from update
Go returns the model and the command together, return m, cmd. Here update returns the command alone, so an early return tea::quit() ends the arm, and the last expression of the function is the command for every other case. Several commands go out as one tea::batch.
The view is a struct with builders
Go's tea.View is a struct whose fields you set after tea.NewView. petitcarnets' tea::View has the same fields under snake case names, and a with_ builder for each, so a view is usually one expression:
tea
v.AltScreen = true
v.MouseMode = tea.MouseModeCellMotion
v.WindowTitle = "notes"
return v
new
.with_alt_screen
.with_mouse_mode
.with_window_title
A String or &str turns into a view with .into(). The fields are public, so v.alt_screen = true works too, but View is #[non_exhaustive] and cannot be built with a struct literal outside the crate; start from View::new or View::default().
Composing components: Cmd::map and Msg::route
In Go, a parent passes its tea.Msg straight to each child and returns each child's tea.Cmd as it is, because both are untyped. In Rust each component has its own message type, so the parent keeps a variant for each child's messages, maps the child's commands up into its own type with Cmd::map, and routes messages down with Msg::route, which passes the runtime's messages (keys, sizes) through as they are and picks out the child's own:
use petitcarnets as tea;
use ;
Cmd::map keeps quit, interrupt, output and terminal handovers as they are, and maps every message a batch, sequence, task, tick, every or exec sends. A model with a single child can simply use the child's message type as its own (type Msg = textinput::Msg;), as the ported textinput example does, and needs neither.
Components update in place, from a borrowed message
A Bubbles component in Go is a value: m.input, cmd = m.input.Update(msg). In petitcarnets it is a struct with
where X is the component's own message (spinner::Msg, textinput::Msg, ...); a component with no messages of its own (the viewport, the table, the paginator) is generic over X. Because the message is borrowed, one message can go to several children without cloning. Components that tick carry an id, as in Bubbles, so two spinners in one parent each take only their own ticks.
Options are methods
Go's tea.NewProgram(m, tea.WithFPS(30), tea.WithColorProfile(p)) becomes a chain of builder methods on tea::Program:
new
.with_fps
.with_color_profile
.run?;
The accessible mode
Program::with_accessible(true) prints frames as plain lines, for screen readers and braille displays, as huh's WithAccessible does: each line that changed is printed once below what is already there, nothing is redrawn in place, the cursor is not moved and the alternate screen is not used. Without the call, it is on when the environment has a non-empty ACCESSIBLE and the program runs in a real terminal; with_accessible(false) turns it off whatever the environment says. Bubble Tea itself has no such mode. Input works as always, so the input modes (bracketed paste, modifyOtherKeys, the kitty keyboard flags, and mouse or focus reports if the view asks) are still set at the start and reset at the end: output captured to a file holds those few sequences around the plain lines.
Errors
Program.Run in Go returns an error that you compare with tea.ErrInterrupted and the others. run returns Result<M, tea::Error>, an enum: Error::Interrupted, Error::Killed, Error::Panic(message) and Error::Io(err). The terminal is restored in every case.
Every Bubble Tea name
Every exported identifier of charm.land/bubbletea/v2 v2.1.1, as go doc -all lists it, with its petitcarnets counterpart. "Not ported" means there is nothing of that name; the reason says what to do instead.
The model and the program
| Bubble Tea (Go) | petitcarnets |
|---|---|
Model (interface) | tea::Model (trait) |
Model.Init() Cmd | fn init(&mut self) -> Cmd<Self::Msg> (optional) |
Model.Update(Msg) (Model, Cmd) | fn update(&mut self, Msg<Self::Msg>) -> Cmd<Self::Msg> |
Model.View() View | fn view(&self) -> View |
Program | tea::Program<M> |
NewProgram(m, opts...) | tea::Program::new(m), then builder methods |
ProgramOption | not ported: options are methods on Program |
(*Program).Run() | program.run(), returning Result<M, Error>; run_with(backend) for another Backend |
(*Program).Send(msg) | program.sender().send(msg): Sender<A> sends your own messages, from any thread; it never blocks, and returns false once the program has stopped (the message is dropped) |
(*Program).Quit() | sender.quit() |
(*Program).Kill() | sender.kill(); run returns Error::Killed |
(*Program).Wait() | not ported: run blocks its caller; run it on a thread and join it |
(*Program).Println(args...) | not ported: send a message and return tea::println(s) from update |
(*Program).Printf(t, args...) | not ported: as Println, with tea::printf! |
(*Program).ReleaseTerminal() | tea::release_terminal(), a command |
(*Program).RestoreTerminal() | tea::restore_terminal(), a command |
ErrInterrupted | tea::Error::Interrupted |
ErrProgramKilled | tea::Error::Killed |
ErrProgramPanic | tea::Error::Panic(message) |
OpenTTY() | not ported as a function: tea::Terminal::new() reads and writes /dev/tty when standard input or output is not a terminal |
LogToFile(path, prefix) | not ported: the crate has no logging dependency; open a file and write to it yourself |
LogToFileWith(path, prefix, log) | not ported, as LogToFile |
LogOptionsSetter | not ported, as LogToFile |
Program options
| Bubble Tea (Go) | petitcarnets |
|---|---|
WithFPS(n) | .with_fps(n) (1 to 120, 60 by default) |
WithColorProfile(p) | .with_color_profile(p) |
WithEnvironment(env) | .with_environment(env), with tea::Environment |
WithInput(r) | not ported as a reader: run_with(backend) takes a tea::Backend for input and output |
WithOutput(w) | not ported as a writer: as WithInput |
WithContext(ctx) | not ported: keep a Sender and call quit or kill from where the context would be cancelled |
WithFilter(fn) | not ported: filter at the top of update, where the model is to hand |
WithWindowSize(w, h) | not ported: in tests, TestProgram::new(m, w, h) or TestTerminal::new(w, h) sets the size |
WithoutCatchPanics() | not ported: the terminal is always restored on a panic |
WithoutRenderer() | not ported; .with_accessible(true) prints frames as plain lines |
WithoutSignalHandler() | not ported: SIGINT, SIGTERM, SIGTSTP, SIGCONT and SIGWINCH are always handled |
WithoutSignals() | not ported, as WithoutSignalHandler |
| (none) | .with_suspend(false): Ctrl-Z arrives as a key instead of suspending (.with_suspend(true), the default, suspends); .without_suspend() before 0.0.9, removed in 0.0.10 |
| (none) | .with_accessible(on): plain-line output for screen readers |
Commands
| Bubble Tea (Go) | petitcarnets |
|---|---|
Cmd (func() Msg) | tea::Cmd<A>; tea::task(f) (or Cmd::task(f)) for a closure, Cmd::none() for nil |
Batch(cmds...) | tea::batch(cmds) |
BatchMsg | not ported: batches are handled by the runtime and never reach the model |
Sequence(cmds...) | tea::sequence(cmds) |
Tick(d, fn) | tea::tick(d, f); f gets a SystemTime |
Every(d, fn) | tea::every(d, f); fires once, on the next multiple of d, as in Go |
Quit(), QuitMsg | tea::quit(); the model never sees a quit message |
Interrupt(), InterruptMsg | tea::interrupt(); run returns Error::Interrupted |
Suspend(), SuspendMsg | tea::suspend(); Ctrl-Z also suspends by default |
ResumeMsg | tea::Msg::Resume |
ClearScreen() | tea::clear_screen() |
Println(args...) | tea::println(s) |
Printf(template, args...) | tea::printf!(template, args...), a macro |
Raw(r), RawMsg | tea::raw(bytes) |
Exec(c, fn) | tea::exec_command(c, f), for any tea::ExecCommand |
ExecProcess(cmd, fn) | tea::exec(std::process::Command, f); f gets io::Result<ExitStatus>. Note the names: Go's ExecProcess is tea::exec, and Go's Exec is tea::exec_command, after the trait it takes |
ExecCommand (interface) | tea::ExecCommand (trait); only run: the command inherits the process's standard streams |
ExecCallback | not ported as a type: any FnOnce(io::Result<..>) -> A closure |
RequestWindowSize() | tea::request_window_size() |
RequestCursorPosition() | tea::request_cursor_position() |
RequestBackgroundColor() | tea::request_background_color() |
RequestForegroundColor() | tea::request_foreground_color() |
RequestCursorColor() | tea::request_cursor_color() |
RequestTerminalVersion() | tea::request_terminal_version() |
RequestCapability(s) | tea::request_capability(name) |
SetClipboard(s) | tea::set_clipboard(s) |
SetPrimaryClipboard(s) | tea::set_primary_clipboard(s) |
ReadClipboard() | tea::read_clipboard() |
ReadPrimaryClipboard() | tea::read_primary_clipboard() |
RequestProgramStatusSupport() | not ported: the Program Status Protocol (OSC 7501) is not implemented |
| (none) | tea::repaint(): every mode turned off and on, the whole frame drawn again |
Messages
| Bubble Tea (Go) | petitcarnets |
|---|---|
Msg (any) | tea::Msg<A>, an enum; your own messages are tea::Msg::App(a) |
KeyPressMsg | tea::Msg::KeyPress(key) |
KeyReleaseMsg | tea::Msg::KeyRelease(key) |
KeyMsg (interface) | not ported: match KeyPress(k) | KeyRelease(k) |
KeyPressMsg.Key(), KeyReleaseMsg.Key() | the variant holds the tea::Key itself |
KeyPressMsg.String(), .Keystroke() | key.to_string(), key.keystroke() |
MouseClickMsg | tea::Msg::MouseClick(mouse) |
MouseReleaseMsg | tea::Msg::MouseRelease(mouse) |
MouseWheelMsg | tea::Msg::MouseWheel(mouse) |
MouseMotionMsg | tea::Msg::MouseMotion(mouse); its String() is mouse.motion_string() |
MouseMsg (interface) | not ported: match the four mouse variants |
MouseClickMsg.Mouse() and the others | the variant holds the tea::Mouse itself |
MouseClickMsg.String() and the others | mouse.to_string() |
PasteMsg{Content}, PasteMsg.String() | tea::Msg::Paste(text) |
PasteStartMsg, PasteEndMsg | tea::Msg::PasteStart, tea::Msg::PasteEnd |
FocusMsg, BlurMsg | tea::Msg::Focus, tea::Msg::Blur |
WindowSizeMsg{Width, Height} | tea::Msg::WindowSize(tea::Size { width, height }) |
ColorProfileMsg | tea::Msg::ColorProfile(profile) |
EnvMsg, EnvMsg.Getenv, EnvMsg.LookupEnv | not ported as a message: tea::Environment::current(), then .var(name) and .get(name) |
BackgroundColorMsg | tea::Msg::BackgroundColor(rgb) |
ForegroundColorMsg | tea::Msg::ForegroundColor(rgb) |
CursorColorMsg | tea::Msg::CursorColor(rgb) |
.IsDark() on the three colour messages | rgb.is_dark() |
.String() on the three colour messages | not ported: format rgb.r, rgb.g, rgb.b yourself |
CursorPositionMsg{X, Y} | tea::Msg::CursorPosition { x, y } |
TerminalVersionMsg{Name}, .String() | tea::Msg::TerminalVersion(name) |
CapabilityMsg{Content}, .String() | tea::Msg::Capability(content) |
ClipboardMsg{Content, Selection} | tea::Msg::Clipboard { content, selection } |
ClipboardMsg.Clipboard() | u8::from(selection); selection is a tea::ClipboardSelection |
ClipboardMsg.String() | content |
ModeReportMsg{Mode, Value} | tea::Msg::ModeReport { mode, value }, with tea::ReportedMode and tea::ModeSetting |
KeyboardEnhancementsMsg{Flags} | tea::Msg::KeyboardEnhancements(report), a tea::KeyboardEnhancementsReport with flags |
.SupportsKeyDisambiguation() | report.supports_key_disambiguation() |
.SupportsEventTypes() | report.supports_event_types() |
.SupportsAlternateKeys() | report.supports_alternate_keys() |
.SupportsAllKeysAsEscapeCodes() | report.supports_all_keys_as_escape_codes() |
.SupportsAssociatedText() | report.supports_associated_text() |
ProgramStatusSupportMsg | not ported: no Program Status Protocol |
| (ultraviolet events Go passes on) | tea::Msg::PixelSize, CellSize, PrimaryDeviceAttributes, SecondaryDeviceAttributes, TertiaryDeviceAttributes, ColorScheme |
Keys
| Bubble Tea (Go) | petitcarnets |
|---|---|
Key | tea::Key |
Key.Text | key.text |
Key.Mod | key.mods |
Key.Code (a rune) | key.code, a tea::KeyCode: KeyCode::Char('a') or a named key |
Key.ShiftedCode, Key.BaseCode | key.shifted_code, key.base_code (Option<KeyCode>) |
Key.IsRepeat | key.is_repeat |
Key.String() | key.to_string(), or compare: key == "ctrl+c" |
Key.Keystroke() | key.keystroke() |
KeyPressMsg{Code: tea.KeyEnter} (building one) | tea::Key::new(tea::KeyCode::Enter); also Key::char('a'), Key::ctrl('c'), .with_mods(m), .with_text(s), .with_shifted_code(c), .with_base_code(c), .with_repeat(on) (the struct is #[non_exhaustive]) |
KeyMod | tea::KeyMod |
ModShift, ModAlt, ModCtrl, ModMeta | KeyMod::SHIFT, KeyMod::ALT, KeyMod::CTRL, KeyMod::META |
ModHyper, ModSuper | KeyMod::HYPER, KeyMod::SUPER |
ModCapsLock, ModNumLock, ModScrollLock | KeyMod::CAPS_LOCK, KeyMod::NUM_LOCK, KeyMod::SCROLL_LOCK |
KeyUp, KeyDown, KeyRight, KeyLeft | KeyCode::Up, Down, Right, Left |
KeyBegin, KeyFind, KeyInsert, KeyDelete, KeySelect | KeyCode::Begin, Find, Insert, Delete, Select |
KeyPgUp, KeyPgDown, KeyHome, KeyEnd | KeyCode::PgUp, PgDown, Home, End |
KeyKpEnter, KeyKpEqual, KeyKpMultiply, KeyKpPlus, KeyKpComma, KeyKpMinus, KeyKpDecimal, KeyKpDivide | KeyCode::KpEnter, KpEqual, KpMultiply, KpPlus, KpComma, KpMinus, KpDecimal, KpDivide |
KeyKp0 to KeyKp9 | KeyCode::Kp0 to KeyCode::Kp9 |
KeyKpSep, KeyKpUp, KeyKpDown, KeyKpLeft, KeyKpRight, KeyKpPgUp, KeyKpPgDown, KeyKpHome, KeyKpEnd, KeyKpInsert, KeyKpDelete, KeyKpBegin | KeyCode::KpSep, KpUp, KpDown, KpLeft, KpRight, KpPgUp, KpPgDown, KpHome, KpEnd, KpInsert, KpDelete, KpBegin |
KeyF1 to KeyF63 | KeyCode::F(1) to KeyCode::F(63); the decoder never makes another n, and one built by hand prints as f{n} but matches no name |
KeyCapsLock, KeyScrollLock, KeyNumLock, KeyPrintScreen, KeyPause, KeyMenu | KeyCode::CapsLock, ScrollLock, NumLock, PrintScreen, Pause, Menu |
KeyMediaPlay, KeyMediaPause, KeyMediaPlayPause, KeyMediaReverse, KeyMediaStop, KeyMediaFastForward, KeyMediaRewind, KeyMediaNext, KeyMediaPrev, KeyMediaRecord | KeyCode::MediaPlay, MediaPause, MediaPlayPause, MediaReverse, MediaStop, MediaFastForward, MediaRewind, MediaNext, MediaPrev, MediaRecord |
KeyLowerVol, KeyRaiseVol, KeyMute | KeyCode::LowerVol, RaiseVol, Mute |
KeyLeftShift, KeyLeftAlt, KeyLeftCtrl, KeyLeftSuper, KeyLeftHyper, KeyLeftMeta | KeyCode::LeftShift, LeftAlt, LeftCtrl, LeftSuper, LeftHyper, LeftMeta |
KeyRightShift, KeyRightAlt, KeyRightCtrl, KeyRightSuper, KeyRightHyper, KeyRightMeta | KeyCode::RightShift, RightAlt, RightCtrl, RightSuper, RightHyper, RightMeta |
KeyIsoLevel3Shift, KeyIsoLevel5Shift | KeyCode::IsoLevel3Shift, IsoLevel5Shift |
KeyBackspace, KeyTab, KeySpace | KeyCode::Backspace, Tab, Space |
KeyEnter, KeyReturn | KeyCode::Enter (Go's two names are one key) |
KeyEscape, KeyEsc | KeyCode::Esc (likewise) |
KeyExtended | KeyCode::Extended |
Mouse
| Bubble Tea (Go) | petitcarnets |
|---|---|
Mouse{X, Y, Button, Mod} | tea::Mouse::new(x, y, button).with_mods(mods); read mouse.x, .y, .button, .mods (u16 coordinates). The struct is #[non_exhaustive], so build it with new |
Mouse.String() | mouse.to_string() |
MouseButton | tea::MouseButton; button.name() is its Go name |
MouseNone, MouseLeft, MouseMiddle, MouseRight | MouseButton::None, Left, Middle, Right |
MouseWheelUp, MouseWheelDown, MouseWheelLeft, MouseWheelRight | MouseButton::WheelUp, WheelDown, WheelLeft, WheelRight |
MouseBackward, MouseForward, MouseButton10, MouseButton11 | MouseButton::Backward, Forward, Button10, Button11 |
MouseMode | tea::MouseMode |
MouseModeNone, MouseModeCellMotion, MouseModeAllMotion | MouseMode::None, CellMotion, AllMotion |
The view
| Bubble Tea (Go) | petitcarnets |
|---|---|
View | tea::View |
NewView(s) | tea::View::new(s), or s.into() |
(*View).SetContent(s) | view.set_content(s) |
View.Content | view.content |
View.AltScreen | .with_alt_screen(on) |
View.Cursor | .with_cursor(cursor); None (the default) hides it |
View.WindowTitle | .with_window_title(t) |
View.ForegroundColor, View.BackgroundColor | .with_foreground_color(rgb), .with_background_color(rgb) |
View.ProgressBar | .with_progress_bar(bar) |
View.MouseMode | .with_mouse_mode(mode) |
View.ReportFocus | .with_report_focus(on) |
View.DisableBracketedPasteMode | .with_bracketed_paste(false); note the inverted sense: the field view.disable_bracketed_paste_mode is Go's, and with_bracketed_paste(on) sets it to !on |
View.KeyboardEnhancements | .with_keyboard_enhancements(e) |
View.OnMouse | not ported: hit-test mouse messages in update against the state the view was drawn from |
View.ProgramStatus | not ported: no Program Status Protocol (OSC 7501) |
KeyboardEnhancements | tea::KeyboardEnhancements::new() |
.ReportEventTypes, .ReportAlternateKeys, .ReportAllKeysAsEscapeCodes, .ReportAssociatedText | .report_event_types(on), .report_alternate_keys(on), .report_all_keys_as_escape_codes(on), .report_associated_text(on) |
Cursor | tea::Cursor |
NewCursor(x, y) | tea::Cursor::new(x, y) |
Cursor.Position (Position{X, Y}) | cursor.x, cursor.y; there is no separate Position type |
Position | not ported: the cursor has x and y fields |
Cursor.Shape, Cursor.Blink, Cursor.Color | .with_shape(s), .with_blink(on), .with_color(rgb) |
CursorShape | tea::CursorShape |
CursorBlock, CursorUnderline, CursorBar | CursorShape::Block, Underline, Bar |
ProgressBar{State, Value} | tea::ProgressBar::new(state, value); read bar.state, bar.value (a u8, at most 100) |
NewProgressBar(state, value) | tea::ProgressBar::new(state, value) |
ProgressBarState | tea::ProgressBarState |
ProgressBarNone, ProgressBarDefault, ProgressBarError, ProgressBarIndeterminate, ProgressBarWarning | ProgressBarState::None, Default, Error, Indeterminate, Warning |
ProgressBarState.String() | not ported: {:?} gives the variant's name |
ProgramStatus, ProgramStatusKind, ProgramState | not ported: no Program Status Protocol |
ProgramStateIdle, ProgramStateWorking, ProgramStateDone, ProgramStateBlocked, ProgramStateError, ProgramStateClear | not ported, as ProgramState |
ProgramStatusKindPermission, ProgramStatusKindQuestion, ProgramStatusKindAuth | not ported, as ProgramStatusKind |
teatest
| teatest v2 (Go) | petitcarnets |
|---|---|
NewTestModel(t, m, opts...) | tea::testing::TestProgram::new(m, w, h) |
WithInitialTermSize(w, h) | the w, h of TestProgram::new, or TestTerminal::new(w, h) |
WithProgramOptions(opts...) | TestProgram::with_terminal(program, TestTerminal::new(w, h)), with a Program built with its methods |
TestOption, TestModelOptions | not ported: the arguments above |
(*TestModel).Send(msg) | test.send(msg); keys with test.press(key), bytes with test.bytes(b) |
(*TestModel).Type(s) | test.type_text(s) |
(*TestModel).Output() | test.output(), or the emulated screen with test.screen() |
WaitFor(t, r, cond, opts...) | test.wait_for(within, |t| cond) |
WithDuration(d) | the within of wait_for |
WithCheckInterval(d), WaitForOption, WaitingForContext | not ported: the condition is checked after each step of the virtual clock, nothing polls |
| (time passing) | test.advance(d): moves the virtual clock, so ticks fire without waiting |
(*TestModel).Quit() | test.quit() |
(*TestModel).WaitFinished(t, opts...) | test.wait_finished(within), which returns whether it finished |
WithFinalTimeout(d) | the within of wait_finished |
WithTimeoutFn(fn), FinalOpt, FinalOpts | not ported: wait_finished returns false, and the test decides |
(*TestModel).FinalModel(t) | test.final_model(), with the model's own type |
(*TestModel).FinalOutput(t) | test.final_output(), test.final_screen() |
(*TestModel).GetProgram() | not ported: test.sender() for sending from elsewhere |
Program (interface) | not ported, as GetProgram |
RequireEqualOutput(t, out) | assert_golden(name, &escape(test.final_output())), or a screen with assert_golden(name, &test.screen().snapshot()) |
Bubbles
The components live in tea::bubbles, behind the default feature bubbles. Each is a struct named Model in a module named after the Go package.
| Bubbles v2 (Go) | petitcarnets |
|---|---|
key.NewBinding(key.WithKeys("k", "up"), key.WithHelp("↑/k", "up")) | key::Binding::new(&["k", "up"]).with_help("↑/k", "up") |
key.WithDisabled(), b.SetEnabled(on) | .with_disabled(), b.set_enabled(on) |
key.Matches(msg, m.keys.Up) | m.keys.up.matches(&key), or key::matches(&key, &[&up, &down]) |
help.New(), m.help.View(m.keys) | help::Model::new(), self.help.view(&self.keys); the keys implement help::KeyMap |
spinner.New(spinner.WithSpinner(spinner.Dot)) | spinner::Model::new().with_spinner(spinner::Spinner::DOT) |
m.spinner.Tick (as a command) | self.spinner.tick() |
case spinner.TickMsg: | tea::Msg::App(Msg::Spinner(tick)), with spinner::Msg (TickMsg) |
textinput.New(), ti.Focus() | textinput::Model::new(), let cmd = ti.focus(); |
ti.Placeholder = "x", ti.CharLimit = 156, ti.SetWidth(20) | ti.placeholder = "x".into(), ti.char_limit = 156, ti.set_width(20) |
ti.Value(), ti.SetValue(s), ti.Reset() | ti.value(), ti.set_value(s), ti.reset() |
textinput.Blink | textinput::blink() |
textarea.New() | textarea::Model::new() |
viewport.New(viewport.WithWidth(w), viewport.WithHeight(h)) | viewport::Model::new(w, h) |
vp.SetContent(s), vp.GotoBottom() | vp.set_content(s), vp.goto_bottom() |
list.New(items, list.NewDefaultDelegate(), w, h) | list::Model::new(items, list::DefaultDelegate::new(), w, h); items implement list::Item |
l.SelectedItem() | l.selected_item(), an Option<&I> |
table.New(table.WithColumns(c), table.WithRows(r), table.WithFocused(true)) | table::Model::new().with_columns(c).with_rows(r).with_focused(true) |
table.Column{Title, Width} | table::Column::new(title, width) |
progress.New(progress.WithDefaultBlend()) | progress::Model::new().with_default_blend() |
p.SetPercent(0.5) | p.set_percent(0.5), a command for the animation |
paginator.New() | paginator::Model::new() |
timer.New(d), t.Init() | timer::Model::new(d), t.init() |
stopwatch.New(), s.Init() | stopwatch::Model::new(), s.init() |
cursor.New() | cursor::Model::new() |
m.input, cmd = m.input.Update(msg) | let cmd = self.input.update(&msg); (see composing) |
filepicker, tree | not ported |
Lip Gloss
tea::style holds the small part of Lip Gloss v2 that the components use: a Copy value built with chained methods, as in Go. The names follow Lip Gloss's, in snake case. The abbreviations this crate used before 0.0.9 (fg, bg, dim, border_fg) were deprecated in 0.0.9 and removed in 0.0.10.
| Lip Gloss (Go) | petitcarnets |
|---|---|
lipgloss.NewStyle() | tea::style::Style::new() (also Style::default()) |
s.Render(strs...) | s.render(text), one string; s.paint(text) styles one line without the box |
Foreground(c), Background(c) | .foreground(c), .background(c), with tea::style::Color; fg and bg until 0.0.9 |
Bold(on), Faint(on), Italic(on), Underline(on), Reverse(on) | .bold(on), .faint(on), .italic(on), .underline(on), .reverse(on); dim until 0.0.9 |
Inherit(other) | .inherit(other) (colours and emphasis, not the box) |
Padding(v, h), Padding(t, r, b, l), PaddingLeft(n) | .padding(v, h), .padding_sides(t, r, b, l), .padding_left(n) |
Margin(v, h), Margin(t, r, b, l), MarginTop(n) | .margin(v, h), .margin_sides(t, r, b, l), .margin_top(n) |
Border(b), Border(b, top, right, bottom, left) | .border(b), .border(b).border_sides([top, right, bottom, left]) |
NormalBorder(), RoundedBorder(), ThickBorder(), DoubleBorder(), HiddenBorder() | Border::NORMAL, ROUNDED, THICK, DOUBLE, HIDDEN |
BorderForeground(c) | .border_foreground(c); border_fg until 0.0.9 |
Width(n), Height(n) | .width(n), .height(n) |
GetMarginTop(), GetPaddingLeft(), GetPaddingRight() | .get_margin_top(), .get_padding_left(), .get_padding_right() |
GetHorizontalFrameSize(), GetVerticalFrameSize() | .horizontal_frame(), .vertical_frame(); .frame_offset() has no Go counterpart |
lipgloss.Width(s), lipgloss.Height(s) | tea::style::block_width(s), tea::style::height(s); tea::style::width(s) is one line's cells |
JoinHorizontal(pos, blocks...), JoinVertical(pos, blocks...) | join_horizontal(pos, &blocks), join_vertical(pos, &blocks), with Position::TOP, CENTER and the rest, or Position(f) |
lipgloss.Wrap(s, w, breakpoints) | tea::style::wrap(s, w), at spaces only |
ansi.Truncate(s, w, tail), ansi.Cut(s, l, r), ansi.Strip(s) (x/ansi) | truncate_with(s, w, tail) (or truncate(s, w)), cut(s, l, r), strip_ansi(s) |
Strikethrough, Blink, underline styles and colours | not provided |
Align, AlignHorizontal, AlignVertical, MaxWidth, MaxHeight, Inline, TabWidth, Transform | not provided |
PaddingTop/Right/Bottom, MarginLeft/Right/Bottom | not provided as single-side setters: use padding_sides and margin_sides |
BorderTop/Right/Bottom/Left(on), BorderBackground, per-side border colours | not provided: border_sides picks the sides, and one border_foreground colours them all |
Unset*, Get* for colours and emphasis, SetString, String | not provided: a Style is a Copy value that compares with ==; build another instead of unsetting |
Place, PlaceHorizontal, PlaceVertical, ColorWhitespace, UnderlineSpaces, Hyperlink | not provided |
LightDark, Complete, AdaptiveColor, colour downsampling helpers | not provided: pick a colour from Msg::BackgroundColor yourself; output is downsampled to the terminal's ColorProfile |
the table, list and tree subpackages | not provided: tea::bubbles::table and list are the Bubbles components |
A worked example: examples/simple
Bubble Tea's examples/simple counts down from 5, one second at a time, and quits at zero or on q or Ctrl-C. Here it is in Go:
package main
// A simple program that counts down from 5 and then exits.
.Cmd
.View
.Msg
Step 1: the model and its messages
type model int becomes a tuple struct. The program's one message of its own, tickMsg, becomes a variant of an enum, which is the model's Msg type. Go's tickMsg carries a time.Time it never fills in; the Rust variant carries nothing.
use petitcarnets as tea;
use Duration;
;
Step 2: the command
Go's tick is itself a command: a function that sleeps a second on its goroutine and returns the message. The literal port is a task that sleeps on its thread:
That works, but tea::tick says the same thing without a thread: the timer runs on the event loop, and in tests it follows the virtual clock instead of sleeping for real.
Step 3: Init, Update and View
Init returns the first tick. Update's type switch becomes one match: the key arms compare the key with its name, and the tick arm matches tea::Msg::App(Msg::Tick). Each return m, cmd becomes return cmd, and return m, nil becomes tea::Cmd::none(). View formats the same text.
The ctrl+z arm can go. In Bubble Tea a program suspends only if it returns tea.Suspend itself; petitcarnets suspends on Ctrl-Z by default, before update sees the key, and sends tea::Msg::Resume when the shell brings the program back. (A program that wants the key builds its Program with .with_suspend(false), and can then return tea::suspend() from a "ctrl+z" arm as Go does.)
Step 4: main
tea.NewProgram(model(5)).Run() becomes tea::Program::new(Model(5)).run(). log.Fatal(err) prints the error and exits with status 1. petitcarnets has no LogToFile; a program that wants a debug log opens a file and writes to it.
The view is inline, as in Bubble Tea v2: when the program quits, its last frame stays on screen and the prompt starts on the line below.
Step 5: the test
Bubble Tea's examples/simple/main_test.go has a teatest test that is disabled: it sleeps 1.2 seconds of real time and compares raw output, and the comments call it flaky. On TestProgram's virtual clock the same test is exact and takes no time at all:
Go's test also sends "ignored msg", a string, as a message. A petitcarnets model's messages are its own enum, so there is nothing to port: a message of another type does not compile.
With the step 2 task that sleeps, the second test would wait three real seconds; with tea::tick it finishes at once, at exactly three seconds of virtual time.
The whole program, steps 1 to 4 together, is the code above in one file: put it in examples/simple.rs of a crate that depends on petitcarnets and run cargo run --example simple.