How to capture the output of Command using .spawn()

Rust newbie here, so forgive me!

I’m writing a deployment script for running a few command line programs at once, and I want to display the output in varying colors to the console depending on which program is running. (Think running Mongod, tailing some log files at the same time, etc)

My limited understanding is that I want to use spawn(), and then maybe configure the stdout() handle to write to a function that adds some color, but I’m not sure what the best way to go about this is

Any tips to get me moving again would be appreciated, thanks!

Color text is made with ANSI
Search shows up a few potential crates for that part of your program;

I think you can set .stdout(Stdio::piped()) to enable capturing output, then take the child’s stdout with child.stdout.take().unwrap().

The stdout taken should implement Read, so you can read data from it. Maybe wrap it in a BufReader, read by line, color, then output to stdout? It should be possible to spawn up one thread per process you spawn in order to do each read line/color/output loop in parallel.

1 Like

Thanks so much! With your piped() hint and this awesome example I was able to figure it out