console-rs / indicatif

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

make set_message/set_prefix accept anything that impls Display #425

Closed chris-laplante closed 2 years ago

chris-laplante commented 2 years ago

Among other things, by broadening the allowed types it lets you pass a console::StyledObject which is convenient.

chris-laplante commented 2 years ago

I also added accessors to ProgressBar to get the current message/prefix.

djc commented 2 years ago

I think the accessors should return &str.

chris-laplante commented 2 years ago

I think the accessors should return &str.

The value is behind a Mutex so I don't think this is possible

djc commented 2 years ago

So the downside here is that it's no longer possible to pass a &static str without it allocating a String for you? Not sure that's a good trade-off... You could just call to_string() in the caller, right?

chris-laplante commented 2 years ago

So the downside here is that it's no longer possible to pass a &static str without it allocating a String for you? Not sure that's a good trade-off... You could just call to_string() in the caller, right?

Yes, that is true :(. An alternative would be to introduce a trait that we impl on both Cow<'static, str> and console::StyledObject. (I did initially try to go a similar route, where I attempted to impl the trait for anything that is impl Into<Cow<'static, str>>. but I'm not sure Rust lets you express that.)

djc commented 2 years ago

Hmm, I feel like adding yet another trait isn't really worth it for this. I think impl Into<Cow<'static, str>> is somewhat of a local optimum, as you can trivially call to_string() on any impl Display that you've got and pass that.

chris-laplante commented 2 years ago

Hmm, I feel like adding yet another trait isn't really worth it for this. I think impl Into<Cow<'static, str>> is somewhat of a local optimum, as you can trivially call to_string() on any impl Display that you've got and pass that.

OK, that's fine by me then. I'll close this.