mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 19:17:43 +00:00
du: pick format for convert_size() once and reverse UNITS (again)
This commit is contained in:
parent
da301be35c
commit
b50aaf6456
1 changed files with 35 additions and 18 deletions
43
src/du/du.rs
43
src/du/du.rs
|
@ -39,7 +39,7 @@ const LONG_HELP: &'static str = "
|
||||||
";
|
";
|
||||||
|
|
||||||
// TODO: Suport Z & Y (currently limited by size of u64)
|
// TODO: Suport Z & Y (currently limited by size of u64)
|
||||||
const UNITS: [(char, u32); 6] = [('K', 1), ('M', 2), ('G', 3), ('T', 4), ('P', 5), ('E', 6)];
|
const UNITS: [(char, u32); 6] = [('E', 6), ('P', 5), ('T', 4), ('G', 3), ('M', 2), ('K', 1)];
|
||||||
|
|
||||||
struct Options {
|
struct Options {
|
||||||
all: bool,
|
all: bool,
|
||||||
|
@ -95,6 +95,7 @@ fn unit_string_to_number(s: &str) -> Option<u64> {
|
||||||
|
|
||||||
let unit = UNITS
|
let unit = UNITS
|
||||||
.iter()
|
.iter()
|
||||||
|
.rev()
|
||||||
.find(|&&(unit_ch, _)| unit_ch == ch)
|
.find(|&&(unit_ch, _)| unit_ch == ch)
|
||||||
.map(|&(_, val)| {
|
.map(|&(_, val)| {
|
||||||
// we found a match, so increment offset
|
// we found a match, so increment offset
|
||||||
|
@ -208,6 +209,28 @@ fn du(
|
||||||
Box::new(stats.into_iter())
|
Box::new(stats.into_iter())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn convert_size_human(size: u64, multiplier: u64, _block_size: u64) -> String {
|
||||||
|
for &(unit, power) in &UNITS {
|
||||||
|
let limit = multiplier.pow(power);
|
||||||
|
if size >= limit {
|
||||||
|
return format!("{:.1}{}", (size as f64) / (limit as f64), unit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
format!("{}B", size)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert_size_k(size: u64, multiplier: u64, _block_size: u64) -> String {
|
||||||
|
format!("{}", ((size as f64) / (multiplier as f64)).ceil())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert_size_m(size: u64, multiplier: u64, _block_size: u64) -> String {
|
||||||
|
format!("{}", ((size as f64) / ((multiplier * multiplier) as f64)).ceil())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert_size_other(size: u64, _multiplier: u64, block_size: u64) -> String {
|
||||||
|
format!("{}", ((size as f64) / (block_size as f64)).ceil())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn uumain(args: Vec<String>) -> i32 {
|
pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let syntax = format!(
|
let syntax = format!(
|
||||||
"[OPTION]... [FILE]...
|
"[OPTION]... [FILE]...
|
||||||
|
@ -309,29 +332,23 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
|
|
||||||
let block_size = read_block_size(matches.opt_str("block-size"));
|
let block_size = read_block_size(matches.opt_str("block-size"));
|
||||||
|
|
||||||
let convert_size = |size: u64| -> String {
|
|
||||||
let multiplier: u64 = if matches.opt_present("si") {
|
let multiplier: u64 = if matches.opt_present("si") {
|
||||||
1000
|
1000
|
||||||
} else {
|
} else {
|
||||||
1024
|
1024
|
||||||
};
|
};
|
||||||
|
let convert_size_fn = {
|
||||||
if matches.opt_present("human-readable") || matches.opt_present("si") {
|
if matches.opt_present("human-readable") || matches.opt_present("si") {
|
||||||
for &(unit, power) in &UNITS {
|
convert_size_human
|
||||||
let limit = multiplier.pow(power);
|
|
||||||
if size >= limit {
|
|
||||||
return format!("{:.1}{}", (size as f64) / (limit as f64), unit);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return format!("{}B", size);
|
|
||||||
} else if matches.opt_present("k") {
|
} else if matches.opt_present("k") {
|
||||||
format!("{}", ((size as f64) / (multiplier as f64)).ceil())
|
convert_size_k
|
||||||
} else if matches.opt_present("m") {
|
} else if matches.opt_present("m") {
|
||||||
format!("{}", ((size as f64) / (multiplier.pow(2) as f64)).ceil())
|
convert_size_m
|
||||||
} else {
|
} else {
|
||||||
format!("{}", ((size as f64) / (block_size as f64)).ceil())
|
convert_size_other
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
let convert_size = |size| convert_size_fn(size, multiplier, block_size);
|
||||||
|
|
||||||
let time_format_str = match matches.opt_str("time-style") {
|
let time_format_str = match matches.opt_str("time-style") {
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue