Skip to content

Commit

Permalink
Print "Cleaned nothing" instead of "Cleaned 0.00 B" (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
Expurple committed Mar 26, 2024
1 parent a1146c5 commit 76a42aa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use self::fingerprint::{
hash_toolchains, remove_not_built_with, remove_older_than, remove_older_until_fits,
};
use self::stamp::Timestamp;
use self::util::format_bytes;
use self::util::{format_bytes, format_bytes_or_nothing};

/// Setup logging according to verbose flag.
fn setup_logging(verbosity_level: u8) {
Expand Down Expand Up @@ -195,14 +195,14 @@ fn main() -> anyhow::Result<()> {
Ok(cleaned_amount) if dry_run => {
info!(
"Would clean: {} from {project_path:?}",
format_bytes(cleaned_amount)
format_bytes_or_nothing(cleaned_amount)
);
total_cleaned += cleaned_amount;
}
Ok(cleaned_amount) => {
info!(
"Cleaned {} from {project_path:?}",
format_bytes(cleaned_amount)
format_bytes_or_nothing(cleaned_amount)
);
total_cleaned += cleaned_amount;
}
Expand All @@ -218,14 +218,14 @@ fn main() -> anyhow::Result<()> {
Ok(cleaned_amount) if dry_run => {
info!(
"Would clean: {} from {project_path:?}",
format_bytes(cleaned_amount)
format_bytes_or_nothing(cleaned_amount)
);
total_cleaned += cleaned_amount;
}
Ok(cleaned_amount) => {
info!(
"Cleaned {} from {project_path:?}",
format_bytes(cleaned_amount)
format_bytes_or_nothing(cleaned_amount)
);
total_cleaned += cleaned_amount;
}
Expand All @@ -248,14 +248,14 @@ fn main() -> anyhow::Result<()> {
Ok(cleaned_amount) if dry_run => {
info!(
"Would clean: {} from {project_path:?}",
format_bytes(cleaned_amount)
format_bytes_or_nothing(cleaned_amount)
);
total_cleaned += cleaned_amount;
}
Ok(cleaned_amount) => {
info!(
"Cleaned {} from {project_path:?}",
format_bytes(cleaned_amount)
format_bytes_or_nothing(cleaned_amount)
);
total_cleaned += cleaned_amount;
}
Expand Down
23 changes: 23 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ pub fn format_bytes(bytes: u64) -> String {
format!("{bytes} TiB")
}

/// Like [format_bytes], but with a special case for formatting `0` as `"nothing"`.
/// With `--recursive`, this helps visually distinguish folders without outdated artifacts.
/// See [#93](https://github.com/holmgr/cargo-sweep/issues/93).
pub fn format_bytes_or_nothing(bytes: u64) -> String {
match bytes {
0 => "nothing".to_string(),
_ => format_bytes(bytes),
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -40,4 +50,17 @@ mod tests {
1024 * 1024 * 1024 * 1024
);
}

#[test]
fn test_format_bytes_or_nothing() {
assert_eq!(format_bytes_or_nothing(0), "nothing");

// Copy-pasted some non-zero values from `format_bytes` tests to test that the output is identical.
assert_eq!(format_bytes(1024), format_bytes_or_nothing(1024));
assert_eq!(format_bytes(1023), format_bytes_or_nothing(1023));
assert_eq!(
format_bytes(500 * 1024 * 1024),
format_bytes_or_nothing(500 * 1024 * 1024)
);
}
}

0 comments on commit 76a42aa

Please sign in to comment.