mirror of
https://github.com/uutils/coreutils
synced 2024-11-17 02:08:09 +00:00
commit
8db47690b2
1 changed files with 17 additions and 19 deletions
36
src/du/du.rs
36
src/du/du.rs
|
@ -35,6 +35,9 @@ const LONG_HELP: &'static str = "
|
||||||
ers of 1000).
|
ers of 1000).
|
||||||
";
|
";
|
||||||
|
|
||||||
|
// TODO: Suport Z & Y (currently limited by size of u64)
|
||||||
|
static UNITS: [(char, u32); 6] = [('E', 6), ('P', 5), ('T', 4), ('G', 3), ('M', 2), ('K', 1)];
|
||||||
|
|
||||||
struct Options {
|
struct Options {
|
||||||
all: bool,
|
all: bool,
|
||||||
program_name: String,
|
program_name: String,
|
||||||
|
@ -247,17 +250,6 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
matches.free.clone()
|
matches.free.clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
let mb = if matches.opt_present("si") {
|
|
||||||
1000 * 1000
|
|
||||||
} else {
|
|
||||||
1024 * 1024
|
|
||||||
};
|
|
||||||
let kb = if matches.opt_present("si") {
|
|
||||||
1000
|
|
||||||
} else {
|
|
||||||
1024
|
|
||||||
};
|
|
||||||
|
|
||||||
let block_size = match matches.opt_str("block-size") {
|
let block_size = match matches.opt_str("block-size") {
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
let mut found_number = false;
|
let mut found_number = false;
|
||||||
|
@ -305,18 +297,24 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
};
|
};
|
||||||
|
|
||||||
let convert_size = |size: u64| -> String {
|
let convert_size = |size: u64| -> String {
|
||||||
|
let multiplier: u64 = if matches.opt_present("si") {
|
||||||
|
1000
|
||||||
|
} else {
|
||||||
|
1024
|
||||||
|
};
|
||||||
|
|
||||||
if matches.opt_present("human-readable") || matches.opt_present("si") {
|
if matches.opt_present("human-readable") || matches.opt_present("si") {
|
||||||
if size >= mb {
|
for &(unit, power) in &UNITS {
|
||||||
format!("{:.1}M", (size as f64) / (mb as f64))
|
let limit = multiplier.pow(power);
|
||||||
} else if size >= kb {
|
if size >= limit {
|
||||||
format!("{:.1}K", (size as f64) / (kb as f64))
|
return format!("{:.1}{}", (size as f64) / (limit as f64), unit);
|
||||||
} else {
|
}
|
||||||
format!("{}B", size)
|
|
||||||
}
|
}
|
||||||
|
return format!("{}B", size);
|
||||||
} else if matches.opt_present("k") {
|
} else if matches.opt_present("k") {
|
||||||
format!("{}", ((size as f64) / (kb as f64)).ceil())
|
format!("{}", ((size as f64) / (multiplier as f64)).ceil())
|
||||||
} else if matches.opt_present("m") {
|
} else if matches.opt_present("m") {
|
||||||
format!("{}", ((size as f64) / (mb as f64)).ceil())
|
format!("{}", ((size as f64) / (multiplier.pow(2) as f64)).ceil())
|
||||||
} else {
|
} else {
|
||||||
format!("{}", ((size as f64) / (block_size as f64)).ceil())
|
format!("{}", ((size as f64) / (block_size as f64)).ceil())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue