#human-readable #byte-size #byte-string #size #number-format #format-bytes #pretty-bytes

bin+lib pretty-bytes-rust

Rust library that takes a number that represent a byte and returns a string that is prettier to read for a human

5 unstable releases

0.3.0 Dec 1, 2023
0.2.2 Dec 1, 2023
0.2.1 May 11, 2023
0.2.0 May 6, 2023
0.1.0 Nov 1, 2022

#153 in Value formatting

Download history 6/week @ 2024-09-29 22/week @ 2024-10-06 30/week @ 2024-10-13 134/week @ 2024-10-20 107/week @ 2024-10-27 127/week @ 2024-11-03 176/week @ 2024-11-10 57/week @ 2024-11-17 190/week @ 2024-11-24 277/week @ 2024-12-01 97/week @ 2024-12-08 379/week @ 2024-12-15 174/week @ 2024-12-22 158/week @ 2024-12-29 298/week @ 2025-01-05 304/week @ 2025-01-12

981 downloads per month

Apache-2.0

17KB
255 lines

Pretty Bytes - Humanify Bytes for Easy Read

github crates.io docs.rs CI Build codecov

Rust library that takes a number that represent a byte and returns a string that is prettier to read for a human

Consumer of the Library

Install

cargo add pretty-bytes-rust

Consumer of the CLI

You can pass the byte with -b

pretty-bytes-rust -b 2000

You can see all the options:

pretty-bytes-rust --help

You can pipe the number of byte

echo "2000" | pretty-bytes-rust 

You can pipe and still use options

echo "2000" | pretty-bytes-rust -n 6 

How to use?

Without Configuration Option

use pretty_bytes_rust::pretty_bytes;
let r1 = pretty_bytes(1024 * 1024 * 5 + 50000, None);
assert_eq!(r1, "5.05 MB");

With Configuration Option - Specifying Decimal Precision

use pretty_bytes_rust::pretty_bytes;
let result = pretty_bytes(
    1024 * 1024 * 3,
    Some(PrettyBytesOptions {
        use_1024_instead_of_1000: Some(false),
        number_of_decimal: Some(3),
        remove_zero_decimal: Some(false),
    }),
);
assert_eq!(result, "3.146 MB");

With Configuration Option - 1024 instead of 1000


```rust
use pretty_bytes_rust::pretty_bytes;
let result = pretty_bytes(
    1024 * 1024 * 3,
    Some(PrettyBytesOptions {
        use_1024_instead_of_1000: Some(true),
        number_of_decimal: Some(3),
        remove_zero_decimal: Some(false),
    }),
);
assert_eq!(result, "3.146 MB");

As a Developer of the Library

What to Install?

You need to install the right toolchain:

rustup toolchain install stable
rustup default stable

To perform test coverage you need to install

cargo install grcov
rustup component add llvm-tools-preview

To generate benchmark plots you need to install GnuPlot

sudo apt update
sudo apt install gnuplot

# To confirm that it is properly installed:
which gnuplot

Tests

cargo test

Tests Coverage

You must install few components before running coverage:

cargo install grcov
rustup component add llvm-tools-preview

Then, you can run:

./coverage.sh

Further explanation in the Mozilla grcov website

Documentation

cargo doc --open

Testing CLI

All commands for the user works but instead of using pretty-bytes-rust -n 12345 you need to use cargo run -- -n 12345

Benchmark

cargo bench

Publishing

cargo login
cargo publish --dry-run
cargo publish

Dependencies

~1.1–1.7MB
~31K SLoC