Skip to content

io

let io = import("io")

io covers the console: writing to stdout and stderr, flushing, and reading stdin.

The globals print, write, and printf are the convenience path and flush on every call. io is the lower-level set, where io.write buffers and you decide when to flush.

Function Returns Description
write(...) none Write the arguments with no separators and no newline; buffered
flush() none Push the buffered stdout out
eprint(...) none Write to stderr, no separators and no newline; written immediately
let io = import("io")
io.write("a", "b", "c") # "abc", no separator, no newline yet
io.eprint("warning\n") # goes to stderr right away
io.flush() # stdout now shows "abc"

io.write buffers, so output reaches the terminal when you call io.flush() or when the process exits. Reach for it when writing a lot, or when drawing a live line, and flush when the piece is ready. io.eprint is unbuffered, so diagnostics appear immediately. The global write(...) mirrors io.write but flushes on every call.

Function Returns Description
read_line() string? One line from stdin, or none at end of input
read_all() string All of stdin up to end of input
read_bytes(count: int?) bytes Up to count bytes, or all of stdin if count is omitted
let io = import("io")
let line = io.read_line()
while line != none {
print(line)
line = io.read_line()
}

read_line returns none at end of input instead of failing, so it works in read-until-end loops. This is the difference from the global input, which reads a line but panics when input ends.

There is no dedicated terminal UI API. In-place updates are done with io.write (or write) and the usual cursor control sequences: \r returns to the start of the line, \e[K erases to the end of the line, and \e[<n>A moves the cursor up n lines.

let io = import("io")
let os = import("os")
loop {
let line = format("time = {:.2f}", os.time())
io.write("\r" + line + "\e[K")
io.flush()
os.sleep(100)
}