find/
find.rs

1/// To run an example run the following command
2/// `cargo run --example find`.
3///
4/// The example is based on https://github.com/zhiburt/ptyprocess/issues/2
5use ptyprocess::PtyProcess;
6use std::io::{BufRead, BufReader};
7use std::process::Command;
8
9fn main() {
10    let mut cmd = Command::new("find");
11    cmd.args(vec!["/home/", "-name", "foo"]);
12    cmd.stderr(std::process::Stdio::null());
13
14    let process = PtyProcess::spawn(cmd).unwrap();
15    let mut reader = BufReader::new(process.get_raw_handle().unwrap());
16
17    let mut buf = String::new();
18    loop {
19        let n = reader.read_line(&mut buf).expect("readline error");
20        if n == 0 {
21            break;
22        }
23
24        // by -1 we drop \n.
25        let text = &buf[0..buf.len() - 1];
26        println!("buffer: {text}");
27
28        buf.clear();
29    }
30}