Console API

Professional-grade terminal manipulation for creating TUIs, terminal games, animations, and interactive CLI applications.

Overview

The Console API provides rich terminal control with cross-platform support (Windows, Linux, macOS). Features include 16 ANSI colors plus full RGB, text styling, precise cursor control, screen manipulation, raw input mode, non-blocking input detection, and buffer integration.

Singleton. Console is a global variable — no factory call needed. All methods are accessed directly on Console.

Output Methods

Console.write(text)

Writes text without a newline.

Console.write("Hello, ")
Console.write("World!")
// Output: Hello, World!
Console.writeLine(text)

Writes text followed by a newline.

Console.writeLine("Line 1")
Console.writeLine("Line 2")
Console.writeBuffer(buffer)

Writes the contents of a Buffer directly to the console. Useful for efficient rendering of pre-composed output.

var buf = Buffer(256)
buf.writeString("Rendered from buffer!")
Console.writeBuffer(buf)
Console.flush()

Forces any buffered output to be written immediately.

Console.write("Loading")
Console.flush()  // Ensure it appears immediately
sleep(1000)
Console.write("...")
Console.flush()

Colors

The Console API supports both named ANSI colors (0–15) and full RGB colors.

Named Colors

Standard (0–7): "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"

Bright (8–15): "bright_black" / "gray", "bright_red", "bright_green", "bright_yellow", "bright_blue", "bright_magenta", "bright_cyan", "bright_white"

Console.setColor(color)

Sets the foreground (text) color.

Console.setColor("red")
Console.writeLine("Red text")

Console.setColor(4)  // Blue
Console.writeLine("Blue text")
Console.setBackgroundColor(color)

Sets the background color.

Console.setBackgroundColor("blue")
Console.setColor("white")
Console.writeLine("White text on blue background")
Console.setColorRGB(r, g, b)

Sets the foreground color using RGB values.

Console.setColorRGB(255, 165, 0)
Console.writeLine("Custom orange text")
Console.setBackgroundColorRGB(r, g, b)

Sets the background color using RGB values.

Console.setBackgroundColorRGB(139, 0, 0)
Console.setColor("white")
Console.writeLine("White on dark red")
Console.reset()

Resets all colors and text styles to default.

Console.setColor("red")
Console.setBold(true)
Console.writeLine("Bold red text")
Console.reset()
Console.writeLine("Normal text")

Text Styling

All styling methods accept a boolean to enable or disable the style.

Console.setBold(enable)

Enables or disables bold text.

Console.setItalic(enable)

Enables or disables italic text.

Console.setUnderline(enable)

Enables or disables underlined text.

Console.setReverse(enable)

Enables or disables reverse video (swaps foreground and background).

Console.setStrikethrough(enable)

Enables or disables strikethrough text.

Console.setDim(enable)

Enables or disables dim (faint) text.

combining styles
Console.setColor("cyan")
Console.setBold(true)
Console.setUnderline(true)
Console.writeLine("Bold, underlined cyan text")
Console.reset()

Cursor Control

The console uses 0-based indexing for cursor positions (top-left is 0, 0).

Console.moveCursor(x, y)

Moves the cursor to an absolute position.

Console.clear()
Console.moveCursor(10, 5)
Console.write("This appears at column 10, row 5")
Console.moveCursorUp(count?)

Moves the cursor up by count rows (default: 1).

Console.moveCursorDown(count?)

Moves the cursor down by count rows (default: 1).

Console.moveCursorLeft(count?)

Moves the cursor left by count columns (default: 1).

Console.moveCursorRight(count?)

Moves the cursor right by count columns (default: 1).

Console.hideCursor()

Hides the cursor. Useful during animations to prevent flickering.

Console.showCursor()

Shows the cursor.

Console.saveCursorPos()

Saves the current cursor position. Restore later with restoreCursorPos().

Console.restoreCursorPos()

Restores the cursor position saved by saveCursorPos().

Console.saveCursorPos()
Console.moveCursor(0, 0)
Console.write("Top-left corner")
Console.restoreCursorPos()
Console.write("Back to saved position")

Screen Manipulation

Console.clear()

Clears the entire screen and moves cursor to top-left (0, 0).

Console.clearLine()

Clears the entire current line.

Console.clearToEndOfLine()

Clears from the cursor position to the end of the line.

Console.clearToStartOfLine()

Clears from the start of the line to the cursor position.

Scrolls the screen content up by count lines (default: 1).

Scrolls the screen content down by count lines (default: 1).

Console.useAltScreen()

Switches to the alternate screen buffer. The alternate buffer is a separate display that doesn't affect the main terminal scrollback. Perfect for full-screen applications.

Console.useMainScreen()

Switches back to the main screen buffer.

Console.useAltScreen()
Console.clear()
// Your full-screen app here
Console.useMainScreen()  // Returns to normal screen

Input Methods

Console.readLine()

Reads a line of text from stdin (blocks until Enter is pressed).

Returns: String containing the input (without newline), or null on EOF.

Console.write("Enter your name: ")
Console.flush()
var name = Console.readLine()
Console.writeLine("Hello, " + name + "!")
Console.readChar()

Reads a single character from stdin.

Returns: String containing one character, or null on EOF. On Unix, may require Enter unless raw mode is enabled.

Console.hasInput()

Checks if input is available without blocking (non-blocking check).

Returns: true if input is available, false otherwise.

game loop
while (running) {
    if (Console.hasInput()) {
        var key = Console.readChar()
        // Handle input
    }
    sleep(16)  // ~60 FPS
}
Console.setRawMode(enable)

Enables or disables raw input mode. In raw mode characters are available immediately (no Enter needed) and input is not echoed. Perfect for games and real-time applications.

Terminal Information

Console.getWidth()

Returns the current terminal width in columns.

Console.getHeight()

Returns the current terminal height in rows.

responsive layout
var width = Console.getWidth()
var height = Console.getHeight()
var text = "Centered Text"
var x = (width - length(text)) / 2
var y = height / 2
Console.clear()
Console.moveCursor(x, y)
Console.write(text)

Examples

Colored Menu

func showMenu() {
    Console.clear()
    Console.setColor("bright_cyan")
    Console.setBold(true)
    Console.writeLine("=== Main Menu ===")
    Console.reset()
    Console.writeLine("")

    Console.setColor("green")
    Console.write("1. ")
    Console.reset()
    Console.writeLine("Start Game")

    Console.setColor("green")
    Console.write("2. ")
    Console.reset()
    Console.writeLine("Settings")

    Console.setColor("green")
    Console.write("3. ")
    Console.reset()
    Console.writeLine("Exit")

    Console.writeLine("")
    Console.write("Select an option: ")
    Console.flush()
}

showMenu()
var choice = Console.readLine()

Progress Bar

func showProgress(percent, width) {
    var filled = (percent * width) / 100
    var empty = width - filled

    Console.write("[")
    Console.setColor("green")
    Console.setBold(true)
    var i = 0
    while (i < filled) {
        Console.write("█")
        i = i + 1
    }
    Console.reset()
    Console.setDim(true)
    i = 0
    while (i < empty) {
        Console.write("░")
        i = i + 1
    }
    Console.reset()
    Console.write("] " + str(percent) + "%")
    Console.flush()
}

Console.clear()
Console.writeLine("Downloading...")
Console.writeLine("")

var progress = 0
while (progress <= 100) {
    Console.moveCursor(0, 2)
    showProgress(progress, 50)
    sleep(50)
    progress = progress + 2
}

Console.writeLine("")
Console.setColor("bright_green")
Console.writeLine("Complete!")
Console.reset()

Colored Logging System

func logInfo(message) {
    Console.setColor("bright_blue")
    Console.write("[INFO] ")
    Console.reset()
    Console.writeLine(message)
}

func logError(message) {
    Console.setColor("bright_red")
    Console.setBold(true)
    Console.write("[ERROR] ")
    Console.reset()
    Console.writeLine(message)
}

logInfo("Starting application...")
logError("Failed to connect to database")

Platform Notes