diff --git a/src/main.rs b/src/main.rs index 91ef802..bc4a85d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -171,13 +171,15 @@ Send files to the graveyard (/tmp/.graveyard) instead of unlinking them.") if matches.is_present("inspect") { if metadata.is_dir() { // Get the size of the directory and all its contents - println!("{}: directory, {} bytes including:", target, - WalkDir::new(source) - .into_iter() - .filter_map(|x| x.ok()) - .filter_map(|x| x.metadata().ok()) - .map(|x| x.len()) - .sum::()); + println!("{}: directory, {} including:", target, + humanize_bytes( + WalkDir::new(source) + .into_iter() + .filter_map(|x| x.ok()) + .filter_map(|x| x.metadata().ok()) + .map(|x| x.len()) + .sum::())); + // Print the first few top-level files in the directory for entry in WalkDir::new(source) .min_depth(1).max_depth(1).into_iter() @@ -185,7 +187,8 @@ Send files to the graveyard (/tmp/.graveyard) instead of unlinking them.") println!("{}", entry.unwrap().path().display()); } } else { - println!("{}: file, {} bytes", target, metadata.len()); + println!("{}: file, {}", target, + humanize_bytes(metadata.len())); // Read the file and print the first few lines if let Ok(f) = fs::File::open(source) { for line in BufReader::new(f) diff --git a/src/util.rs b/src/util.rs index 04f389c..c9692c1 100644 --- a/src/util.rs +++ b/src/util.rs @@ -29,3 +29,13 @@ fn rename_grave>(grave: G) -> PathBuf { .next() .expect("Failed to rename duplicate file or directory") } + +fn humanize_bytes(bytes: u64) -> String { + let values = ["bytes", "KB", "MB", "GB", "TB"]; + let pair = values.iter() + .enumerate() + .take_while(|x| bytes as usize / (1000 as usize).pow(x.0 as u32) > 10) + .last() + .unwrap(); + format!("{} {}", bytes as usize / (1000 as usize).pow(pair.0 as u32), pair.1) +}