io
The io module provides console I/O and stream operations.
Import
Section titled “Import”import ioimport { write, read_line, print } from ioConsole Output
Section titled “Console Output”Write a string to stdout without a newline:
@io#write("Hello")@io#write(" World")@// Output: Hello Worldwrite_line
Section titled “write_line”Write a string to stdout with a newline:
@io#write_line("Hello, World!")@// Output: Hello, World!\nPrint any value with automatic conversion:
@io#print(42)@io#print("hello")@io#print([1, 2, 3])print_line
Section titled “print_line”Print any value with a newline:
@io#print_line("The answer is " + string(42))ewrite
Section titled “ewrite”Write to stderr:
@io#ewrite("Error: something went wrong")eprint_line
Section titled “eprint_line”Print to stderr with newline:
@io#eprint_line("Warning: deprecated API")Console Input
Section titled “Console Input”read_line
Section titled “read_line”Read a line from stdin:
@io#write("Enter your name: ")@name = @io#read_line()@io#write("Hello, " + @name)read_char
Section titled “read_char”Read a single character:
@char = @io#read_char()read_all
Section titled “read_all”Read all input until EOF:
@content = @io#read_all()read_int
Section titled “read_int”Read an integer from stdin:
@io#write("Enter a number: ")@num = @io#read_int()read_float
Section titled “read_float”Read a float from stdin:
@io#write("Enter a price: ")@price = @io#read_float()Formatted Output
Section titled “Formatted Output”format
Section titled “format”Create formatted strings:
@message = @io#format("Hello, {0}! You are {1}.", "Alice", 30)@// "Hello, Alice! You are 30."printf
Section titled “printf”Print formatted output:
@io#printf("Name: {0}, Age: {1}", "Alice", 30)Streams
Section titled “Streams”InputStream
Section titled “InputStream”@input = @io#InputStream::new("file.txt")@line = @input#read_line()@input#close()OutputStream
Section titled “OutputStream”@output = @io#OutputStream::new("output.txt")@output#write("Hello, World!")@output#close()BufferedStream
Section titled “BufferedStream”@stream = @io#BufferedStream::new("data.txt")@chunk = @stream#read(1024)@stream#write("data")@stream#flush()@stream#close()File I/O Helpers
Section titled “File I/O Helpers”read_file
Section titled “read_file”Read entire file as string:
@content = @io#read_file("example.txt")write_file
Section titled “write_file”Write string to file:
@io#write_file("output.txt", "Hello, World!")append_file
Section titled “append_file”Append to file:
@io#append_file("log.txt", "New entry\n")Terminal Control
Section titled “Terminal Control”clear_screen
Section titled “clear_screen”Clear the terminal:
@io#clear_screen()set_cursor
Section titled “set_cursor”Set cursor position:
@io#set_cursor(10, 5)hide_cursor
Section titled “hide_cursor”Hide the cursor:
@io#hide_cursor()show_cursor
Section titled “show_cursor”Show the cursor:
@io#show_cursor()Colors
Section titled “Colors”set_foreground
Section titled “set_foreground”Set text color:
@io#set_foreground(@io#RED)@io#write("Red text")@io#reset_colors()set_background
Section titled “set_background”Set background color:
@io#set_background(@io#BLUE)@io#write("Blue background")reset_colors
Section titled “reset_colors”Reset to default colors:
@io#reset_colors()Available colors: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE.
Progress Indicators
Section titled “Progress Indicators”ProgressBar
Section titled “ProgressBar”@progress = @io#ProgressBar::new(100)for @i in 0..100 @progress#update(@i) @io#sleep(milliseconds(50))fin for.@progress#finish()Spinner
Section titled “Spinner”@spinner = @io#Spinner::new("Loading...")@spinner#start()@// ... do work ...@spinner#stop()