Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to customize template parameters/units #299

Closed
redzic opened this issue Jul 1, 2021 · 5 comments
Closed

No way to customize template parameters/units #299

redzic opened this issue Jul 1, 2021 · 5 comments

Comments

@redzic
Copy link
Contributor

redzic commented Jul 1, 2021

Currently, I have forked indicatif to add an easier way to display the frames per second of an encode. I did this because calling set_message seems to add significant "flashing" to the progress bar, whereas just using the templates does not seem to cause this issue. Furthermore, I do not want to have to duplicate data structures for keeping track of the frames per second when this is already kept track of by a ProgressBar. For example, tqdm has a way of specifying the units and using more precision for the iterations per second, which doesn't seem to be currently possible with indicatif, unless the unit you want to use is bytes per second.

Maybe there could be some function to be called that adds user-defined functions to the template parser.

@djc
Copy link
Collaborator

djc commented Jul 2, 2021

Can you be more concrete about the changes you made/what kind of data you want to show, and what is missing to support that?

@redzic
Copy link
Contributor Author

redzic commented Jul 2, 2021

In short, I want to be able to define a function that can reference the ProgressState which is called during template expansion, in format_state. Currently, I have achieved this by just forking indicatif and adding those functions myself.

Here are the changes that I have made to support my use case (the code doesn't seem to inline in github for me for whatever reason):

https://github.com/redzic/indicatif/blob/7fc8131591e41d1d26edb8f2e6fefec50ff8cdbc/src/style.rs#L264
https://github.com/redzic/indicatif/blob/7fc8131591e41d1d26edb8f2e6fefec50ff8cdbc/src/state.rs#L119-L127

For my use case, I would like to display the iterations per second with more precision and with a different unit. If you use per_sec, then it it has no decimal precision, and the unit at the end is always /s. I would like a specifiable amount of precision, and the ability to change the unit to fps or anything else.

Using set_message to achieve this causes a lot of flickering (which doesn't happen otherwise), and requires me to keep a redundant copy of data related to tracking the iterations per second. Basically, it is more error-prone and causes unnecessary overhead for my use case.

I was thinking that the easiest way to support this is to just have the ability to dynamically add keys that format_state calls. Perhaps ProgressStyle could be updated to hold a hash map of strings to functions that format_state calls.

I could take a stab at implementing this if the design is good, otherwise there could be some discussion to be had as to how to achieve this.

@djc
Copy link
Collaborator

djc commented Jul 5, 2021

I think it would be okay to have support for custom template keys which get a &ProgressState. Let's only expose ProgressState::position() publicly for now?

@redzic
Copy link
Contributor Author

redzic commented Jul 13, 2021

Fixed by #301 ! Thanks

@redzic redzic closed this as completed Jul 13, 2021
@djc
Copy link
Collaborator

djc commented Jul 13, 2021

Thank you for getting it done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants