Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/uu/numfmt/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ numfmt-help-padding = pad the output to N characters; positive N will right-alig
numfmt-help-header = print (without converting) the first N header lines; N defaults to 1 if not specified
numfmt-help-round = use METHOD for rounding when scaling
numfmt-help-suffix = print SUFFIX after each formatted number, and accept inputs optionally ending with SUFFIX
numfmt-help-unit-separator = use STRING to separate the number from any unit when printing; by default, no separator is used
numfmt-help-invalid = set the failure mode for invalid input
numfmt-help-zero-terminated = line delimiter is NUL, not newline

Expand Down
13 changes: 10 additions & 3 deletions src/uu/numfmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ fn transform_to(
opts: &TransformOptions,
round_method: RoundMethod,
precision: usize,
unit_separator: &str,
) -> Result<String> {
let (i2, s) = consider_suffix(s, &opts.to, round_method, precision)?;
let i2 = i2 / (opts.to_unit as f64);
Expand All @@ -286,10 +287,15 @@ fn transform_to(
)
}
Some(s) if precision > 0 => {
format!("{i2:.precision$}{}", DisplayableSuffix(s, opts.to),)
format!(
"{i2:.precision$}{unit_separator}{}",
DisplayableSuffix(s, opts.to),
)
}
Some(s) if i2.abs() < 10.0 => {
format!("{i2:.1}{unit_separator}{}", DisplayableSuffix(s, opts.to))
}
Some(s) if i2.abs() < 10.0 => format!("{i2:.1}{}", DisplayableSuffix(s, opts.to)),
Some(s) => format!("{i2:.0}{}", DisplayableSuffix(s, opts.to)),
Some(s) => format!("{i2:.0}{unit_separator}{}", DisplayableSuffix(s, opts.to)),
})
}

Expand Down Expand Up @@ -317,6 +323,7 @@ fn format_string(
&options.transform,
options.round,
precision,
&options.unit_separator,
)?;

// bring back the suffix before applying padding
Expand Down
13 changes: 13 additions & 0 deletions src/uu/numfmt/src/numfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {

let suffix = args.get_one::<String>(SUFFIX).cloned();

let unit_separator = args
.get_one::<String>(UNIT_SEPARATOR)
.cloned()
.unwrap_or_default();

let invalid = InvalidModes::from_str(args.get_one::<String>(INVALID).unwrap()).unwrap();

let zero_terminated = args.get_flag(ZERO_TERMINATED);
Expand All @@ -246,6 +251,7 @@ fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {
delimiter,
round,
suffix,
unit_separator,
format,
invalid,
zero_terminated,
Expand Down Expand Up @@ -370,6 +376,12 @@ pub fn uu_app() -> Command {
.help(translate!("numfmt-help-suffix"))
.value_name("SUFFIX"),
)
.arg(
Arg::new(UNIT_SEPARATOR)
.long(UNIT_SEPARATOR)
.help(translate!("numfmt-help-unit-separator"))
.value_name("STRING"),
)
.arg(
Arg::new(INVALID)
.long(INVALID)
Expand Down Expand Up @@ -419,6 +431,7 @@ mod tests {
delimiter: None,
round: RoundMethod::Nearest,
suffix: None,
unit_separator: String::new(),
format: FormatOptions::default(),
invalid: InvalidModes::Abort,
zero_terminated: false,
Expand Down
2 changes: 2 additions & 0 deletions src/uu/numfmt/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub const TO: &str = "to";
pub const TO_DEFAULT: &str = "none";
pub const TO_UNIT: &str = "to-unit";
pub const TO_UNIT_DEFAULT: &str = "1";
pub const UNIT_SEPARATOR: &str = "unit-separator";
pub const ZERO_TERMINATED: &str = "zero-terminated";

pub struct TransformOptions {
Expand All @@ -52,6 +53,7 @@ pub struct NumfmtOptions {
pub delimiter: Option<String>,
pub round: RoundMethod,
pub suffix: Option<String>,
pub unit_separator: String,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably should be an OsString as GNU numfmt allows non-UTF8 values. But I think it's something for a future PR.

pub format: FormatOptions,
pub invalid: InvalidModes,
pub zero_terminated: bool,
Expand Down
13 changes: 13 additions & 0 deletions tests/by-util/test_numfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,3 +1115,16 @@ fn test_zero_terminated_embedded_newline() {
// Newlines get replaced by a single space
.stdout_is("1000 2000\x003000 4000\x00");
}

#[test]
fn test_unit_separator() {
for (args, expected) in [
(&["--to=si", "--unit-separator= ", "1000"][..], "1.0 k\n"),
(&["--to=iec", "--unit-separator= ", "1024"], "1.0 K\n"),
(&["--to=iec-i", "--unit-separator= ", "2048"], "2.0 Ki\n"),
(&["--to=si", "--unit-separator=__", "1000"], "1.0__k\n"),
(&["--to=si", "--unit-separator= ", "500"], "500\n"), // no unit = no separator
] {
new_ucmd!().args(args).succeeds().stdout_only(expected);
}
}
Loading