term
Terminal output: ANSI colors and styles (16, 256 and 24-bit color), stripping escapes, ANSI-aware width and padding, tables, progress bars, spinners, cursor control, window titles and hyperlinks.
Everything builds strings and is pure, except enabled, println, eprintln and columns.
Those use env to honour NO_COLOR and FORCE_COLOR, and to check whether output is a terminal.
Effects: env (only the four functions above). Deps: text.
fn main(): int uses env {
term.println(term.bold(term.green("✓")) + " build finished") // plain text when piped
term.println(term.paint("warning", term.style().bold().fg(term.Yellow)) + ": 2 unused")
let t = term.table(["package", "size"], [["json", "12 KB"], ["http", "48 KB"]])
.align([term.Left, term.Right])
print(t)
// ╭─────────┬───────╮
// │ package │ size │
// ├─────────┼───────┤
// │ json │ 12 KB │
// │ http │ 48 KB │
// ╰─────────┴───────╯
for i in 0..=50 {
eprint(term.CLEAR_LINE + term.progress("download", i, 50, term.columns()))
}
0
}
API
Colors and styles:
enum Color { Default, Black, Red, Green, Yellow, Blue, Magenta, Cyan, White, BrightBlack … BrightWhite, Idx(n), Rgb(r, g, b) }.Idxis the 256-color palette. Out-of-range values are clamped to 0–255.struct Style { fg, bg, bold, dim, italic, underline, inverse, strike }.style()has no attributes. The chainable setters arefg(st, c),bg(st, c),bold(st),dim,italic,underline,inverseandstrike.paint(s, st):swrapped inst's codes and a reset. A reset insides(from a nestedpaint) turnsstback on, so styles nest.- Shortcuts on strings:
bold(s),dim,italic,underline,inverse,strike,fg(s, c),bg(s, c),black,red,green,yellow,blue,magenta,cyan,white,gray. codes(st): stris the raw SGR sequence, andRESETisESC[0m.color(name): Color!ColorErrtakesred,bright-blue/bright_blue,gray/grey,default,#rrggbb,#rgb, or"0"–"255".message(e)describes aColorErr.
Measuring:
strip(s)removes CSI sequences (colors, cursor moves), OSC sequences (titles, hyperlinks) and two-charESC xescapes.width(s): terminal columns after stripping. CJK and emoji count 2 (viatext.width).pad_right(s, w),pad_left(s, w),center(s, w): these ignore escapes.
Output (uses env):
enabled()/enabled(fd): false ifNO_COLORis non-empty. Otherwise true ifFORCE_COLORis set and isn't"0". Otherwise true iffdis a terminal andTERMisn'tdumb.println(s)/eprintln(s): print to stdout or stderr, stripping escapes when color is off for that stream.columns(): int: the terminal width, else$COLUMNS, else 80.
Cursor and screen (strings to print):
CLEAR_LINE(\rplus erase line),CLEAR_SCREEN,HIDE_CURSOR,SHOW_CURSOR.up(n),down(n),left(n),right(n),goto(row, col)(1-based).title(s)sets the window title.link(label, url)makes an OSC 8 hyperlink; terminals without support show just the label.
Progress:
bar(done, total, w): aw-column bar with eighth-block resolution.doneis clamped, and atotalof 0 gives a full bar.percent(done, total): 0–100.progress(label, done, total, w)giveslabel [bar] 42% (21/50), exactlywcolumns wide. The bar is at least 10 columns, so a very smallwmakes the line wider thanw.spinner(i): frameiof a braille spinner. Anyiworks; it wraps.
Tables:
table(header, rows): Table, then.align([Align])(Left,Right,Center; missing columns default toLeft) and.border(Border).Borderis one ofRounded(the default),Sharp,Ascii,Plain(two-space gaps, no lines) orMarkdown(GFM, with|in cells escaped).render(t)/"{t}"give the lines joined by\n, with no trailing newline.- Cells may contain colors (not counted in widths) and
\n(the cell spans lines). Short rows are padded. An empty header draws no header row.
Not yet: truncating styled text, color downgrading (true color → 256 → 16) for limited terminals, reading keys or raw mode.