Skip to content

Commit

Permalink
Add bytes info
Browse files Browse the repository at this point in the history
  • Loading branch information
aurexav committed Dec 23, 2024
1 parent a5760c0 commit c9b6c3a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cli/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ pub fn ser_size_mb<S>(size: &usize, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&format!("{:.2} MB", *size as f64 / 1024. / 1024.))
fn format_with_commas(num: &usize) -> String {
let s = num.to_string();

s.as_bytes().rchunks(3).map(String::from_utf8_lossy).rev().collect::<Vec<_>>().join(",")
}

let mb = *size as f64 / (1024. * 1024.);
let bytes = format_with_commas(size);

serializer.serialize_str(&format!("{mb:.2} MB ({bytes} bytes)"))
}

#[test]
Expand All @@ -41,6 +50,6 @@ fn ser_should_work() {
size: 1024 * 1024
})
.unwrap(),
r#"{"time":"2021-08-01T00:00:00+00:00","size":"1.00 MB"}"#
r#"{"time":"2021-08-01T00:00:00+00:00","size":"1.00 MB (1,048,576 bytes)"}"#
)
}

0 comments on commit c9b6c3a

Please sign in to comment.