refactor(df): use number_prefix like 'ls' instead of doing the display by hand

This commit is contained in:
Sylvestre Ledru 2020-04-29 19:03:22 +02:00 committed by Roy Ivy III
parent 90de33d5d7
commit fc83024ebe
4 changed files with 35 additions and 20 deletions

1
Cargo.lock generated
View file

@ -415,6 +415,7 @@ dependencies = [
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
"number_prefix 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"uucore 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]

View file

@ -10,6 +10,7 @@ path = "src/df.rs"
[dependencies]
clap = "2.32"
libc = "0.2"
number_prefix = "0.2"
uucore = "0.0.1"
[target.'cfg(target_os = "windows")'.dependencies]

View file

@ -12,6 +12,8 @@
extern crate clap;
extern crate libc;
extern crate number_prefix;
#[macro_use]
extern crate uucore;
@ -27,6 +29,7 @@ use kernel32::{
GetVolumeInformationW, GetVolumePathNamesForVolumeNameW, QueryDosDeviceW,
};
use number_prefix::{binary_prefix, decimal_prefix, Prefixed, Standalone};
use std::cell::Cell;
use std::collections::HashMap;
use std::collections::HashSet;
@ -77,7 +80,6 @@ static ABOUT: &str = "Show information about the file system on which each FILE
or all file systems by default.";
static EXIT_OK: i32 = 0;
#[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "windows"))]
static EXIT_ERR: i32 = 1;
#[cfg(windows)]
@ -711,24 +713,20 @@ fn filter_mount_list(vmi: Vec<MountInfo>, paths: &[String], opt: &Options) -> Ve
/// e.g. It returns 1G when value is 1 * 1024 * 1024 * 1024 and base is 1024.
/// Note: It returns `value` if `base` isn't positive.
fn human_readable(value: u64, base: i64) -> String {
#![allow(non_snake_case)]
if base <= 0 {
return value.to_string();
}
let KB: u64 = base as u64;
let MB: u64 = KB * base as u64;
let GB: u64 = MB * base as u64;
let TB: u64 = GB * base as u64;
if value >= TB {
format!("{:.1}T", (value as f64) / (TB as f64))
} else if value >= GB {
format!("{:.1}G", (value as f64) / (GB as f64))
} else if value >= MB {
format!("{:.1}M", (value as f64) / (MB as f64))
} else if value >= KB {
format!("{:.1}K", (value as f64) / (KB as f64))
} else {
format!("{}B", value)
match base {
d if d < 0 => value.to_string(),
1000 => match decimal_prefix(value as f64) {
Standalone(bytes) => bytes.to_string(),
Prefixed(prefix, bytes) => format!("{:.1}{}", bytes, prefix).to_uppercase(),
},
1024 => match binary_prefix(value as f64) {
Standalone(bytes) => bytes.to_string(),
Prefixed(prefix, bytes) => format!("{:.1}{}", bytes, prefix).to_uppercase(),
},
_ => crash!(EXIT_ERR, "Internal error: Unknown base value {}", base),
}
}

View file

@ -1,5 +1,12 @@
use common::util::*;
#[test]
fn test_df_compatible_no_size_arg() {
let (_, mut ucmd) = at_and_ucmd!();
let result = ucmd.arg("-a").run();
assert!(result.success);
}
#[test]
fn test_df_compatible() {
let (_, mut ucmd) = at_and_ucmd!();
@ -13,4 +20,12 @@ fn test_df_compatible_type() {
let result = ucmd.arg("-aT").run();
assert!(result.success);
}
// TODO
#[test]
fn test_df_compatible_si() {
let (_, mut ucmd) = at_and_ucmd!();
let result = ucmd.arg("-aH").run();
assert!(result.success);
}
// ToDO: more tests...