How to capture Command's dynamic output?

    .args(&["toolchain", "install", "nightly-2019-07-15-x86_64-apple-darwin"])

// info: syncing channel updates for 'nightly-2019-07-15-x86_64-apple-darwin'
// info: latest update on 2019-07-15, rust version 1.38.0-nightly (83e4eed16 2019-07-14)
// info: downloading component 'rustc'
// info: downloading component 'rust-std'

It lost the download progress.

You're probably looking for the Command::stdout() and Command::stderr() methods. If you pass in Stdio::piped() it'll set up a pipe you can read output from as it's generated.

Otherwise the Command::output() method will wait until the process finishes, returning all generated output at the end (still requires Stdio::piped()).

Any example? To get the real time download progress.

Something like:

info: downloading component 'rust-std'
 38.7 MiB / 168.6 MiB ( 23 %)  37.1 MiB/s in  2s ETA:  3s

I'd experiment with spawn()-ing the child process and then reading from the stdout pipe using the normal read() method (or maybe wrap stdout in a std::io::BufReader and iterate over the lines()).

What are you trying to do with this output, by the way? Trying to programmatically read this sort of interactive output tends to be pretty brittle and there might be a better way.

I want to know the progress, sometimes it might take an hour or more (poor network).

Most tools that output incremental progress only do so when stdout is a tty, since it relies on cursor position to overwrite previous output.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.