veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

tui

lib/tui.vk

lib/tui.vk Pure-Veko zero-FFI terminal UI (TUI) toolkit over ANSI/VT100 escape sequences: - Double-buffered Screen with 24-bit TrueColor RGB + style bitmask cells - Deterministic diffing render-diff ANSI frame encoder & plain-text render-plain - Box drawing (.single, .double, .rounded, .ascii), progress bars, tables, sparklines, and scrollable menus - CSI / Ctrl / UTF-8 parse-key input decoder over os/stdin bytes Zero extra capabilities required.

signature
STYLE-NONE = fn [] i64

Style bitmask flags for Cell.style.

signature
STYLE-BOLD = fn [] i64
signature
STYLE-DIM = fn [] i64
signature
STYLE-UNDERLINE = fn [] i64
signature
STYLE-REVERSE = fn [] i64
signature
COLOR-DEFAULT = fn [] i64

Default terminal color sentinel (-1).

rgb fn
signature
rgb = fn [r i64, g i64, b i64] i64

Packs an 8-bit RGB triple into a 24-bit i64 color ((r << 16) | (g << 8) | b).

Executable Example
veko
use [tui]
c = tui/rgb 16 185 129
if c != 1096065 [ err 'rgb pack mismatch' ] else [ .ok ]
signature
color-white = fn [] i64

Built-in high-contrast TrueColor palette helpers.

signature
color-slate = fn [] i64
signature
color-surface = fn [] i64
signature
color-cyan = fn [] i64
signature
color-emerald = fn [] i64
signature
color-amber = fn [] i64
signature
color-rose = fn [] i64
esc fn
signature
esc = fn [] str

Returns the ASCII ESC byte (0x1b) as a single-byte string.

csi fn
signature
csi = fn [suffix str] str

Returns an ANSI CSI sequence (ESC [ + suffix).

signature
clear-screen = fn [] str

Terminal screen & cursor control escape sequences.

signature
alt-screen-on = fn [] str
signature
alt-screen-off = fn [] str
signature
hide-cursor = fn [] str
signature
show-cursor = fn [] str
signature
reset-style = fn [] str
Cell struct
signature
Cell = struct { ch str = ..., fg i64 = ..., bg i64 = ..., style i64 = ... }

One character cell in a double-buffered terminal Screen.

Screen struct
signature
Screen = struct { width i64, height i64, cells block[Cell], prev block[Cell], dirty bool = ... }

Double-buffered terminal framebuffer with front (cells) and committed (prev) grids.

KeyEvent struct
signature
KeyEvent = struct { kind enum = ..., ch str = ..., ctrl bool = ..., alt bool = ... }

Decoded keyboard input event returned by parse-key.

signature
new-screen = fn [w i64, h i64] Screen

Allocates a new w x h double-buffered Screen initialized to blank spaces.

Executable Example
veko
use [tui]
mut s = tui/new-screen 20 5
tui/draw-text mut s 2 1 'Veko TUI' (tui/color-cyan) -1 (tui/STYLE-BOLD)
frame1 = tui/render-diff mut s
frame2 = tui/render-diff mut s
if (len frame1) == 0 or (len frame2) != 0 [ err 'diff failed' ] else [ .ok ]
signature
clear = fn [mut s Screen, fg i64, bg i64]

Clears every cell on s to a space with foreground fg and background bg.

signature
set-cell = fn [mut s Screen, x i64, y i64, ch str, fg i64, bg i64, style i64]

Sets one cell at (x, y) if within bounds.

signature
get-cell = fn [s Screen, x i64, y i64] Cell

Reads the cell at (x, y), returning a blank cell if out of bounds.

signature
draw-text = fn [mut s Screen, x i64, y i64, text str, fg i64, bg i64, style i64]

Draws a single-line string starting at (x, y), clipped to the screen width.

signature
fill-rect = fn [mut s Screen, x i64, y i64, w i64, h i64, ch str, fg i64, bg i64, style i64]

Fills a rectangle (x, y, w, h) with character ch.

signature
draw-box = fn [mut s Screen, x i64, y i64, w i64, h i64, title str, border enum, fg i64, bg i64]

Draws a framed box with optional title. Supported border styles: .single, .double, .rounded, .ascii.

Executable Example
veko
use [tui]
mut s = tui/new-screen 16 4
tui/draw-box mut s 0 0 16 4 'Stats' .ascii -1 -1
plain = tui/render-plain s
if not (str/contains? plain 'Stats') [ err 'missing box title' ] else [ .ok ]
signature
draw-progress = fn [mut s Screen, x i64, y i64, w i64, pct i64, fill_fg i64, track_fg i64, bg i64]

Draws a horizontal progress bar of width w filled to pct (clamped 0..100).

signature
draw-sparkline = fn [mut s Screen, x i64, y i64, vals block, fg i64, bg i64]

Draws an inline sparkline across len vals columns using 8-level vertical bar glyphs.

signature
draw-table = fn [mut s Screen, x i64, y i64, headers block, rows block, col_widths block, fg i64, hdr_fg i64, bg i64]

Draws a tabular grid with bold headers and fixed column widths.

signature
draw-menu = fn [mut s Screen, x i64, y i64, w i64, items block, selected i64, fg i64, sel_fg i64, sel_bg i64, bg i64]

Draws a selectable vertical menu list with highlight row selected.

signature
render-diff = fn [mut s Screen] str

Renders only cells modified since the previous render-diff call into a minimal ANSI frame, then commits the front buffer into s.prev. Returns '' if nothing changed.

signature
render-plain = fn [s Screen] str

Renders a clean multi-line plain-text snapshot of s (without ANSI codes) for logs and tests.

signature
parse-key = fn [raw bytes] KeyEvent

Decodes an input byte slice from os/stdin into a structured KeyEvent. Handles ANSI CSI arrows, Home/End/PgUp/PgDn/Delete, Enter, Tab, Backspace, Esc, Ctrl-A..Z, and UTF-8 text.

Executable Example
veko
use [tui]
mut up_raw = bytes/with-cap 3
bytes/push mut up_raw 27
bytes/push mut up_raw 91
bytes/push mut up_raw 65
ev = tui/parse-key up_raw
if ev.kind != .up [ err 'expected .up' ] else [ .ok ]