console-rs / indicatif

A command line progress reporting library for Rust
MIT License
4.36k stars 241 forks source link

Add option for JSON escaping for placeholder values #527

Closed akx closed 1 year ago

akx commented 1 year ago

Refs #525 (I went ahead and wrote some code...).

This PR

IOW, a ProgressStyle with json_strings(true) and a template

{{"pos": {pos}, "len": {len}, "prefix": {prefix}, "msg": {msg}}}

(this is what ProgressStyle::with_json_keys(&["pos", "len", "prefix", "msg"]) would internally generate) will result in progress lines like

{"pos": "1", "len": "10", "prefix": "hello", "msg": "something"}

which can then be sent to a TermLike target (#354, #526) to implement #525 :)


If you like, I can put this behind a feature gate to avoid the json dependency – but what should indicatif do when trying to do JSON output when it's not compiled in? I'm pretty sure we wouldn't want it to just output non-escaped JSON...

djc commented 1 year ago

Sorry for being slow to respond -- while I think your use case is something we'd like to accomodate, I think this PR is probably not the right way to go.

In thinking some more about it, do you think it would be feasible to write a custom ProgressTracker that outputs the JSON? It seems like a viable path to me, in that the impl could take care of all the particular JSON needs.

akx commented 1 year ago

@djc No worries! I think I considered that too, but there was something missing in indicatif that was required for that to work. I'll revisit that idea when I have the time :)

djc commented 1 year ago

Would definitely be open to extending the API available to ProgressTracker if that helps your use case, let me know.

djc commented 1 year ago

Going to close this for now, feel free to reopen if you want to work on this again (or open a new one).