cli_stream/lib.rs
1//! Streaming subprocess control: spawn a child, read its **stdout** and
2//! **stderr** line-by-line, write to its **stdin**, and cancel it
3//! (SIGTERM → SIGKILL on unix, `TerminateProcess` elsewhere). No console
4//! window is flashed on Windows.
5//!
6//! It knows no CLI's output format and no agent's protocol — it moves lines.
7//!
8//! A child gets **pipes, never a terminal**, so `isatty` is false for it. That
9//! is usually what you want — no colour codes, no progress bars — but a CLI
10//! built around interactive prompts will refuse or hang, so run those in
11//! whatever non-interactive mode they offer. [`needs_terminal`] recognises the
12//! complaint when one slips through.
13//!
14//! ```no_run
15//! use cli_stream::{Command, Event};
16//!
17//! # fn main() -> Result<(), cli_stream::StreamError> {
18//! // stdout and stderr are always streamed.
19//! let (handle, events) = Command::new("some-cli").args(["--verbose"]).start()?;
20//! for event in events {
21//! match event {
22//! Event::Stdout { line, .. } => println!("out: {line}"),
23//! Event::Stderr { line, .. } => eprintln!("err: {line}"),
24//! _ => {}
25//! }
26//! }
27//! # let _ = handle;
28//! # Ok(())
29//! # }
30//! ```
31//!
32//! Stdin is opt-in, because a child that inherits a terminal's stdin can block
33//! forever waiting for input nobody is typing. Ask for it and answer the child
34//! as it asks — the handle is yours for the whole run:
35//!
36//! ```no_run
37//! use cli_stream::{Command, Event, Stdin};
38//!
39//! # fn main() -> Result<(), cli_stream::StreamError> {
40//! let (handle, events) = Command::new("some-cli").stdin(Stdin::Piped).start()?;
41//! for event in events {
42//! if let Event::Stdout { line, .. } = event {
43//! if line.contains("Password:") {
44//! handle.write_line("hunter2")?;
45//! }
46//! }
47//! }
48//! # Ok(())
49//! # }
50//! ```
51//!
52//! [`Command::stream`] takes a callback instead, for a caller forwarding onto a
53//! sink rather than looping.
54//!
55//! [`InstallEvent`] is the sibling shape for streamed install/login output.
56//!
57//! A deliberate *leaf*: it depends on nothing of ours, so anything driving a
58//! CLI can use it. Finding a CLI a user installed — resolving a bare name,
59//! locating the `node` it was installed beside — is a different question, and
60//! lives with the caller that needs it (`agent_harness::node_cli`).
61
62pub mod error;
63pub mod install;
64pub mod process;
65
66pub use error::StreamError;
67pub use install::InstallEvent;
68pub use process::{hidden_command, needs_terminal, Command, Event, ProcessHandle, Stderr, Stdin};