1 unstable release
Uses old Rust 2015
0.1.0 | Oct 18, 2015 |
---|
#1000 in Unix APIs
5KB
81 lines
pine
line oriented process output
apidocs
Find them here
usage
Rust's interface for working with processes is pretty great, but sometimes you may wish to stream process output as it becomes available rather waiting for the process to exit before you can get a handle on the total process output.
For these usecases, pine
provides in iterator interface over lines of process output,
represented as enum of pine::Line::StdOut
or pine::Line::StdErr
. This is well suited for unix programs with emit
line-oriented output. A prerequite for your program to gain access
to these lines of output, is making sure your child process output is "piped" to your program. Rust's Command interface
makes this simple.
extern crate pine;
use std::process::{Command, Stdio};
let mut process = Command::new("/bin/sh")
.arg("-c")
.arg("curl https://www.howsmyssl.com/a/check")
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn().ok().unwrap();
With the child's output piped to your program, you can then iterate over lines of output as
they are available. using the pine::lines
function.
use pine::Line;
let lines = pine::lines(&mut process);
for line in lines.iter() {
match line {
Line::StdOut(line) => println!("out -> {}", line),
Line::StdErr(line) => println!("err -> {}", line)
}
}
Note iter()
returns an iterator, which means any functions defined on iterator are
at your disposal for processing line output.
Doug Tangren (softprops) 2015