Skip to main content

Crate cli_stream

Crate cli_stream 

Source
Expand description

Streaming subprocess control: spawn a child, read its stdout and stderr line-by-line, write to its stdin, and cancel it (SIGTERM → SIGKILL on unix, TerminateProcess elsewhere). No console window is flashed on Windows.

It knows no CLI’s output format and no agent’s protocol — it moves lines.

A child gets pipes, never a terminal, so isatty is false for it. That is usually what you want — no colour codes, no progress bars — but a CLI built around interactive prompts will refuse or hang, so run those in whatever non-interactive mode they offer. needs_terminal recognises the complaint when one slips through.

use cli_stream::{Command, Event};

// stdout and stderr are always streamed.
let (handle, events) = Command::new("some-cli").args(["--verbose"]).start()?;
for event in events {
    match event {
        Event::Stdout { line, .. } => println!("out: {line}"),
        Event::Stderr { line, .. } => eprintln!("err: {line}"),
        _ => {}
    }
}

Stdin is opt-in, because a child that inherits a terminal’s stdin can block forever waiting for input nobody is typing. Ask for it and answer the child as it asks — the handle is yours for the whole run:

use cli_stream::{Command, Event, Stdin};

let (handle, events) = Command::new("some-cli").stdin(Stdin::Piped).start()?;
for event in events {
    if let Event::Stdout { line, .. } = event {
        if line.contains("Password:") {
            handle.write_line("hunter2")?;
        }
    }
}

Command::stream takes a callback instead, for a caller forwarding onto a sink rather than looping.

InstallEvent is the sibling shape for streamed install/login output.

A deliberate leaf: it depends on nothing of ours, so anything driving a CLI can use it. Finding a CLI a user installed — resolving a bare name, locating the node it was installed beside — is a different question, and lives with the caller that needs it (agent_harness::node_cli).

Re-exports§

pub use error::StreamError;
pub use install::InstallEvent;
pub use process::hidden_command;
pub use process::needs_terminal;
pub use process::Command;
pub use process::Event;
pub use process::ProcessHandle;
pub use process::Stderr;
pub use process::Stdin;

Modules§

error
Typed errors for the streaming engine.
install
Streamed install / sign-in progress events.
process
Streaming subprocess control: spawn a child, pipe its stdout/stderr line-by-line through a callback as Events, and hand back a ProcessHandle for cancellation (SIGTERM → SIGKILL).