console-rs / indicatif

A command line progress reporting library for Rust
MIT License
4.22k stars 238 forks source link

allow padding HumanBytes Display output #583

Closed darrell-roberts closed 3 months ago

darrell-roberts commented 10 months ago

I've updated the Display implementation for HumanBytes so it can accept formatting parameters.

djc commented 10 months ago

So what exactly is this trying to solve? It seems unfortunate that this will need an additional allocation.

darrell-roberts commented 10 months ago

The goal was to be able to output HumanBytes with formatting parameters.

Ex:

#[test]
    fn test_human_bytes() {
        use std::fmt::Write;

        let mut s = String::new();
        write!(&mut s, "{:>10}", HumanBytes(10_000)).unwrap();
        assert_eq!(s, "  9.77 KiB");
    }

I agree though I would not want to incur an additional allocation cost. If there is a better alternative I'd go with that.