tui
lib/tui.vklib/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.
STYLE-BOLD = fn [] i64
STYLE-DIM = fn [] i64
STYLE-UNDERLINE = fn [] i64
STYLE-REVERSE = fn [] i64
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).
use [tui]
c = tui/rgb 16 185 129
if c != 1096065 [ err 'rgb pack mismatch' ] else [ .ok ]
color-slate = fn [] i64
color-surface = fn [] i64
color-cyan = fn [] i64
color-emerald = fn [] i64
color-amber = fn [] i64
color-rose = fn [] i64
alt-screen-on = fn [] str
alt-screen-off = fn [] str
hide-cursor = fn [] str
show-cursor = fn [] str
reset-style = fn [] str
Cell = struct { ch str = ..., fg i64 = ..., bg i64 = ..., style i64 = ... }
One character cell in a double-buffered terminal Screen.
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 { kind enum = ..., ch str = ..., ctrl bool = ..., alt bool = ... }
Decoded keyboard input event returned by parse-key.
new-screen = fn [w i64, h i64] Screen
Allocates a new w x h double-buffered Screen initialized to blank spaces.
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 ]
clear = fn [mut s Screen, fg i64, bg i64]
Clears every cell on s to a space with foreground fg and background bg.
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.
get-cell = fn [s Screen, x i64, y i64] Cell
Reads the cell at (x, y), returning a blank cell if out of bounds.
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.
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.
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.
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 ]
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).
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.
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.
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.
render-plain = fn [s Screen] str
Renders a clean multi-line plain-text snapshot of s (without ANSI codes) for logs and tests.
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.
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 ]