I am starting my Rust Journey and I am fighting to find examples in the internet to run a bash command from Rust and continuously display its output, like when we run in bash top
or htop
.
use std::process::Command;
fn main() {
let mut cmd = Command::new("top");
cmd.arg("-d")
.arg("1");
match cmd.output() {
Ok(output) => {
unsafe {
println!("{}", String::from_utf8_unchecked(output.stdout));
eprintln!("{}", String::from_utf8_unchecked(output.stderr));
}
},
Err(error) => {
eprintln!("ERROR: {}", error);
},
}
}
For what I understand this approach will wait for the command to be executed to return the output, but I need to have a continuous stream from the bash shell and display it... How can I achieve it?
P.S: I use the top
program here just for illustrative purpose, because what I need is that I can execute any bash program that continuously outputs to the screen until we kill it, like tail -f
, docker run -it some/image
, etc.