Skip to content

std/term ​

Terminal raw mode, terminal size, and single-key input. Each OS has its own implementation behind the one import path. Restore the saved state before exiting; std/keys decodes the escape sequences that raw mode lets through.

milo
from "std/term" import { enableRawMode, restoreTerminal, terminalSize }

API reference ​

std/term has one implementation per target (darwin, linux, windows); the resolver picks the one for the target you build for.

TermSize ​

milo
pub struct TermSize

Size of the controlling terminal, in character cells.

Queried on stdout rather than stdin so it still works when input is piped. The ioctl request number is platform-specific and is inlined rather than exposed as a helper, because std/pty already defines a tiocgwinsz in the same flat namespace and importing both would clash.

Falls back to 80x24 when the fd is not a terminal (piped, redirected, or run under a harness) so callers always get a usable frame size.

Fields: rows: i64, cols: i64.

TermState ​

milo
pub struct TermState

macOS arm64: termios is 72 bytes, padded to 80

Fields: saved: [u8; 80].

Functions ​

enableRawMode ​

milo
pub fn enableRawMode(): TermState

enableRawModeBlocking ​

milo
pub fn enableRawModeBlocking(): TermState

Like enableRawMode but VMIN=1: read(0,…) blocks until one byte arrives instead of returning immediately. For an input-driven loop (a line editor) this waits on the keyboard without busy-spinning, which VMIN=0 would force.

getConsoleModeOf ​

milo
pub fn getConsoleModeOf(handle: i64): u32

Only on windows.

rawModeFrom ​

milo
pub fn rawModeFrom(saved: u32): u32

Only on windows.

ENABLE_PROCESSED_INPUT 0x1 | ENABLE_LINE_INPUT 0x2 | ENABLE_ECHO_INPUT 0x4 cleared, ENABLE_VIRTUAL_TERMINAL_INPUT 0x200 set.

readKey ​

milo
pub fn readKey(): i32

read a keypress without blocking; returns KEY_* constant

restoreTerminal ​

milo
pub fn restoreTerminal(state: &TermState): void

stdinHandle ​

milo
pub fn stdinHandle(): i64

Only on windows.

terminalSize ​

milo
pub fn terminalSize(): TermSize

Queried on stdout, matching the POSIX arms, so it still works with piped input. Falls back to 80x24 when stdout is not a console (redirected, or under a harness).