Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print "Cleaned nothing" instead of "Cleaned 0.00 B" #127

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
);
}
}
Loading