minimal agent cli memoria.tools
memoria tea
2 folders · 45 files

cmd
term
cell_test.odin
cell.odin
components_test.odin
cursor_test.odin
cursor.odin
event.odin
filepicker_test.odin
filepicker.odin
frame.odin
GUIDE.md
help.odin
key_test.odin
key.odin
list_test.odin
list.odin
paginator_test.odin
paginator.odin
picker.odin
program.odin
progress_test.odin
progress.odin
README.md
render_test.odin
render.odin
select.odin
signals_unix.odin
signals_windows.odin
spinner_test.odin
spinner.odin
stopwatch.odin
style_test.odin
style.odin
table_test.odin
table.odin
textarea_gutter_test.odin
textarea_mouse_test.odin
textarea.odin
textinput_test.odin
textinput.odin
timer_test.odin
timer.odin
tree_test.odin
tree.odin
viewport_test.odin
viewport.odin
README.md

tea #

A small TUI library for Odin, modeled on the observable behavior of Charm's Bubble Tea / Lip Gloss stack. By default it keeps the frame at the bottom of the terminal and repaints it in place; Options.alt_screen switches to the alternate screen and anchors the frame at the top-left.

Status: v0, working. Tested on macOS; Linux and Windows targets compile. Windows uses VT input/output modes and GetConsoleScreenBufferInfo; the runtime smoke test is wired into the Windows CI workflow.

What is here #

term/term.odin      ANSI output, width, grapheme helpers
term/keys.odin      byte stream -> Key/Paste/Mouse events
term/raw_*.odin     Unix termios / Windows console modes
term/input_*.odin   Unix poll/read / Windows ReadFile/wait
term/size_*.odin    darwin/linux/windows window size
event.odin          Event union
frame.odin          Frame, Widget_View, Cursor
render.odin         cell-diff renderer + line fallback
cell.odin           styled grapheme cells + wide-cell placeholders
program.odin        UI loop, input thread, resize/tick, shutdown
style.odin          Color/Style, SGR, ANSI-aware width/truncate/pad
textinput.odin      single-line editor
textarea.odin       multi-line editor
select.odin         vertical list with a cursor
list.odin           scrollable select
picker.odin         query + filtered select
table.odin          fixed-column grid with a selected row
filepicker.odin     directory listing and navigation
viewport.odin       scrollable line list with tail-follow
spinner.odin        tick-driven frame animation
progress.odin       determinate bar with optional percentage
paginator.odin      page arithmetic and page indicator
timer.odin          tick-driven countdown
stopwatch.odin      tick-driven elapsed time
tree.odin           expandable parent-linked tree
cursor.odin         blinking virtual cursor cell
cmd/smoke           Windows/Unix console smoke test
key.odin            typed key bindings
help.odin           one-line help rendering
cmd/demo/main.odin  text input demo
cmd/gallery/main.odin  all components, Ctrl+N/P to switch

GUIDE.md is the implementation guide this was built from.

Run the demo #

odin build ./cmd/demo -out:tea-demo
./tea-demo

odin build ./cmd/gallery -out:tea-gallery
./tea-gallery

Run the demo on the alternate screen with TEA_ALT_SCREEN=1 ./tea-demo, or with mouse tracking with TEA_MOUSE=1 ./tea-demo.

Demo: type, press Enter to submit, Esc or Ctrl-C to quit. Gallery: Ctrl+n and Ctrl+p cycle through textinput, select, list, picker, textarea, table, filepicker, progress, timer, stopwatch, tree, and cursor; Ctrl+t jumps to the tree and Ctrl+b to the virtual cursor. Up/Down change the progress bar; space/r control the timer and stopwatch. Esc quits. The frame stays at the bottom of the shell; on exit raw mode, bracketed paste, and cursor visibility are restored.

App API #

View_Ctx :: struct {
    width, height: int,
    origin_row:    int, // 1-based absolute top of the last frame
    frame_rows:    int,
}

App :: struct {
    ctx:    rawptr,
    init:   proc(ctx: rawptr, v: View_Ctx),
    update: proc(ctx: rawptr, ev: Event, v: View_Ctx) -> Update_Result,
    view:   proc(ctx: rawptr, v: View_Ctx, f: ^Frame),
}

run(app: App, options := Options{}) -> bool

Options :: struct {
    input_fd:   term.Handle,
    output:     ^os.File,
    tick_ms:    int,
    alt_screen: bool,
    mouse:      bool,
}

