Skip to content
Draft

io

The io module provides console I/O and stream operations.

import io
import { write, read_line, print } from io

Write a string to stdout without a newline:

@io#write("Hello")
@io#write(" World")
@// Output: Hello World

Write a string to stdout with a newline:

@io#write_line("Hello, World!")
@// Output: Hello, World!\n

Print any value with automatic conversion:

@io#print(42)
@io#print("hello")
@io#print([1, 2, 3])

Print any value with a newline:

@io#print_line("The answer is " + string(42))

Write to stderr:

@io#ewrite("Error: something went wrong")

Print to stderr with newline:

@io#eprint_line("Warning: deprecated API")

Read a line from stdin:

@io#write("Enter your name: ")
@name = @io#read_line()
@io#write("Hello, " + @name)

Read a single character:

@char = @io#read_char()

Read all input until EOF:

@content = @io#read_all()

Read an integer from stdin:

@io#write("Enter a number: ")
@num = @io#read_int()

Read a float from stdin:

@io#write("Enter a price: ")
@price = @io#read_float()

Create formatted strings:

@message = @io#format("Hello, {0}! You are {1}.", "Alice", 30)
@// "Hello, Alice! You are 30."

Print formatted output:

@io#printf("Name: {0}, Age: {1}", "Alice", 30)
@input = @io#InputStream::new("file.txt")
@line = @input#read_line()
@input#close()
@output = @io#OutputStream::new("output.txt")
@output#write("Hello, World!")
@output#close()
@stream = @io#BufferedStream::new("data.txt")
@chunk = @stream#read(1024)
@stream#write("data")
@stream#flush()
@stream#close()

Read entire file as string:

@content = @io#read_file("example.txt")

Write string to file:

@io#write_file("output.txt", "Hello, World!")

Append to file:

@io#append_file("log.txt", "New entry\n")

Clear the terminal:

@io#clear_screen()

Set cursor position:

@io#set_cursor(10, 5)

Hide the cursor:

@io#hide_cursor()

Show the cursor:

@io#show_cursor()

Set text color:

@io#set_foreground(@io#RED)
@io#write("Red text")
@io#reset_colors()

Set background color:

@io#set_background(@io#BLUE)
@io#write("Blue background")

Reset to default colors:

@io#reset_colors()

Available colors: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE.

@progress = @io#ProgressBar::new(100)
for @i in 0..100
@progress#update(@i)
@io#sleep(milliseconds(50))
fin for.
@progress#finish()
@spinner = @io#Spinner::new("Loading...")
@spinner#start()
@// ... do work ...
@spinner#stop()