diff --git a/src/main.rs b/src/main.rs index bfaf99b..4a479b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) { @@ -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; } @@ -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; } @@ -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; } diff --git a/src/util.rs b/src/util.rs index 46924bb..1c1359d 100644 --- a/src/util.rs +++ b/src/util.rs @@ -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::*; @@ -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) + ); + } }