1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

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)", "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)", "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)", "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)", "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)", "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]

View file

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

View file

@ -12,6 +12,8 @@
extern crate clap; extern crate clap;
extern crate libc; extern crate libc;
extern crate number_prefix;
#[macro_use] #[macro_use]
extern crate uucore; extern crate uucore;
@ -27,6 +29,7 @@ use kernel32::{
GetVolumeInformationW, GetVolumePathNamesForVolumeNameW, QueryDosDeviceW, GetVolumeInformationW, GetVolumePathNamesForVolumeNameW, QueryDosDeviceW,
}; };
use number_prefix::{binary_prefix, decimal_prefix, Prefixed, Standalone};
use std::cell::Cell; use std::cell::Cell;
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::HashSet; 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."; or all file systems by default.";
static EXIT_OK: i32 = 0; static EXIT_OK: i32 = 0;
#[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "windows"))]
static EXIT_ERR: i32 = 1; static EXIT_ERR: i32 = 1;
#[cfg(windows)] #[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. /// 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. /// Note: It returns `value` if `base` isn't positive.
fn human_readable(value: u64, base: i64) -> String { fn human_readable(value: u64, base: i64) -> String {
#![allow(non_snake_case)] match base {
if base <= 0 { d if d < 0 => value.to_string(),
return value.to_string();
} 1000 => match decimal_prefix(value as f64) {
let KB: u64 = base as u64; Standalone(bytes) => bytes.to_string(),
let MB: u64 = KB * base as u64; Prefixed(prefix, bytes) => format!("{:.1}{}", bytes, prefix).to_uppercase(),
let GB: u64 = MB * base as u64; },
let TB: u64 = GB * base as u64;
if value >= TB { 1024 => match binary_prefix(value as f64) {
format!("{:.1}T", (value as f64) / (TB as f64)) Standalone(bytes) => bytes.to_string(),
} else if value >= GB { Prefixed(prefix, bytes) => format!("{:.1}{}", bytes, prefix).to_uppercase(),
format!("{:.1}G", (value as f64) / (GB as f64)) },
} else if value >= MB {
format!("{:.1}M", (value as f64) / (MB as f64)) _ => crash!(EXIT_ERR, "Internal error: Unknown base value {}", base),
} else if value >= KB {
format!("{:.1}K", (value as f64) / (KB as f64))
} else {
format!("{}B", value)
} }
} }

View file

@ -1,5 +1,12 @@
use common::util::*; 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] #[test]
fn test_df_compatible() { fn test_df_compatible() {
let (_, mut ucmd) = at_and_ucmd!(); let (_, mut ucmd) = at_and_ucmd!();
@ -13,4 +20,12 @@ fn test_df_compatible_type() {
let result = ucmd.arg("-aT").run(); let result = ucmd.arg("-aT").run();
assert!(result.success); 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...