#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

#177 in Value formatting

Download history 30/week @ 2024-07-27 54/week @ 2024-08-03 2/week @ 2024-08-10 27/week @ 2024-08-24 4/week @ 2024-08-31 15/week @ 2024-09-07 12/week @ 2024-09-14 14/week @ 2024-09-21 6/week @ 2024-09-28 20/week @ 2024-10-05 32/week @ 2024-10-12 133/week @ 2024-10-19 69/week @ 2024-10-26 146/week @ 2024-11-02 194/week @ 2024-11-09

543 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
~32K SLoC