mirror of
https://github.com/nivekuil/rip
synced 2024-11-22 19:53:12 +00:00
Humanize filesize output
This commit is contained in:
parent
c5d84b9777
commit
e0d48f5aca
2 changed files with 21 additions and 8 deletions
19
src/main.rs
19
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::<u64>());
|
||||
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::<u64>()));
|
||||
|
||||
// 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)
|
||||
|
|
10
src/util.rs
10
src/util.rs
|
@ -29,3 +29,13 @@ fn rename_grave<G: AsRef<Path>>(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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue