terminal

Build terminal UIs: query the terminal, switch stdin to raw mode, read individual keystrokes, and generate ANSI escape sequences for color, cursor, and screen control. Import it with import terminal.

State and input

FunctionSignatureDescription
isattyterminal.isatty() → boolWhether stdout is a terminal
sizeterminal.size() → objectTerminal dimensions as {cols, rows} (defaults to 80x24 if unavailable)
rawModeterminal.rawMode() → intSwitch stdin to raw mode (no echo, no line buffering); returns a state handle, or -1 on error
restoreModeterminal.restoreMode(handle: int) → boolRestore the mode saved by rawMode
readKeyterminal.readKey() → stringRead one keystroke; returns a key name (see below) or a single printable character
writeterminal.write(s: string) → boolUnbuffered write to stdout with no trailing newline — use for mid-frame ANSI sequences
flushterminal.flush() → voidFlush pending stdout output

readKey returns names like up, down, left, right, enter, tab, esc, backspace, home, end, pageup, pagedown, insert, delete, f1f12, and ctrl+actrl+z.

Cursor and screen

These return ANSI escape-sequence strings to pass to terminal.write.

FunctionSignatureDescription
clearterminal.clear() → stringClear screen and home the cursor
clearLineterminal.clearLine() → stringClear the entire current line
clearRightterminal.clearRight() → stringClear from cursor to end of line
hideCursorterminal.hideCursor() → stringHide the cursor
showCursorterminal.showCursor() → stringShow the cursor
altScreenterminal.altScreen() → stringEnter the alternate screen buffer
mainScreenterminal.mainScreen() → stringLeave the alternate screen buffer
saveCursorterminal.saveCursor() → stringSave the cursor position
loadCursorterminal.loadCursor() → stringRestore the saved cursor position
moveToterminal.moveTo(row: int, col: int) → stringMove cursor to a 1-indexed position

Styling and color

FunctionSignatureDescription
resetterminal.reset() → stringReset all attributes
boldterminal.bold() → stringBold
dimterminal.dim() → stringDim
italicterminal.italic() → stringItalic
underlineterminal.underline() → stringUnderline
reverseterminal.reverse() → stringReverse video
colorterminal.color(name: string) → stringForeground color by name (red, green, …, brightblue, default) or a "0""255" string for 256-color
bgterminal.bg(name: string) → stringBackground color, same names
rgbterminal.rgb(r: int, g: int, b: int) → string24-bit truecolor foreground
bgRgbterminal.bgRgb(r: int, g: int, b: int) → string24-bit truecolor background

Example

import terminal

// switch to raw mode and read keys until Esc
let st = terminal.rawMode()
terminal.write(terminal.clear())

let running = true
while running {
  let key = terminal.readKey()
  if key == "esc" {
    running = false
  } else {
    // print the key in green at the top-left
    terminal.write(terminal.moveTo(1, 1) + terminal.clearLine())
    terminal.write(terminal.color("green") + "key: " + key + terminal.reset())
    terminal.flush()
  }
}

terminal.restoreMode(st)
Standard library · View as Markdown · llms-full.txt