Io

functions and utilities for using the terminal and standard input/output


Functions


The Io module provides functionality for terminal input/output operations, cursor manipulation, and console formatting. It offers both basic printing capabilities and advanced terminal controls.

Key Concepts

  • Terminal input/output operations
  • Colored text output
  • Cursor manipulation
  • Terminal information retrieval
  • Multi-line input handling

Basic Usage

Output Operations

; basic printing (with newline)
print "Hello world"
print ["Multiple" "values" "joined"]   ; Multiple values joined

; printing without newline
prints "Loading..."
print "."                             ; Loading....

; print items on separate lines
print.lines ["one" "two" "three"]
; one
; two
; three

Input Operations

; basic input with prompt
name: input "What's your name? "

; single character input
ch: input ø    ; returns single character without echo

; REPL-style input with history
cmd: input.repl ">> "

Colored Output

; using color function
print color #red "Error!"
print color.bold #blue "Important"
print color.underline #green "Success"

; keep color active
prints color.keep #red "This "
prints "and this "
print "all red"

; combining styles
print color.bold.underline #blue "Highlighted"

Cursor Control

; hide/show cursor
cursor false      ; hide cursor
cursor true       ; show cursor

; move cursor to specific position
goto 10 5         ; move to x:10, y:5
print "X"
goto ø 10         ; move to current x, line 10
print "Y"
goto 5 ø          ; move to column 5, current line
print "Z"

; clear screen
clear

Common Patterns

Progress Indicators

; simple progress indicator
loop 1..5 'i [
    prints "."
    pause 500    ; pause for 500ms
]
print ""

; animated spinner
spinner: ["⠋" "⠙" "⠹" "⠸" "⠼" "⠴" "⠦" "⠧" "⠇" "⠏"]
loop 1..20 'i [
    prints "\b"        ; go back one character
    prints spinner\[i % size spinner]
    pause 100
]
print ""

Interactive Menus

clearMenu: function [][
    clear
    cursor false
]

showMenu: function [options][
    selected: 0
    while [true][
        clearMenu
        
        loop options 'opt -> 
            print ["   " opt]
            
        goto 0 selected
        prints "→ "
        
        key: input ø
        case key [
            'w' -> if selected > 0 [dec 'selected]
            's' -> if selected < (size options)-1 [inc 'selected]
            '\r' [
                cursor true
                return selected
            ]
        ]
    ]
]

; usage
choice: showMenu ["Start" "Settings" "Exit"]
print ["Selection:" choice]

Status Updates

; colorful status messages
printStatus: function [kind message][
    case kind [
        "info"    -> print [color #blue "ℹ" message]
        "success" -> print [color #green "✓" message]
        "warning" -> print [color #yellow "⚠" message]
        "error"   -> print [color #red "✗" message]
    ]
]

; usage
printStatus "info" "Loading configuration..."
printStatus "success" "Configuration loaded"
printStatus "warning" "Disk space low"
printStatus "error" "Connection failed"

Tip
When working with colors, remember that not all terminals support all color features. You might want to check terminal capabilities first.

Terminal Information

; get terminal information
print ["Width:" terminal\width]
print ["Height:" terminal\height]