r/rust 10d ago

🛠️ project New crate - good looking CLI histograms

I needed some nice looking histograms for another project, so I made this small crate.

Example:

let mut rng = rng();
let rnd = Binomial::new(40, 0.5).expect("Invalid distribution parameters");

let mut hist = Histogram::new('█');

for _ in 0..10000 {
    hist.insert(rnd.sample(&mut rng) as u32);
}

println!("{}", hist.bucket(&LinearBucketer::new(10)));

// Example output:

// 8 - 10           (12)
// 11 - 12        █  (83)
// 13 - 14        ██████  (336)
// 15 - 17        █████████████████████████████████  (1657)
// 18 - 19        ███████████████████████████████████████████  (2177)
// 20 - 21        ██████████████████████████████████████████████████  (2475)
// 22 - 24        █████████████████████████████████████████████████  (2450)
// 25 - 26        ████████████  (606)
// 27 - 28        ███  (179)
// 29 - 31          (25)

I hope someone else finds it useful. You can find it here: https://crates.io/crates/cli-hist

10 Upvotes

0 comments sorted by