From 76a42aadcf0c1e483d1e6afb76fd195f130e5233 Mon Sep 17 00:00:00 2001 From: Dmitrii Aleksandrov Date: Tue, 26 Mar 2024 07:12:47 +0400 Subject: [PATCH] Print "Cleaned nothing" instead of "Cleaned 0.00 B" (#93) --- src/main.rs | 14 +++++++------- src/util.rs | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) 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) + ); + } }