I/O, Env & Process
I/O, Env & Process
lib/io.tg — Console I/O
Reading Input
func input() -> String # Read a line from stdinfunc input_i32() -> i32 # Read an integer from stdinfunc input_prompt(prompt: String = "") -> String # Read with promptfunc stdin_read_line(prompt: String = "") -> String # Alias for input_promptfunc stdin_read_all() -> String # Read all stdin until EOFFlexible Source Reading
func stdread(path: String = "") -> StringReads from a file or stdin depending on the path argument:
"","-","stdin","STDIN","con","CON","/dev/stdin"→ reads from stdin- Any other path → reads the file using
fs.read_text
File Content Display
func print_file_if_non_empty(path: String, header: String = "", footer: String = "") -> i32Prints a file’s content if it exists and is non-empty. Optionally prints header/footer around the content.
Example
import io
func main() -> i32: let name = io.input_prompt("Enter your name: ") print("Hello, " + name + "!")
let age = io.input_i32() print(age) return 0lib/env.tg — Environment
Command-Line Arguments
func count() -> i32 # Number of command-line argumentsfunc get(index: i32) -> String # Get argument by index (0 = program name)func program_name() -> String # Shorthand for get(0)func args_text() -> String # All arguments as a single stringEnvironment Variables
func get_var(key: String) -> String # Read environment variablefunc set_var(key: String, value: String) -> i32 # Set environment variablefunc cwd() -> String # Current working directoryExample: CLI Tool
import env
func main() -> i32: let n = env.count() print("Arguments received:")
let i = 0 while (i < n): let arg = env.get(i) print(arg) i = i + 1
if (n > 1): print("Processing file: " + env.get(1)) return 0lib/process.tg — Process Management
Shell Execution
func run(cmd: String) -> i32 # Run a shell command, returns exit codeStructured Process Execution
func exec(program: String, args_text: String) -> i32func exec_timeout(program: String, args_text: String, timeout_ms: i32) -> i32func exec_redirect(program: String, args_text: String, stdout_path: String, stderr_path: String) -> i32func exec_redirect_timeout(program: String, args_text: String, stdout_path: String, stderr_path: String, timeout_ms: i32) -> i32The args_text parameter uses newline (\n) as the argument separator. Use argv_sep() to get the separator character.
Utility Functions
func quote(text: String) -> String # Wrap text in double quotesfunc argv_sep() -> String # Returns argument separator (newline)func open_path(path: String) -> i32 # Open file/folder in OS default appfunc download(url: String, out_path: String) -> i32 # Download file (uses runtime or curl)func unzip(zip_path: String, out_dir: String) -> i32 # Extract ZIP archivefunc move_file(src: String, dst: String) -> i32 # Move/rename fileCommand Struct
struct Command: cmd: String
impl Command: func exec(self) -> i32Example: Automation Script
import fsimport process
func main() -> i32: # Create a log file print("Creating log file...") let f = fs.open_write("log.txt") f.write("System check initiated.") f.close()
# Run a shell command print("Checking OS info...") let code = process.run("echo Hello from Shell")
if (code == 0): print("Shell command executed.") else: print("Shell command failed.") return 0lib/string.tg — String Library
Core Functions
func len(text: String) -> i32 # String lengthfunc char_at(text: String, index: i32) -> String # Character at indexfunc trim(text: String) -> String # Trim whitespacefunc contains(text: String, needle: String) -> i32 # Search (1=found, 0=not)func starts_with(text: String, prefix: String) -> i32func ends_with(text: String, suffix: String) -> i32func replace(text: String, old_frag: String, new_frag: String) -> Stringfunc lower(text: String) -> String # To lowercasefunc upper(text: String) -> String # To uppercasefunc from_codepoint(cp: i32) -> String # Unicode codepoint → Stringfunc quote(text: String) -> String # Wrap in double quotesfunc unquote(text: String) -> String # Remove surrounding double quotesExample
import "lib/string.tg" as str
func main() -> i32: let text = " Hello World " print(str.trim(text)) # Output: Hello World print(str.upper("hello")) # Output: HELLO print(str.contains("abc", "b")) # Output: 1 print(str.replace("a-b-c", "-", ".")) # Output: a.b.c return 0lib/map.tg — Key-Value Map
func new() -> ptrfunc put(m: ptr, key: String, value: ptr) -> i32func get(m: ptr, key: String) -> ptrfunc is_null_ptr(p: ptr) -> i32Example
import map
func main() -> i32: let m = map.new() m.put("x", "Value X") m.put("y", "Value Y")
let val_x = m.get("x") print(val_x)
let missing = m.get("z") if (map.is_null_ptr(missing) == 1): print("Key z not found") return 0