mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 19:17:43 +00:00
Merge pull request #7322 from alexsnaps/issue-7221
numfmt: fix Unit::SI uses lowercase `k` suffix for kilos
This commit is contained in:
commit
81048228cf
5 changed files with 63 additions and 62 deletions
|
@ -278,12 +278,12 @@ fn transform_to(
|
||||||
format!(
|
format!(
|
||||||
"{:.precision$}{}",
|
"{:.precision$}{}",
|
||||||
i2,
|
i2,
|
||||||
DisplayableSuffix(s),
|
DisplayableSuffix(s, opts.to),
|
||||||
precision = precision
|
precision = precision
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Some(s) if i2.abs() < 10.0 => format!("{:.1}{}", i2, DisplayableSuffix(s)),
|
Some(s) if i2.abs() < 10.0 => format!("{:.1}{}", i2, DisplayableSuffix(s, opts.to)),
|
||||||
Some(s) => format!("{:.0}{}", i2, DisplayableSuffix(s)),
|
Some(s) => format!("{:.0}{}", i2, DisplayableSuffix(s, opts.to)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,20 +45,21 @@ pub enum RawSuffix {
|
||||||
|
|
||||||
pub type Suffix = (RawSuffix, WithI);
|
pub type Suffix = (RawSuffix, WithI);
|
||||||
|
|
||||||
pub struct DisplayableSuffix(pub Suffix);
|
pub struct DisplayableSuffix(pub Suffix, pub Unit);
|
||||||
|
|
||||||
impl fmt::Display for DisplayableSuffix {
|
impl fmt::Display for DisplayableSuffix {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let Self((ref raw_suffix, ref with_i)) = *self;
|
let Self((ref raw_suffix, ref with_i), unit) = *self;
|
||||||
match raw_suffix {
|
match (raw_suffix, unit) {
|
||||||
RawSuffix::K => write!(f, "K"),
|
(RawSuffix::K, Unit::Si) => write!(f, "k"),
|
||||||
RawSuffix::M => write!(f, "M"),
|
(RawSuffix::K, _) => write!(f, "K"),
|
||||||
RawSuffix::G => write!(f, "G"),
|
(RawSuffix::M, _) => write!(f, "M"),
|
||||||
RawSuffix::T => write!(f, "T"),
|
(RawSuffix::G, _) => write!(f, "G"),
|
||||||
RawSuffix::P => write!(f, "P"),
|
(RawSuffix::T, _) => write!(f, "T"),
|
||||||
RawSuffix::E => write!(f, "E"),
|
(RawSuffix::P, _) => write!(f, "P"),
|
||||||
RawSuffix::Z => write!(f, "Z"),
|
(RawSuffix::E, _) => write!(f, "E"),
|
||||||
RawSuffix::Y => write!(f, "Y"),
|
(RawSuffix::Z, _) => write!(f, "Z"),
|
||||||
|
(RawSuffix::Y, _) => write!(f, "Y"),
|
||||||
}
|
}
|
||||||
.and_then(|()| match with_i {
|
.and_then(|()| match with_i {
|
||||||
true => write!(f, "i"),
|
true => write!(f, "i"),
|
||||||
|
|
|
@ -86,7 +86,7 @@ fn test_to_si() {
|
||||||
.args(&["--to=si"])
|
.args(&["--to=si"])
|
||||||
.pipe_in("1000\n1100000\n100000000")
|
.pipe_in("1000\n1100000\n100000000")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_is("1.0K\n1.1M\n100M\n");
|
.stdout_is("1.0k\n1.1M\n100M\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -245,15 +245,15 @@ fn test_suffixes() {
|
||||||
// TODO add support for ronna (R) and quetta (Q)
|
// TODO add support for ronna (R) and quetta (Q)
|
||||||
let valid_suffixes = ['K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' /*'R' , 'Q'*/];
|
let valid_suffixes = ['K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' /*'R' , 'Q'*/];
|
||||||
|
|
||||||
// TODO implement special handling of 'K'
|
|
||||||
for c in ('A'..='Z').chain('a'..='z') {
|
for c in ('A'..='Z').chain('a'..='z') {
|
||||||
let args = ["--from=si", "--to=si", &format!("1{c}")];
|
let args = ["--from=si", "--to=si", &format!("1{c}")];
|
||||||
|
|
||||||
if valid_suffixes.contains(&c) {
|
if valid_suffixes.contains(&c) {
|
||||||
|
let s = if c == 'K' { 'k' } else { c };
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only(format!("1.0{c}\n"));
|
.stdout_only(format!("1.0{s}\n"));
|
||||||
} else {
|
} else {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.args(&args)
|
.args(&args)
|
||||||
|
@ -506,7 +506,7 @@ fn test_delimiter_to_si() {
|
||||||
.args(&["-d=,", "--to=si"])
|
.args(&["-d=,", "--to=si"])
|
||||||
.pipe_in("1234,56")
|
.pipe_in("1234,56")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only("1.3K,56\n");
|
.stdout_only("1.3k,56\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -515,7 +515,7 @@ fn test_delimiter_skips_leading_whitespace() {
|
||||||
.args(&["-d=,", "--to=si"])
|
.args(&["-d=,", "--to=si"])
|
||||||
.pipe_in(" \t 1234,56")
|
.pipe_in(" \t 1234,56")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only("1.3K,56\n");
|
.stdout_only("1.3k,56\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -524,7 +524,7 @@ fn test_delimiter_preserves_leading_whitespace_in_unselected_fields() {
|
||||||
.args(&["-d=|", "--to=si"])
|
.args(&["-d=|", "--to=si"])
|
||||||
.pipe_in(" 1000| 2000")
|
.pipe_in(" 1000| 2000")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only("1.0K| 2000\n");
|
.stdout_only("1.0k| 2000\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -552,7 +552,7 @@ fn test_delimiter_with_padding() {
|
||||||
.args(&["-d=|", "--to=si", "--padding=5"])
|
.args(&["-d=|", "--to=si", "--padding=5"])
|
||||||
.pipe_in("1000|2000")
|
.pipe_in("1000|2000")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only(" 1.0K|2000\n");
|
.stdout_only(" 1.0k|2000\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -561,21 +561,21 @@ fn test_delimiter_with_padding_and_fields() {
|
||||||
.args(&["-d=|", "--to=si", "--padding=5", "--field=-"])
|
.args(&["-d=|", "--to=si", "--padding=5", "--field=-"])
|
||||||
.pipe_in("1000|2000")
|
.pipe_in("1000|2000")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only(" 1.0K| 2.0K\n");
|
.stdout_only(" 1.0k| 2.0k\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_round() {
|
fn test_round() {
|
||||||
for (method, exp) in [
|
for (method, exp) in [
|
||||||
("from-zero", ["9.1K", "-9.1K", "9.1K", "-9.1K"]),
|
("from-zero", ["9.1k", "-9.1k", "9.1k", "-9.1k"]),
|
||||||
("from-zer", ["9.1K", "-9.1K", "9.1K", "-9.1K"]),
|
("from-zer", ["9.1k", "-9.1k", "9.1k", "-9.1k"]),
|
||||||
("f", ["9.1K", "-9.1K", "9.1K", "-9.1K"]),
|
("f", ["9.1k", "-9.1k", "9.1k", "-9.1k"]),
|
||||||
("towards-zero", ["9.0K", "-9.0K", "9.0K", "-9.0K"]),
|
("towards-zero", ["9.0k", "-9.0k", "9.0k", "-9.0k"]),
|
||||||
("up", ["9.1K", "-9.0K", "9.1K", "-9.0K"]),
|
("up", ["9.1k", "-9.0k", "9.1k", "-9.0k"]),
|
||||||
("down", ["9.0K", "-9.1K", "9.0K", "-9.1K"]),
|
("down", ["9.0k", "-9.1k", "9.0k", "-9.1k"]),
|
||||||
("nearest", ["9.0K", "-9.0K", "9.1K", "-9.1K"]),
|
("nearest", ["9.0k", "-9.0k", "9.1k", "-9.1k"]),
|
||||||
("near", ["9.0K", "-9.0K", "9.1K", "-9.1K"]),
|
("near", ["9.0k", "-9.0k", "9.1k", "-9.1k"]),
|
||||||
("n", ["9.0K", "-9.0K", "9.1K", "-9.1K"]),
|
("n", ["9.0k", "-9.0k", "9.1k", "-9.1k"]),
|
||||||
] {
|
] {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.args(&[
|
.args(&[
|
||||||
|
@ -651,7 +651,7 @@ fn test_transform_with_suffix_on_input() {
|
||||||
.args(&["--suffix=b", "--to=si"])
|
.args(&["--suffix=b", "--to=si"])
|
||||||
.pipe_in("2000b")
|
.pipe_in("2000b")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only("2.0Kb\n");
|
.stdout_only("2.0kb\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -660,7 +660,7 @@ fn test_transform_without_suffix_on_input() {
|
||||||
.args(&["--suffix=b", "--to=si"])
|
.args(&["--suffix=b", "--to=si"])
|
||||||
.pipe_in("2000")
|
.pipe_in("2000")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only("2.0Kb\n");
|
.stdout_only("2.0kb\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -669,7 +669,7 @@ fn test_transform_with_suffix_and_delimiter() {
|
||||||
.args(&["--suffix=b", "--to=si", "-d=|"])
|
.args(&["--suffix=b", "--to=si", "-d=|"])
|
||||||
.pipe_in("1000b|2000|3000")
|
.pipe_in("1000b|2000|3000")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only("1.0Kb|2000|3000\n");
|
.stdout_only("1.0kb|2000|3000\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
2
tests/fixtures/numfmt/df_expected.txt
vendored
2
tests/fixtures/numfmt/df_expected.txt
vendored
|
@ -3,6 +3,6 @@ udev 8.2G 0 8.2G 0% /dev
|
||||||
tmpfs 1.7G 2.1M 1.7G 1% /run
|
tmpfs 1.7G 2.1M 1.7G 1% /run
|
||||||
/dev/nvme0n1p2 1.1T 433G 523G 46% /
|
/dev/nvme0n1p2 1.1T 433G 523G 46% /
|
||||||
tmpfs 8.3G 145M 8.1G 2% /dev/shm
|
tmpfs 8.3G 145M 8.1G 2% /dev/shm
|
||||||
tmpfs 5.3M 4.1K 5.3M 1% /run/lock
|
tmpfs 5.3M 4.1k 5.3M 1% /run/lock
|
||||||
tmpfs 8.3G 0 8.3G 0% /sys/fs/cgroup
|
tmpfs 8.3G 0 8.3G 0% /sys/fs/cgroup
|
||||||
/dev/nvme0n1p1 536M 8.2M 528M 2% /boot/efi
|
/dev/nvme0n1p1 536M 8.2M 528M 2% /boot/efi
|
||||||
|
|
54
tests/fixtures/numfmt/gnutest_si_result.txt
vendored
54
tests/fixtures/numfmt/gnutest_si_result.txt
vendored
|
@ -1,34 +1,34 @@
|
||||||
-1.1K
|
-1.1k
|
||||||
-1.0K
|
-1.0k
|
||||||
-999
|
-999
|
||||||
1
|
1
|
||||||
500
|
500
|
||||||
999
|
999
|
||||||
1.0K
|
1.0k
|
||||||
1.0K
|
1.0k
|
||||||
1.1K
|
1.1k
|
||||||
1.1K
|
1.1k
|
||||||
9.9K
|
9.9k
|
||||||
10K
|
10k
|
||||||
10K
|
10k
|
||||||
10K
|
10k
|
||||||
10K
|
10k
|
||||||
10K
|
10k
|
||||||
11K
|
11k
|
||||||
11K
|
11k
|
||||||
11K
|
11k
|
||||||
50K
|
50k
|
||||||
99K
|
99k
|
||||||
100K
|
100k
|
||||||
100K
|
100k
|
||||||
100K
|
100k
|
||||||
100K
|
100k
|
||||||
100K
|
100k
|
||||||
101K
|
101k
|
||||||
101K
|
101k
|
||||||
101K
|
101k
|
||||||
102K
|
102k
|
||||||
999K
|
999k
|
||||||
1.0M
|
1.0M
|
||||||
1.0M
|
1.0M
|
||||||
1.0M
|
1.0M
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue