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.
Console is a global variable — no factory call needed. All methods are accessed directly on Console.
Output Methods
Writes text without a newline.
text(string) — text to output
Console.write("Hello, ") Console.write("World!") // Output: Hello, World!
Writes text followed by a newline.
text(string) — text to output
Console.writeLine("Line 1") Console.writeLine("Line 2")
Writes the contents of a Buffer directly to the console. Useful for efficient rendering of pre-composed output.
buffer(Buffer) — buffer containing data to write
var buf = Buffer(256) buf.writeString("Rendered from buffer!") Console.writeBuffer(buf)
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"
Sets the foreground (text) color.
color(string or number) — color name or index (0–15)
Console.setColor("red") Console.writeLine("Red text") Console.setColor(4) // Blue Console.writeLine("Blue text")
Sets the background color.
color(string or number) — color name or index (0–15)
Console.setBackgroundColor("blue") Console.setColor("white") Console.writeLine("White text on blue background")
Sets the foreground color using RGB values.
r(number) — red component (0–255)g(number) — green component (0–255)b(number) — blue component (0–255)
Console.setColorRGB(255, 165, 0) Console.writeLine("Custom orange text")
Sets the background color using RGB values.
r(number) — red component (0–255)g(number) — green component (0–255)b(number) — blue component (0–255)
Console.setBackgroundColorRGB(139, 0, 0) Console.setColor("white") Console.writeLine("White on dark red")
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.
Enables or disables bold text.
enable(boolean) —trueto enable,falseto disable
Enables or disables italic text.
enable(boolean) —trueto enable,falseto disable
Enables or disables underlined text.
enable(boolean) —trueto enable,falseto disable
Enables or disables reverse video (swaps foreground and background).
enable(boolean) —trueto enable,falseto disable
Enables or disables strikethrough text.
enable(boolean) —trueto enable,falseto disable
Enables or disables dim (faint) text.
enable(boolean) —trueto enable,falseto disable
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).
Moves the cursor to an absolute position.
x(number) — column position (0-based)y(number) — row position (0-based)
Console.clear() Console.moveCursor(10, 5) Console.write("This appears at column 10, row 5")
Moves the cursor up by count rows (default: 1).
Moves the cursor down by count rows (default: 1).
Moves the cursor left by count columns (default: 1).
Moves the cursor right by count columns (default: 1).
Hides the cursor. Useful during animations to prevent flickering.
Shows the cursor.
Saves the current cursor position. Restore later with 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
Clears the entire screen and moves cursor to top-left (0, 0).
Clears the entire current line.
Clears from the cursor position to the end of the line.
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).
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.
Switches back to the main screen buffer.
Console.useAltScreen() Console.clear() // Your full-screen app here Console.useMainScreen() // Returns to normal screen
Input Methods
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 + "!")
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.
Checks if input is available without blocking (non-blocking check).
Returns: true if input is available, false otherwise.
while (running) { if (Console.hasInput()) { var key = Console.readChar() // Handle input } sleep(16) // ~60 FPS }
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.
enable(boolean) —trueto enable,falseto disable
Terminal Information
Returns the current terminal width in columns.
Returns the current terminal height in rows.
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
- Windows: Requires Windows 10+ for full ANSI support. Virtual Terminal mode is automatically enabled. Raw mode works with
_getch(). - Unix/Linux/macOS: Full ANSI color support. Terminal settings are saved and restored. Raw mode uses
termios. - All methods work cross-platform. Colors may appear differently depending on terminal theme. Not all terminals support italic or strikethrough.