Skip to content

Streaming and Executable Scripts

On this page

spawn() returns a process handle immediately. Iterate over the handle to consume stdout one line at a time, or call read_line(timeout_ms) when waiting must be bounded.

Streams Git help output, then performs a timed line read from another process.

08-streaming.zolo
// Feature: streaming — read a process's stdout line-by-line as it arrives.

// Syntax: `.spawn()` starts the pipeline without waiting and returns a

// ProcHandle; `for line in proc { … }` blocks per line until EOF.

// `.timeout(ms)` on the handle ends the LOOP (not the process) after a

// silent period — the tail-follow idiom.

// When to use: logs, long builds, anything you'd `tail -f`.


let proc = sh"git help -a".spawn()

let mut lines = 0
let mut first_line = ""
for line in proc {
    if lines == 0 {
        first_line = line
    }
    lines = lines + 1
}

print("streamed {lines > 10} (more than 10 lines)")
print("first line: {first_line.len() >= 0}")

// The handle is inspectable too:

let p2 = sh"git version".spawn()
let l = p2.read_line(2000)
print("read_line with timeout: {l != nil}")
p2.wait()

Requires the Zolo CLI/host — open in the playground or run locally.

On Unix, a .zolo file can start with #!/usr/bin/env zolo and be launched directly after it is made executable. The lexer ignores this first line; on Windows it is harmless.

Uses a shebang and prints a greeting based on the local Git identity.

09-shebang-script.zolo
#!/usr/bin/env zolo
// Feature: shebang — a .zolo file as a directly-executable script.

// Syntax: `#!/usr/bin/env zolo` on LINE 1 is lexer trivia (ignored by the

// compiler, preserved by the formatter). On Unix: chmod +x and run it.

// On Windows it's inert but harmless (best-effort, like every scripting

// language). `#!` anywhere else is still a parse error — #[attr] and

// #{map} are untouched.


let who = sh"git config --get user.name".run_raw().stdout.trim()
if who == "" {
    print("hello from an executable zolo script")
} else {
    print("hello, {who}")
}

Requires the Zolo CLI/host — open in the playground or run locally.

Search Zolo

9 results

enespt-br