prettysize-rs icon indicating copy to clipboard operation
prettysize-rs copied to clipboard

Support display modifiers

Open RagnarGrootKoerkamp opened this issue 1 year ago • 2 comments

Hey, thanks for the nice crate!

It would be great if you could add support for display modifiers, so that e.g. format!("{size:>10}") prints the value of the size with a total width of (at least) 10 characters, aligned to the right, and format!("{size:<10.3}") left-aligns the value and uses exactly 3 decimal places.

This is useful e.g. to display values in a column layout. In a perfect world something like below is what I'm looking for, but this may be a bit too much.

 1.0 B
 2.1kB
 4.0MB
12.2GB

RagnarGrootKoerkamp avatar Jun 14 '24 15:06 RagnarGrootKoerkamp

Oh nevermind, I see now that by default it already has exactly 3 significant digits. That's perfect! (Formatting the total width is easy by doing format!("{:>10}", format!("{size}").)

RagnarGrootKoerkamp avatar Jun 17 '24 15:06 RagnarGrootKoerkamp

Hey, thanks for opening this issue.

There's a 0.6x branch with a new with_scale() option that can be used to explicitly set the number of decimal places that I'll merge into master after release 0.5 (and you can use it today), but that's not to be confused with width/precision (assuming that was what you intended).

If you don't mind, I'm going to re-open this issue because it would be interesting to see what native integration we can support with rust's format specifiers. It might end up going nowhere, but it doesn't mean we can't explore it and see. Perhaps we can translate a precision specifier like {:.N} into a call to with_scale(N) directly, which would be nice!

mqudsi avatar Jun 18 '24 00:06 mqudsi

This was merged in https://github.com/neosmart/prettysize-rs/commit/bacd69d127d9825cec139b540be700fab58312f8

mqudsi avatar May 23 '25 18:05 mqudsi