Skip to content

Commit

Permalink
Implement a function to humanize bytes. Remove dependency. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleury authored Sep 2, 2024
1 parent 222803a commit 05bff0f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 43 deletions.
33 changes: 0 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ exclude = ["/.github/"]

[dependencies]
clap = { version = "4.5.16", features = ["derive"] }
humanize-bytes = "1.0.6"
spinoff = { version = "0.8.0", features = ["dots"] }

[dev-dependencies]
Expand Down
44 changes: 44 additions & 0 deletions src/bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#![allow(clippy::cast_precision_loss)]

const UNITS: [&str; 6] = ["B", "KB", "MB", "GB", "TB", "PB"];

const BASE: u64 = 1000;

pub fn humanize(bytes: u64) -> String {
if bytes < BASE {
return format!("{bytes} B");
}
let exponent = bytes.ilog10() / BASE.ilog10();
let unit = UNITS[exponent as usize];
let value = bytes as f64 / BASE.pow(exponent) as f64;
let precision = match unit {
"KB" => 0,
"MB" => 1,
_ => 2,
};
format!("{value:.precision$} {unit}")
}

#[cfg(test)]
mod test {
use super::humanize;

#[test]
fn test_humanize() {
assert_eq!(humanize(0), "0 B");
assert_eq!(humanize(256), "256 B");
assert_eq!(humanize(512), "512 B");
assert_eq!(humanize(1_000), "1 KB");
assert_eq!(humanize(2_650), "3 KB");
assert_eq!(humanize(737_525), "738 KB");
assert_eq!(humanize(1_000_000), "1.0 MB");
assert_eq!(humanize(1_240_000), "1.2 MB");
assert_eq!(humanize(1_250_000), "1.2 MB");
assert_eq!(humanize(1_260_000), "1.3 MB");
assert_eq!(humanize(10_525_000), "10.5 MB");
assert_eq!(humanize(2_886_000_000), "2.89 GB");
assert_eq!(humanize(200_500_150_001), "200.50 GB");
assert_eq!(humanize(50_000_000_000_000), "50.00 TB");
assert_eq!(humanize(1_421_000_000_000_000), "1.42 PB");
}
}
10 changes: 5 additions & 5 deletions src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs::ReadDir;
use std::io;
use std::path::PathBuf;

use humanize_bytes::humanize_bytes_decimal;
use crate::bytes;

pub fn from_path(path: &str) -> io::Result<FileIter> {
let dir = std::fs::read_dir(path)?;
Expand All @@ -27,7 +27,7 @@ impl File {

impl Display for File {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.size, self.path.display())
write!(f, "{} {}", self.size, self.path.display())
}
}

Expand All @@ -36,7 +36,7 @@ pub struct Size(u64);

impl Display for Size {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:>8}", humanize_bytes_decimal!(self.0))
write!(f, "{}", bytes::humanize(self.0))
}
}

Expand Down Expand Up @@ -84,11 +84,11 @@ mod test {
let cases = vec![
Case {
file: File::new("/path/to/file.txt", 1000),
want: " 1 kB: /path/to/file.txt",
want: "1 KB /path/to/file.txt",
},
Case {
file: File::new("/path/to/file.txt", 34250),
want: " 34.2 kB: /path/to/file.txt",
want: "34 KB /path/to/file.txt",
},
];
for case in cases {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod bytes;
mod files;

use std::{collections::BTreeMap, io};
Expand Down
8 changes: 4 additions & 4 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ fn binary_with_no_args_prints_top_5_largest_files_under_working_directory() {
fn binary_with_path_arg_prints_the_top_5_largest_files_under_the_given_path() {
let want = [
"*** Top 5 largest files ***",
" 7 B: ./testdata/en/world.txt",
" 6 B: ./testdata/es/mundo.txt",
" 6 B: ./testdata/en/hello.txt",
" 5 B: ./testdata/es/hola.txt",
"7 B ./testdata/en/world.txt",
"6 B ./testdata/es/mundo.txt",
"6 B ./testdata/en/hello.txt",
"5 B ./testdata/es/hola.txt",
];
Command::cargo_bin("dstats")
.unwrap()
Expand Down

0 comments on commit 05bff0f

Please sign in to comment.