Skip to content

std.term - ZZ

Raw mode, single-key reads, terminal size, and flush. Fallible ops return Result so non-TTY stdin degrades gracefully; is_tty is the total predicate.

import std.term

std.term controls the terminal: raw mode (byte-at-a-time, no echo), single-key reads, size queries, and flush. Raw mode clears ICANON/ECHO (plus ISIG, so Ctrl+C arrives as byte 3 and ZZ code can restore via defer instead of dying with a raw TTY). Non-TTY stdin fails soft with .err("std.term.<op>: not a tty") — pair with is_tty (poll excepted: works on pipes).

import std.term
func main() {
match term.enable_raw() {
.ok(_) => println("raw on"),
.err(e) => println("no tty: {e}"),
}
defer term.disable_raw()
match term.read_key() {
.ok(k) => println("key: {k}"),
.err(e) => println("read failed: {e}"),
}
}
Key Bytes
Up / Down / Right / Left 27,91,65 / 66 / 67 / 68
Enter 13
Esc (lone) 27
Space 32
Backspace 127
Letters ASCII (a=97, q=113)
Ctrl+C 3 (raw mode passes through as byte)

After reading 27 (Esc prefix), poll(50) for the rest: bytes waiting = escape sequence; silence = lone Esc.

  • enable_raw/disable_raw/read_key/poll/get_size return Result<_, str> — .err("not a tty") on non-TTY.
  • is_tty is total (bool).
  • flush is total (unit).
  • Always defer term.disable_raw() after enable_raw — avoids leaving the TTY raw on exit/panic.
  • After reading 27, poll(50) to distinguish Esc from arrow sequences.
  • poll works on pipes — closed stdin reports ready; next read returns EOF.
  • Non-TTY stdin (CI, pipes) fails soft — always pair with is_tty.
import std.term
func read_char() -> Result<int, str> {
if !term.is_tty() {
return .err("std.term.read_char: not a tty")
}
match term.enable_raw() {
.ok(_) => (),
.err(e) => return .err(e),
}
defer term.disable_raw()
return term.read_key()
}

7 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.

Function Signature Description
std.term.disable_raw disable_raw() -> Result<unit, str> Restore saved state (idempotent)
std.term.enable_raw enable_raw() -> Result<unit, str> Save termios, switch stdin raw
std.term.flush flush() -> unit Flush stdout now
std.term.get_size get_size() -> Result<[int], str> Terminal [cols, rows] via TIOCGWINSZ
std.term.is_tty is_tty() -> bool Total predicate for graceful degradation
std.term.poll poll(ms: int) -> Result<bool, str> True when byte ready (<=0 = no wait; works on pipes)
std.term.read_key read_key() -> Result<int, str> Block for one byte 0–255
  • std.colors — ANSI colour helpers that survive a pipeline.

Full index: API reference · Standard library