4.10. Terminal capability probes
The TTY module answers whether a stream is attached to a real terminal, and how wide that terminal is.
This is the gate for anything that redraws itself with a carriage return: a
progress bar written to a pipe, a redirect, or a supervising process is garbage
in the resulting log. Checking TERM is not a substitute — it stays set when
standard output is redirected. daslib/ansi_colors handles styling; this
module answers the capability question underneath it.
All functions and symbols are in “tty” module, use require to get access to it.
require daslib/tty
Example:
require daslib/tty
[export]
def main() {
if (is_stdout_terminal()) {
print("drawing a {terminal_columns()}-column progress bar\n")
} else {
print("output is captured — emitting plain lines instead\n")
}
}
4.10.1. Constants
- TTY_STDIN = 0
TTY_STDIN:int const
- TTY_STDOUT = 1
TTY_STDOUT:int const
- TTY_STDERR = 2
TTY_STDERR:int const
- TTY_DEFAULT_WIDTH = 80
TTY_DEFAULT_WIDTH:int const
4.10.2. Terminal detection
- is_stderr_terminal(): bool
True when standard error is attached to a terminal.
- is_stdin_terminal(): bool
True when standard input is attached to a terminal.
- is_stdout_terminal(): bool
True when standard output is attached to a terminal. This is the gate for a redrawn progress display — false under a pipe, a redirect, or a supervising process.
- is_stream_terminal(fd: int ): bool
True when the standard stream fd (TTY_STDIN / TTY_STDOUT / TTY_STDERR) is attached to a terminal. Any other descriptor is false.
- Arguments:
fd : int
4.10.3. Terminal geometry
- terminal_columns(fallback: int = TTY_DEFAULT_WIDTH ): int
def terminal_columns (fallback: int = TTY_DEFAULT_WIDTH) : int
- Arguments:
fallback : int