Small helpers for laying out text in a terminal, written in aowl (.aowl, built with
aowlmony). One module, src/tui.aowl, no dependencies beyond the standard library.
import tui
echo displayWidth("\e[32m日本\e[0m") # 4: escapes take no columns, CJK takes two
for l in wrap("\e[1ma long bold sentence\e[0m", 10): echo l # the bold is kept on every line
var o = boxOpts("notes") # rounded on a UTF-8 terminal, + - | otherwise
o.border = sgr(2) # a dim border
for l in box(@["first line", "second line"], o): echo l
for l in withHint("a line of text", hintText(":help", sgr(2)), terminalWidth()): echo l╭─ notes ─────╮
│ first line │
│ second line │
╰─────────────╯
a line of text ⟶ :help
| proc | what |
|---|---|
displayWidth(s) |
columns s takes: ANSI escapes (CSI, OSC) 0, wide characters (CJK, emoji) 2, combining marks and zero-width characters 0 |
runeWidth(cp) |
columns one code point takes (0, 1 or 2) |
stripAnsi(s) |
s without its escape sequences |
padRight(s, w), padLeft(s, w) |
pad with spaces to w columns, escapes not counted |
truncate(s, w, ellipsis = "…") |
cut to w columns, ending in the ellipsis when cut |
wrap(s, w) |
word wrap to w columns; \n breaks; a style open at a break is closed (\e[0m) and opened again on the next line; a word longer than a line is cut |
box(lines, opts) |
the lines in a box (BoxOpts: title, padding, border style, titleStyle, width (0: fit the text), maxWidth (0: the terminal's width), ascii); too-wide lines are wrapped |
boxOpts(title = "", padding = 1) |
the usual options: fit the text, at most the terminal's width, ASCII when the locale is not UTF-8 |
boxInner(opts) |
the columns a box made with opts leaves for its text, at the most |
terminalWidth(fallback = 80) |
$COLUMNS, else the terminal's own width (ioctl TIOCGWINSZ on stdout, stderr, stdin), else fallback |
ioctlWidth() |
the terminal's width by ioctl, 0 when there is none |
isTty(fd = 1) |
whether a file descriptor is a terminal |
utf8Terminal() |
whether the locale (LC_ALL, then LC_CTYPE, then LANG) is UTF-8 |
hintText(cmd, style = "", ascii = false) |
a hint as shown: ⟶ cmd (-> cmd in ASCII) |
withHint(content, hint, width, right = true, gap = 2) |
the content with the hint at the right edge (or gap spaces after it); when both do not fit in width, the hint goes on its own line |
sgr(code), Reset |
an SGR escape (sgr(2) dim, sgr(1) bold, sgr(36) cyan) and the reset |
| proc | what |
|---|---|
styled(s, style), span(text, style), render(spans), spanWidth(spans) |
text in a style; styled spans joined |
wrapIndent(s, w, indent) |
wrap with a hanging indent (the lines after the first start indent in) |
ioctlSize(), terminalHeight(), liveSize() |
the terminal's columns and rows (liveSize is asked each frame, so a resize is seen without a signal handler) |
enableRaw(fd = 0), disableRaw(), rawMode() |
raw mode by termios: keys one at a time, no echo, Ctrl-C / Ctrl-Z as keys; disableRaw puts back exactly what was there |
AltScreenOn/Off, HideCursor, ShowCursor, ClearScreen, ClearLine, ClearToEnd, moveTo(col, row), cursorLeft/Right(n) |
escapes for the screen and the cursor |
decodeKey(s, i), decodeKeys(s) |
bytes to Keys: arrows, Home/End, PgUp/PgDn, Delete, Insert, F1-F12, Tab, S-Tab, Enter, Backspace, Esc, Ctrl-letters, Alt-keys, xterm modifiers (C-→), UTF-8 characters |
readKeys(ms = -1, fd = 0), waitInput(fd, ms), readBytes(fd), keyEof |
the keys typed within ms (a lone Esc waits 40 ms for the rest of a sequence) |
keyName(k), keysFromNames("→ → End q") |
keys as short names, and back (for scripted runs and tests) |
newCanvas(w, h), put(c, x, y, s, maxW, style), fill, rowText, lines, plainLines |
a grid of styled cells; put keeps a string's own escapes and wide characters |
newScreen(), frame(screen, canvas) |
the escapes that turn the terminal into the canvas, writing only the rows that changed |
newInlineView(), inlineFrame(view, lines, width), inlineClose(view, keep), cursorUp(n) |
a few rows drawn under the cursor in the normal scrollback (no alternate screen) and redrawn in place by moving back up over them; rows are cut so none wraps; closing clears them and leaves keep lines |
rect, splitRows(r, sizes), splitCols(r, sizes), shrink(r) |
layout: fixed sizes and equal shares (0) |
paneOpts(title, border), pane(c, r, lines, opts), drawFrame(c, r, opts), paneLines |
a bordered pane: title, footer, scrolling with "↑ N" / "↓ N more" in its edge, wrapping with the indent kept |
progressBar(w, pos, total, marks, done, todo, ascii), barCol(w, pos, total) |
a bar with a knob and ticks at marks |
spinner(n, ascii) |
frame n of a spinner (braille, or | / - \) |
editLine(prompt, history, words), newLineEdit, feed(edit, key, history, words), editView |
a line editor: ← → Home End (C-a C-e, C-← C-→ by words; Ctrl with an arrow is decoded from xterm's ESC[1;5A..D and rxvt's ESC Oa..d), Backspace Delete C-u C-k C-w, ↑ ↓ through the history as a shell does (past the newest: the line being typed), C-r search, Tab completion from words, C-c drops the line, C-d on an empty line ends; a plain readLine when not a terminal. feed drives it without a terminal |
editLine(..., suggest = fn, color = true), SuggestFn, SuggestSet, SuggestLevel, Suggestion, setSuggestions, suggesting, selected, suggestRows, editFrame, inputPending |
suggestions under the line: fn(line, full) gives levels (depths, shortest first), each a list best first, each a whole line and a short "why". C-← C-→ choose the depth, C-↑ C-↓ the one selected, Tab takes it (the cursor at its end), Esc hides them until the next key; plain ← → ↑ ↓ and Enter are as without them (Enter takes the line as typed; C-← C-→ move by words when nothing is shown). A key waits only for fn(line, false); fn(line, true) is asked when no key waits, and again every 15 ms while its answer says it is not complete (it may stop early on inputPending()). The rows are cut so none wraps, drawn with relative moves only, and cleared (ESC[J) when the list gets shorter or the line is taken |
loadHistory(path, most), saveHistory(path, history, most) |
the history in a file the caller names |
BoxOpts also has dashed (┄ ┆ edges, : in ASCII) and footer (text in the bottom edge).
Styles are passed in as escape strings. The library never decides by itself whether
colour is wanted: pass "" for no style (for example when NO_COLOR is set or the output
is not a terminal).
The width tables cover the common cases (East Asian wide and fullwidth, the emoji blocks, combining diacritics, variation selectors, zero-width joiners and spaces). They are not the full Unicode tables: a rare script may be off by a column.
tests/run.sh
builds tests/ttui.aowl in a scratch copy under $TMPDIR and runs it (each check prints
ok or FAIL, and two controls that must fail show the checks can fail), then checks
terminalWidth inside a pseudo-terminal of 91 columns (script) and isTty on a pipe; and,
in a pseudo-terminal with keys typed at it, raw mode (keys decoded, C-c a key, stty -g the
same before and after) and the line editor (moves, ↑ ↑ ↓ history like a shell, the line being
typed kept, Tab, C-d, the history file across two runs). tests/tsuggest.aowl checks the
suggestions: Ctrl with the arrows decoded, the depth and the selection moved, Tab, Esc, and the
frames drawn over a small fake terminal (no row left behind when the list gets shorter, none
wider than the screen, the cursor back on the line, the bottom of the screen); in a
pseudo-terminal, suggestions taken with Tab, C-→ C-↓ and Esc.
MIT (see LICENSE).