neosmart / prettysize-rs

Pretty-print file sizes and more
Other
42 stars 4 forks source link

Support display modifiers #13

Open RagnarGrootKoerkamp opened 3 weeks ago

RagnarGrootKoerkamp commented 3 weeks ago

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 commented 3 weeks ago

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}").)

mqudsi commented 3 weeks ago

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!