Set alt_screen = true to enter ESC[?1049h at startup and leave it with ESC[?1049l on exit. Frame resets then return to the top-left instead of the bottom-left.

Set mouse = true to enable button-event tracking plus SGR coordinates (ESC[?1002h ESC[?1006h); cleanup disables them. Decoded Mouse events carry 0-based absolute cell coordinates, button, action, and modifiers.

Inline mode queries the cursor position (ESC[6n) once at startup and tracks the frame's absolute top row through growth and scrolling; alt_screen uses origin 1. View_Ctx.origin_row is 1-based, so a local mouse row is e.y - (origin_row - 1) and a local column is e.x.

The renderer defaults to cell diffs (Diff_Mode.Cells); set renderer.diff_mode = .Lines to force the line-diff fallback.

update returns {dirty = true} when the frame should be repainted. view appends already-styled lines to the frame and optionally sets f.cursor to a frame-relative Cursor.

Components follow a plain convention instead of an interface:

Widget_View :: struct {
    lines:  []string,
    cursor: Maybe(Cursor), // relative to lines[0]
}

frame_append(&frame, view) adds the lines and offsets the cursor by the widget's origin.

Components #

  • Text_Input — single line, UTF-8, paste, cursor-follow horizontal window.
  • Textarea — multi-line, hard wrap, optional line-number gutter, visual-row navigation, Shift/mouse selection, selection delete/replace, and undo/redo.
  • Select — list cursor, Up/Down/Home/End/Enter.
  • List — a Select with a scrolling window.
  • Picker — query Text_Input + filtered Select; Enter chooses, Tab switches between query and list.
  • Table — fixed columns, header, selected row, cursor-follow scrolling.
  • Filepicker — directory listing; Enter descends or returns a path, Backspace goes to the parent.
  • Viewport — scrollable styled lines; follows the bottom until the user scrolls up.
  • Spinner — frame sequence advanced by Tick events.
  • Progress — determinate bar; width includes the optional percentage.
  • Paginator — page math, bounds, and a one-based page indicator.
  • Timer / Stopwatch — deterministic tick-driven countdown/elapsed time.
  • Tree — parent-linked nodes with expand/collapse and cursor navigation.
  • Cursor_Blink — one-cell virtual cursor; reverse-video highlight blinks while the character stays visible.
  • Binding / help_view — typed key chords and width-clipped help text.

Design rules #

  1. One UI thread owns app state and the renderer.
  2. Only the renderer writes to stdout while a frame is live.
  3. A frame is a list of styled lines, parsed into cells: SGR state, grapheme clusters, and two-column glyphs with zero-width placeholders. The renderer retains the previous cell grid between frames.
  4. Same-height frames diff cells and repaint only changed spans. Growth, shrink, resets, and Diff_Mode.Lines use the line fallback: growth appends rows with real newlines, shrink clears stale rows, first frames paint all.
  5. Inline mode uses relative cursor movement; renderer_reset is the only place that uses absolute positioning, immediately after a full clear.
  6. Styles are self-contained per line (prefix + text + reset).
  7. Widget measurement and cell normalization both use Odin's grapheme iterator (UAX #29 clusters and cluster width); VS16 promotes emoji presentation to two cells.

Tests #

odin test .
odin test ./term

odin build ./cmd/smoke -out:tea-smoke
./tea-smoke --require-console

The renderer tests assert exact escape sequences; the key tests feed byte streams through a pipe; component tests cover editing, wrapping, selection, and filtering.

Windows CI is in .github/workflows/tea-windows.yml: it sets up MSVC and Odin, runs both test suites, builds both demos and the smoke binary, then runs tea-smoke.exe --require-console on windows-latest. The smoke test calls AllocConsole when CI has no console, enters raw mode, queries the size, writes one VT frame, and restores the terminal.

Known v0 limits #

  • The Windows workflow has not been executed from this development environment yet. Ctrl-C/break/close/shutdown are mapped to the quit flag through SetConsoleCtrlHandler.
  • Inline mouse mapping assumes the terminal answers the startup CPR query; if it does not, the renderer falls back to a bottom-anchored frame origin.
  • Cell diffing applies to same-height frames; height changes and resets use the line fallback.
  • Picker filtering is a case-insensitive substring; no fuzzy matching.
  • On Unix, SIGTERM/SIGHUP/SIGINT are handled through an atomic flag polled by the input thread; a pathological partial UTF-8 sequence can delay exit briefly. Windows uses SetConsoleCtrlHandler for the same flag.