console-rs / indicatif

A command line progress reporting library for Rust
MIT License
4.21k stars 238 forks source link

Question: How to get latest progress bar state string #617

Closed Halimao closed 6 months ago

Halimao commented 6 months ago

Summary

Is there a way that we could get the latest progress bar state just like output in the terminal, something like following cases, get the progress bar state string that is shown to user

Case1: downloading or download process terminate

[00:00:06] [###################################--------] 67.79 MiB/97.79 MiB (5s) -

Case2: downloaded successfully

[00:00:06] [########################################] 97.79 MiB/97.79 MiB (0s) - Download complete
chris-laplante commented 6 months ago

By state string, do you mean the message? (e.g. "Download complete" in Case 2) Or do you mean the entire line that is displayed (including the #### characters and everything)?

I assume you mean the latter. There is no built-in way, but you can probably make it work by implementing the TermLike trait and sort of "fork" (or tee) the output to the actual terminal and an InMemoryTerm. Then you use the InMemoryTerm to get the output.

Halimao commented 6 months ago

@chris-laplante Hi sir, thanks for your reply. I indeed mean the latter, get the entire line that is displayed to user. I will try the way you suggested, thanks again.

chris-laplante commented 6 months ago

@chris-laplante Hi sir, thanks for your reply. I indeed mean the latter, get the entire line that is displayed to user. I will try the way you suggested, thanks again.

Sure thing, just let me know if have issues with it :)