Saltar al contenido

Streaming y Scripts Ejecutables

En esta página

spawn() devuelve un handle de proceso inmediatamente. Itera sobre el handle para consumir stdout línea a línea, o llama a read_line(timeout_ms) cuando la espera deba tener un límite.

Transmite la ayuda de Git y después realiza una lectura de línea con timeout de otro proceso.

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()

Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.

En Unix, un archivo .zolo puede comenzar con #!/usr/bin/env zolo y ejecutarse directamente después de darle permiso de ejecución. El lexer ignora esa primera línea; en Windows es inofensiva.

Usa un shebang e imprime un saludo basado en la identidad local de 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}")
}

Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.

Buscar en Zolo

9 resultados

enespt-br