#process #stdio #input-output #stdin #running #output-stream #tiny

interactive_process

A tiny Rust library for interacting with a running process over stdio

4 releases

0.1.3 Jul 25, 2023
0.1.2 Aug 23, 2021
0.1.1 Aug 23, 2021
0.1.0 Aug 22, 2021

#239 in Unix APIs

Download history 39/week @ 2024-01-05 101/week @ 2024-01-12 105/week @ 2024-01-19 25/week @ 2024-01-26 98/week @ 2024-02-02 70/week @ 2024-02-09 111/week @ 2024-02-16 135/week @ 2024-02-23 125/week @ 2024-03-01 103/week @ 2024-03-08 115/week @ 2024-03-15 25/week @ 2024-03-22 98/week @ 2024-03-29 51/week @ 2024-04-05 86/week @ 2024-04-12 61/week @ 2024-04-19

296 downloads per month
Used in 3 crates (2 directly)

MIT/Apache

9KB
58 lines

interactive_process

crates.io docs.rs

A tiny Rust library for interacting with a running process over stdio.

A common pattern in Unix is to have programs that either produce or consume newline-delimited text over standard input and output (stdio) streams.

This crate provides a light wrapper (really light, look at src/lib.rs) that provides a tidy little abstraction for this pattern on top of Rust's built-in std::process. Besides std, this crate has no dependencies.

Usage

The examples in examples/ are instructive. For example, here's echo_stream.rs:

use interactive_process::InteractiveProcess;
use std::{process::Command, thread::sleep, time::Duration};

fn main() {
    /// Use Rust's built-in `std::process` to construct a `Command`.
    /// `examples/echo_stream.py` repeats back lines sent to it,
    /// prefixed with "echo: ".
    let cmd = Command::new("examples/echo_stream.py");

    /// Pass this command to `InteractiveProcess`, along with a
    /// callback. In this case, we'll print every line that the
    /// process prints to `stdout`, prefixed by "Got: ".
    let mut proc = InteractiveProcess::new(cmd, |line| {
        println!("Got: {}", line.unwrap());
    })
    .unwrap();

    /// Send some data, waiting in between.
    /// The result of this is "Got: echo: data1" being printed by our callback,
    /// since our callback preprends "Got: " and the child process prepends
    /// "echo: ".
    proc.send("data1").unwrap();

    /// Sleep in this thread. Note that the process' `stdout` is processed in
    /// another thread, so while this thread sleeps, that thread will pick
    /// up the message printed by the child process and run the callback.
    sleep(Duration::from_secs(1));

    /// Repeat that a few more times, for kicks.
    proc.send("data2").unwrap();
    sleep(Duration::from_secs(1));
    proc.send("data3").unwrap();

    // If we don't sleep here, the process won't have time to reply
    // before we kill it.
    sleep(Duration::from_millis(1));

    /// We're done with the process, but it is not self-terminating,
    /// so we can't use `proc.wait()`. Instead, we'll take the `Child` from
    /// the `InteractiveProcess` and kill it ourselves.
    proc.close().kill().unwrap();
}

Limitations

I've tested this for simple things on Linux, but it's not battle-tested and I haven't tested it on other platforms. If you encounter issues, please open an issue and I'll do my best to work through it with you.

No runtime deps