Programs

std.term

ANSI terminal sequences, key-byte decoding, hosted terminal metadata, and raw mode.

When To Use std.term

In Zerolang, use std.term when terminal code needs ANSI output sequences, hosted terminal metadata, nonblocking input reads, or key decoding for bytes already read from input.

Runnable today:

HelperReturnsNotes
std.term.reset()StringANSI SGR reset.
std.term.bold()StringANSI SGR bold style.
std.term.dim()StringANSI SGR dim style.
std.term.underline()StringANSI SGR underline style.
std.term.inverse()StringANSI SGR inverse style.
std.term.fgDefault()StringReset foreground color.
std.term.fgBlack()StringBlack foreground color.
std.term.fgRed()StringRed foreground color.
std.term.fgGreen()StringGreen foreground color.
std.term.fgYellow()StringYellow foreground color.
std.term.fgBlue()StringBlue foreground color.
std.term.fgMagenta()StringMagenta foreground color.
std.term.fgCyan()StringCyan foreground color.
std.term.fgWhite()StringWhite foreground color.
std.term.bgDefault()StringReset background color.
std.term.bgBlack()StringBlack background color.
std.term.bgRed()StringRed background color.
std.term.bgGreen()StringGreen background color.
std.term.bgYellow()StringYellow background color.
std.term.bgBlue()StringBlue background color.
std.term.bgMagenta()StringMagenta background color.
std.term.bgCyan()StringCyan background color.
std.term.bgWhite()StringWhite background color.
std.term.clearScreen()StringClear the full terminal screen.
std.term.clearScreenDown()StringClear from the cursor through the end of the screen.
std.term.clearScreenUp()StringClear from the cursor through the start of the screen.
std.term.clearLine()StringClear the current terminal line.
std.term.clearLineRight()StringClear from the cursor through the end of the line.
std.term.clearLineLeft()StringClear from the cursor through the start of the line.
std.term.cursorHome()StringMove the cursor to row 1, column 1.
std.term.cursorTo(buffer, row, column)Maybe<Span<u8>>Writes a 1-based ANSI cursor-position sequence into caller storage.
std.term.cursorUp(buffer, count)Maybe<Span<u8>>Writes an ANSI cursor-up sequence into caller storage; count 0 writes an empty span.
std.term.cursorDown(buffer, count)Maybe<Span<u8>>Writes an ANSI cursor-down sequence into caller storage; count 0 writes an empty span.
std.term.cursorRight(buffer, count)Maybe<Span<u8>>Writes an ANSI cursor-right sequence into caller storage; count 0 writes an empty span.
std.term.cursorLeft(buffer, count)Maybe<Span<u8>>Writes an ANSI cursor-left sequence into caller storage; count 0 writes an empty span.
std.term.saveCursor()StringSave the current cursor position.
std.term.restoreCursor()StringRestore the saved cursor position.
std.term.hideCursor()StringHide the terminal cursor.
std.term.showCursor()StringShow the terminal cursor.
std.term.enterAltScreen()StringEnter the alternate screen buffer.
std.term.leaveAltScreen()StringLeave the alternate screen buffer.
std.term.enterBracketedPaste()StringEnable bracketed paste markers in supporting terminals.
std.term.leaveBracketedPaste()StringDisable bracketed paste markers in supporting terminals.
std.term.enterMouseCapture()StringEnable SGR mouse tracking and drag/wheel capture in supporting terminals.
std.term.leaveMouseCapture()StringDisable the SGR mouse tracking modes enabled by enterMouseCapture.
std.term.keyNone()u32Sentinel returned for incomplete or unsupported key bytes.
std.term.keyEscape()u32Escape key code.
std.term.keyEnter()u32Enter key code for \r or \n.
std.term.keyTab()u32Tab key code.
std.term.keyBackspace()u32Backspace key code for 0x7f or 0x08.
std.term.keyCtrlA()u32Ctrl-A key code.
std.term.keyCtrlC()u32Ctrl-C key code.
std.term.keyCtrlD()u32Ctrl-D key code.
std.term.keyCtrlE()u32Ctrl-E key code.
std.term.keyCtrlK()u32Ctrl-K key code.
std.term.keyCtrlL()u32Ctrl-L key code.
std.term.keyCtrlN()u32Ctrl-N key code.
std.term.keyCtrlP()u32Ctrl-P key code.
std.term.keyCtrlR()u32Ctrl-R key code.
std.term.keyCtrlU()u32Ctrl-U key code.
std.term.keyCtrlW()u32Ctrl-W key code.
std.term.keyArrowUp()u32Up-arrow key code above the Unicode scalar range.
std.term.keyArrowDown()u32Down-arrow key code above the Unicode scalar range.
std.term.keyArrowRight()u32Right-arrow key code above the Unicode scalar range.
std.term.keyArrowLeft()u32Left-arrow key code above the Unicode scalar range.
std.term.keyDelete()u32Delete key code above the Unicode scalar range.
std.term.keyHome()u32Home key code above the Unicode scalar range.
std.term.keyEnd()u32End key code above the Unicode scalar range.
std.term.keyPageUp()u32Page Up key code above the Unicode scalar range.
std.term.keyPageDown()u32Page Down key code above the Unicode scalar range.
std.term.keyInsert()u32Insert key code above the Unicode scalar range.
std.term.keyShiftTab()u32Shift-Tab key code above the Unicode scalar range.
std.term.keyF1()u32F1 key code above the Unicode scalar range.
std.term.keyF2()u32F2 key code above the Unicode scalar range.
std.term.keyF3()u32F3 key code above the Unicode scalar range.
std.term.keyF4()u32F4 key code above the Unicode scalar range.
std.term.keyF5()u32F5 key code above the Unicode scalar range.
std.term.keyF6()u32F6 key code above the Unicode scalar range.
std.term.keyF7()u32F7 key code above the Unicode scalar range.
std.term.keyF8()u32F8 key code above the Unicode scalar range.
std.term.keyF9()u32F9 key code above the Unicode scalar range.
std.term.keyF10()u32F10 key code above the Unicode scalar range.
std.term.keyF11()u32F11 key code above the Unicode scalar range.
std.term.keyF12()u32F12 key code above the Unicode scalar range.
std.term.keyPasteStart()u32Bracketed paste start marker code above the Unicode scalar range.
std.term.keyPasteEnd()u32Bracketed paste end marker code above the Unicode scalar range.
std.term.keyCode(bytes)u32Decodes one key from caller-provided bytes, returning Unicode scalar values for printable UTF-8 and named constants for control keys.
std.term.keyByteLen(bytes)usizeReturns the decoded key width in bytes, or 0 for incomplete or unsupported input.
std.term.stdinIsTty()BoolReports whether standard input is attached to a terminal.
std.term.stdoutIsTty()BoolReports whether standard output is attached to a terminal.
std.term.widthOr(fallback)usizeReturns terminal columns, or fallback when unavailable.
std.term.heightOr(fallback)usizeReturns terminal rows, or fallback when unavailable.
std.term.enterRawMode()BoolPuts standard input into raw, nonblocking terminal mode when supported.
std.term.leaveRawMode()BoolRestores the terminal mode saved by enterRawMode().
std.term.readInput(buffer)Maybe<usize>Fills the caller buffer with currently available stdin bytes without blocking; returns null when no bytes are available or the input source is unsupported.

