fix: dont format filecount when output is set to json

This commit is contained in:
AminurAlam
2025-10-10 15:55:18 +05:30
committed by andy.boot
parent 67d23e80ff
commit f98b841d23
2 changed files with 15 additions and 1 deletions

View File

@@ -439,6 +439,9 @@ pub fn get_number_format(output_str: &str) -> Option<(u64, char)> {
} }
pub fn human_readable_number(size: u64, output_str: &str) -> String { pub fn human_readable_number(size: u64, output_str: &str) -> String {
if output_str == "count" {
return size.to_string();
};
match get_number_format(output_str) { match get_number_format(output_str) {
Some((x, u)) => { Some((x, u)) => {
format!("{}{}", (size / x), u) format!("{}{}", (size / x), u)
@@ -539,6 +542,13 @@ mod tests {
assert_eq!(s, "short 3 4.0K 100%"); assert_eq!(s, "short 3 4.0K 100%");
} }
#[test]
fn test_machine_readable_filecount() {
assert_eq!(human_readable_number(1, "count"), "1");
assert_eq!(human_readable_number(1000, "count"), "1000");
assert_eq!(human_readable_number(1024, "count"), "1024");
}
#[test] #[test]
fn test_human_readable_number() { fn test_human_readable_number() {
assert_eq!(human_readable_number(1, ""), "1B"); assert_eq!(human_readable_number(1, ""), "1B");

View File

@@ -319,7 +319,11 @@ fn print_output(
if config.get_output_json(&options) { if config.get_output_json(&options) {
OUTPUT_TYPE.with(|wrapped| { OUTPUT_TYPE.with(|wrapped| {
wrapped.replace(output_format); if by_filecount {
wrapped.replace("count".to_string());
} else {
wrapped.replace(output_format);
}
}); });
println!("{}", serde_json::to_string(&tree).unwrap()); println!("{}", serde_json::to_string(&tree).unwrap());
} else { } else {