Lyricdev
GitHub

std.term

The terminal, driven by ECMA-48 escape sequences.

isTerminal asks whether a standard stream is a terminal, size how wide and high the terminal on the standard output is. A Style is a set of attributes — a foreground and a background Color (the 16, one of the 256, or RGB), bold, dim, italic, underline, inverse, strike — whose Style.apply wraps a text in the SGR sequence and the reset where styles are on (see colorsEnabled). cursorUp and the functions after it return the sequences themselves, to print. Under Windows the runtime switches the console to virtual-terminal processing once, so the same bytes mean the same there. The module has no raw mode.

Examples

Compiles; not run here

import std.io { println };
import std.term { Style };

let warn = Style { fg = .Yellow, bold = true };
println(warn.apply("careful"));

Types

StdStream enum — One of the three standard streams of the process.
Color enum — A terminal color: one of the 16 of ECMA-48 and aixterm, one of the 256, or RGB.
Style struct — A set of attributes to put on a text: colors and SGR flags, all off by default.

Functions

pub fn isTerminal(stream: StdStream): bool

Returns whether the stream is a terminal.

A stream redirected to a pipe or a file, or not open at all, is not one.

pub fn size(): ?(int, int)

Returns the columns and rows of the terminal on the standard output.

Returns null where the standard output is not a terminal.

pub fn colorsEnabled(): bool

Returns whether styles are written out.

NO_COLOR set and not empty says no, else FORCE_COLOR set and not empty says yes, else TERM=dumb says no, else the standard output being a terminal decides. An empty value counts as not set, since std.os can set a variable but not unset it.

pub fn reset(): string

Returns the SGR sequence that turns every style off.

pub fn cursorUp(n: int = 1): string

Returns the sequence that moves the cursor n rows up (CUU).

pub fn cursorDown(n: int = 1): string

Returns the sequence that moves the cursor n rows down (CUD).

pub fn cursorForward(n: int = 1): string

Returns the sequence that moves the cursor n columns right (CUF).

pub fn cursorBack(n: int = 1): string

Returns the sequence that moves the cursor n columns left (CUB).

pub fn cursorTo(row: int, column: int): string

Returns the sequence that moves the cursor to the row and column, both counted from 1 (CUP).

pub fn cursorNextLine(n: int = 1): string

Returns the sequence that moves the cursor to the start of the line n lines down (CNL).

pub fn cursorHide(): string

Returns the sequence that hides the cursor.

pub fn cursorShow(): string

Returns the sequence that shows the cursor again.

pub fn cursorSave(): string

Returns the sequence that saves the cursor's position (SCOSC).

cursorRestore moves the cursor back there. Both are the forms xterm and the Windows console accept.

pub fn cursorRestore(): string

Returns the sequence that moves the cursor to the position cursorSave saved (SCORC).

pub fn clearScreen(): string

Returns the sequence that erases the whole screen and moves the cursor to the top left.

pub fn clearToEnd(): string

Returns the sequence that erases from the cursor to the end of the screen.

pub fn eraseLine(): string

Returns the sequence that erases the whole line and moves the cursor to its start.

pub fn eraseToEndOfLine(): string

Returns the sequence that erases from the cursor to the end of the line.