Module term

term
Stability: unstable — a small facade (`is_terminal`, `size`, `supports_color`, `enter_raw_mode`, `restore_mode`) whose names are settled, over a `std/sys/tty` that is not. Two things are open. `enter_raw_mode` / `restore_mode` are a PAIR the caller must balance by hand, and a process that dies between them leaves the terminal in raw mode — the shape that fixes it is a guard value with a `Dispose`, the way `Semaphore.with_permit` and `Mutex.with_lock` already work in `std/sync`. And `supports_color` answers a bool from `TERM`/`NO_COLOR` heuristics, where callers increasingly want a LEVEL (none / 16 / 256 / truecolor). Both are additive to add and breaking to change afterwards, so they belong before the freeze rather than after. — stable modules only change additively; this one may still change.

Terminal facade: TTY detection, terminal size, color-support detection and raw mode for the standard streams — the typed, non-sys face of std/sys/tty (which stays fd- and errno-oriented). std/cli and interactive tools consume this module.

Stability

unstable — a small facade (is_terminal, size, supports_color, enter_raw_mode, restore_mode) whose names are settled, over a std/sys/tty that is not. Two things are open. enter_raw_mode / restore_mode are a PAIR the caller must balance by hand, and a process that dies between them leaves the terminal in raw mode — the shape that fixes it is a guard value with a Dispose, the way Semaphore.with_permit and Mutex.with_lock already work in std/sync. And supports_color answers a bool from TERM/NO_COLOR heuristics, where callers increasingly want a LEVEL (none / 16 / 256 / truecolor).

Both are additive to add and breaking to change afterwards, so they belong before the freeze rather than after.

Types

Stream enum
Stream

One of the process's three standard streams.

Variants

VariantFieldsDescription
Stdin
Stdout
Stderr
TermSize struct
TermSize

Terminal dimensions in character cells.

Fields

NameTypeDescription
columnsusize

Width in columns.

rowsusize

Height in rows.

Functions

is_terminal function
fn(s : Stream) -> bool

True when s is connected to a terminal (false for pipes, files and redirections — including every yo test child, whose streams are pipes).

Parameters

NameTypeNotes
sStream

Returns: bool

size_of function
fn(s : Stream) -> Option(TermSize)

The terminal size of s, or .None when s is not a terminal (or the size is unavailable).

Parameters

NameTypeNotes
sStream

Returns: Option(TermSize)

size function
fn() -> Option(TermSize)

The terminal size of stdout, or .None when stdout is not a terminal.

Returns: Option(TermSize)

supports_color function
fn(s : Stream) -> bool

True when it is reasonable to emit ANSI colors on s: the stream is a terminal, NO_COLOR is unset (https://no-color.org — any value disables), and TERM is not dumb.

Parameters

NameTypeNotes
sStream

Returns: bool

enter_raw_mode function
fn(s : Stream, exn : Exception) -> unit

Put the terminal for s into RAW mode (no echo, byte-at-a-time input). Throws IoError when s is not a terminal or the mode change fails. ALWAYS pair with restore_mode() — a process exiting in raw mode leaves the user's shell unreadable.

Parameters

NameTypeNotes
sStream
exnException

Returns: unit

restore_mode function
fn() -> unit

Restore the terminal mode saved by the first enter_raw_mode call. Safe to call when raw mode was never entered.

Returns: unit