Console API
Terminal output, ANSI styling, cursor and screen control, and basic input, all exposed as the global Console singleton.
Overview
Console is a singleton that is auto-registered in every script. There is no constructor, and every call is Console.method(...). It covers writing to standard output and standard error, colors and text styles, cursor movement, screen clearing and scrolling, the alternate screen buffer, and line- or byte-level input from standard input.
Console.setColor("green") Console.writeLine("hello, console") Console.reset()
Conventions
- Singleton. Always called as
Console.method(...). There is no constructor; aConsoleis never created. - Return value. Methods return
null. They do not chain.Console.setColor("red").writeLine(...)is a runtime error. Call them as separate statements. - Coordinates. Cursor positions are 1-based:
moveCursor(1, 1)is the top-left cell.getWidth()andgetHeight()return the current terminal size in cells. - Streams. Plain methods (
write,writeLine,writeBuffer,flush) target standard output. The*Errvariants (writeErr,writeLineErr,writeBufferErr,isTTYErr) target standard error. ANSI escapes from styling and cursor methods are written to standard output. - Buffers.
writeBufferandwriteBufferErraccept a Buffer; raw bytes are emitted as-is, with no encoding conversion. - Booleans. Methods that take a flag (
setBold(true),setRawMode(false)) accept a real boolean. - Restore on exit. When the process ends or the
Consoleis finalized, terminal state is restored: SGR is reset, the cursor is shown again, the alternate screen is exited, and raw-mode termios / Windows console mode are restored. - Errors. Bad arguments raise a runtime error in the form
Console.method(...): <reason>(e.g.color must be 0..15).
Console is garbage-collected. A hard kill (SIGKILL) bypasses this. If a script enters raw mode or the alt screen and dies that way, run reset from the shell to recover the terminal.
cmd.exe without VT support) display escape sequences literally.
Capabilities
Returns the current terminal width in columns. Falls back to 80 if the size can't be queried.
Returns the current terminal height in rows. Falls back to 24 if the size can't be queried.
Returns true if standard output is a terminal.
Returns true if standard error is a terminal.
print("size = %nx%n", Console.getWidth(), Console.getHeight()) print("isTTY = %v", Console.isTTY())
Writing
Writes s to stdout with no newline.
Writes s to stdout followed by \n.
Writes s to stderr with no newline.
Writes s to stderr followed by \n.
Writes the raw bytes of a Buffer to stdout. Bytes are emitted as-is, with no encoding conversion.
buf(Buffer) — the buffer whose bytes are written
Writes the raw bytes of a Buffer to stderr.
Flushes pending stdout. Useful before reading input or before sleeping, since a prompt written without a trailing newline may not appear until it is flushed.
Console.write("no newline ") Console.writeLine("then a line") Console.writeLineErr("diagnostics go to stderr") var b = Buffer.fromString("via buffer\n") Console.writeBuffer(b) Console.flush()
Colors & Styles
Every color setter accepts a color in two forms: by index, a number from 0–15 covering the standard 16 ANSI colors, or by name, a string from the table below. The legacy snake_case forms (bright_red, etc.) are also accepted for compatibility.
| Name | Bright variant |
|---|---|
black | brightBlack (alias gray) |
red | brightRed |
green | brightGreen |
yellow | brightYellow |
blue | brightBlue |
magenta | brightMagenta |
cyan | brightCyan |
white | brightWhite |
Sets the foreground color.
c(number or string) — color index (0–15) or a color name
Sets the background color.
c(number or string) — color index (0–15) or a color name
Sets the foreground to a 24-bit truecolor.
r,g,b(number) — color components, each 0–255
Sets the background to a 24-bit truecolor.
r,g,b(number) — color components, each 0–255
Clears all color and style attributes, back to the terminal default.
Enables or disables bold.
Enables or disables dim.
Enables or disables italic.
Enables or disables underline.
Enables or disables reverse video.
Enables or disables strikethrough.
Truecolor and several styles depend on the terminal. On terminals that don't support them, the escape is still sent and is silently ignored or downgraded by the host. Output remains correct, just unstyled.
if (Console.isTTY()) { ... }.
Console.setColorRGB(255, 128, 0) Console.write("orange ") Console.setBold(true) Console.writeLine("and bold") Console.reset() Console.setBackgroundColor("blue") Console.write(" bg ") Console.reset() Console.writeLine("")
Cursor
Moves the cursor to an absolute position. Coordinates are 1-based; moveCursor(1, 1) is the top-left cell.
row(number) — target row, 1-basedcol(number) — target column, 1-based
Moves the cursor up n rows. n defaults to 1.
Moves the cursor down n rows. n defaults to 1.
Moves the cursor left n columns. n defaults to 1.
Moves the cursor right n columns. n defaults to 1.
Hides the cursor. Visibility is restored automatically at finalization.
Shows the cursor.
Saves the current cursor position (DECSC).
Restores a previously saved position (DECRC).
// Draw on the last row, then jump back Console.saveCursorPos() Console.moveCursor(Console.getHeight(), 1) Console.clearLine() Console.write("READY") Console.restoreCursorPos() Console.flush()
Clearing & Scrolling
Clears the entire screen and homes the cursor.
Clears the current line.
Clears from the cursor to the end of the line.
Clears from the start of the line to the cursor.
Scrolls the screen up n lines. n defaults to 1.
Scrolls the screen down n lines. n defaults to 1.
Screen Modes
Switches to the alternate screen buffer, for full-screen TUIs. The previous screen contents are preserved by the terminal.
Returns to the main screen. Also done automatically at finalization if the alternate screen was active.
Sets the terminal window title.
s(string) — the new title
Emits an audible/visual bell by writing \a. Many terminals visualize this as a flash; some do nothing.
Input
Reads one line from stdin with the trailing newline stripped.
Returns: the line as a string, or null on EOF.
Console.flush() before readLine() if a prompt was written without a trailing newline, otherwise the prompt may not appear before the read blocks.
Console.write("name? ") Console.flush() // Show the prompt before blocking var name = Console.readLine() if (name != null) { print("hello, %v", name) }
Reads a single character. In line-buffered mode this still waits for Enter; combine with setRawMode(true) to read keystrokes immediately. Returns one byte. Multi-byte UTF-8 characters and escape sequences (arrow keys, function keys, mouse events) arrive as several bytes, so read repeatedly and parse at the script level when they matter.
Returns: the character as a string, or null on EOF.
Returns true if at least one byte is available on stdin without blocking. It only checks stdin and says nothing about whether stdout is ready to write.
Enables or disables raw mode. In raw mode echo is off and input is delivered byte-by-byte. The mode is restored at finalization regardless.
on(boolean) —trueto enter raw mode,falseto leave it
Examples
TTY-Aware Styling
func status(label, color) { var styled = Console.isTTY() // Only style real terminals if (styled) { Console.setColor(color) Console.setBold(true) } Console.write(label) if (styled) { Console.reset() } Console.writeLine("") } status("PASS", "green") status("FAIL", "brightRed") print("size = %nx%n", Console.getWidth(), Console.getHeight())
Full-Screen Key Loop
Console.useAltScreen() Console.clear() Console.hideCursor() Console.setRawMode(true) Console.moveCursor(1, 1) Console.writeLine("press q to quit") Console.flush() while (true) { var key = Console.readChar() if (key == null or key == "q") { break } Console.moveCursor(2, 1) Console.clearLine() Console.write("key: ") Console.write(key) Console.flush() } // Finalization would restore all of this too; unwinding explicitly is optional Console.setRawMode(false) Console.showCursor() Console.useMainScreen()