Metadata labels:

  • effects: ANSI/key helpers are pure; TTY/size helpers read hosted terminal metadata; raw-mode helpers update the hosted terminal; readInput reads from hosted stdin
  • allocation behavior: no allocation
  • target support: ANSI/key helpers are target-neutral; TTY/size/raw-mode/input helpers require hosted runtime support
  • error behavior: ANSI/key helpers are infallible; hosted helpers return fallbacks, false, or null when unavailable
  • ownership notes: ANSI sequences are borrowed static byte views
  • example: conformance/native/pass/std-term-ansi.graph

Example:

pub fn main(world: World) -> Void raises {    check world.out.write(std.term.enterAltScreen())    check world.out.write(std.term.enterMouseCapture())    check world.out.write(std.term.clearScreen())    check world.out.write(std.term.cursorHome())    var cursor: [24]u8 = [0_u8; 24]    let top: Maybe<Span<u8>> = std.term.cursorTo(cursor, 1_usize, 1_usize)    if top.has {        check world.out.write(top.value)    }    check world.out.write(std.term.bold())    check world.out.write(std.term.fgCyan())    let width: usize = std.term.widthOr(80_usize)    let height: usize = std.term.heightOr(24_usize)    let raw: Bool = std.term.enterRawMode()    var input: [16]u8 = [0_u8; 16]    let pending: Maybe<usize> = std.term.readInput(input)    if pending.has {        let bytes: Span<u8> = std.mem.prefix(input, pending.value)        let key: u32 = std.term.keyCode(bytes)        if key == std.term.keyCtrlC() {            check world.out.write("cancel")        }    }    check world.out.write("ready")    if raw {        let restored: Bool = std.term.leaveRawMode()        if !restored {            return        }    }    check world.out.write(std.term.reset())    check world.out.write(std.term.leaveMouseCapture())    check world.out.write(std.term.leaveAltScreen())}

Key decoding is target-neutral: it parses bytes the caller already has. TTY and size helpers are hosted metadata calls and return caller fallbacks when a terminal size is unavailable. Raw mode is a hosted terminal capability: call leaveRawMode() before returning to normal line-oriented terminal input. readInput() is nonblocking; in raw mode it can be polled by interactive programs, and on noninteractive stdin it returns available piped bytes when the host exposes them.