Pular para o conteúdo

Streaming e Scripts Executáveis

Nesta página

spawn() devolve um handle de processo imediatamente. Itere sobre o handle para consumir stdout linha a linha, ou chame read_line(timeout_ms) quando a espera precisar de limite.

Faz streaming da ajuda do Git e depois lê uma linha com timeout de outro processo.

08-streaming.zolo
Playground
// 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()

Requer a CLI/host do Zolo — abra no playground ou rode localmente.

No Unix, um arquivo .zolo pode começar com #!/usr/bin/env zolo e ser executado diretamente depois de receber permissão de execução. O lexer ignora essa primeira linha; no Windows, ela é inofensiva.

Usa shebang e imprime uma saudação baseada na identidade local do Git.

09-shebang-script.zolo
Playground
#!/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}")
}

Requer a CLI/host do Zolo — abra no playground ou rode localmente.

Buscar no Zolo

9 resultados

enespt-br