From 4fe7865ad0f8c373a02230c9ae24a2f18ff41208 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Fri, 7 Mar 2025 12:43:35 -0600 Subject: [PATCH] allow --group-digits to be used in `into string` (#15265) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description This PR allows the `into string` command to pass the `--group-digits` flag which already existed in this code but was hard coded to `false`. Now you can do things like ```nushell ❯ 1234567890 | into string --group-digits 1,234,567,890 ❯ ls | into string size --group-digits | last 5 ╭─#─┬────────name─────────┬─type─┬──size──┬───modified───╮ │ 0 │ README.md │ file │ 12,606 │ 4 weeks ago │ │ 1 │ rust-toolchain.toml │ file │ 1,125 │ 2 weeks ago │ │ 2 │ SECURITY.md │ file │ 2,712 │ 7 months ago │ │ 3 │ toolkit.nu │ file │ 21,929 │ 2 months ago │ │ 4 │ typos.toml │ file │ 542 │ 7 months ago │ ╰─#─┴────────name─────────┴─type─┴──size──┴───modified───╯ ❯ "12345" | into string --group-digits 12,345 ``` # User-Facing Changes # Tests + Formatting # After Submitting --- .../nu-command/src/conversions/into/string.rs | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/crates/nu-command/src/conversions/into/string.rs b/crates/nu-command/src/conversions/into/string.rs index 261afe1dd7..42eabc8935 100644 --- a/crates/nu-command/src/conversions/into/string.rs +++ b/crates/nu-command/src/conversions/into/string.rs @@ -1,15 +1,15 @@ -use std::sync::Arc; - use nu_cmd_base::input_handler::{operate, CmdArgument}; use nu_engine::command_prelude::*; use nu_protocol::{shell_error::into_code, Config}; use nu_utils::get_system_locale; use num_format::ToFormattedString; +use std::sync::Arc; struct Arguments { decimals_value: Option, cell_paths: Option>, config: Arc, + group_digits: bool, } impl CmdArgument for Arguments { @@ -52,6 +52,11 @@ impl Command for SubCommand { SyntaxShape::CellPath, "For a data structure input, convert data at the given cell paths.", ) + .switch( + "group-digits", + "group digits together by the locale specific thousands separator", + Some('g'), + ) .named( "decimals", SyntaxShape::Int, @@ -148,6 +153,7 @@ fn string_helper( ) -> Result { let head = call.head; let decimals_value: Option = call.get_flag(engine_state, stack, "decimals")?; + let group_digits = call.has_flag(engine_state, stack, "group-digits")?; if let Some(decimal_val) = decimals_value { if decimal_val.is_negative() { return Err(ShellError::TypeMismatch { @@ -182,6 +188,7 @@ fn string_helper( decimals_value, cell_paths, config, + group_digits, }; operate(action, args, input, head, engine_state.signals()) } @@ -190,10 +197,12 @@ fn string_helper( fn action(input: &Value, args: &Arguments, span: Span) -> Value { let digits = args.decimals_value; let config = &args.config; + let group_digits = args.group_digits; + match input { Value::Int { val, .. } => { let decimal_value = digits.unwrap_or(0) as usize; - let res = format_int(*val, false, decimal_value); + let res = format_int(*val, group_digits, decimal_value); Value::string(res, span) } Value::Float { val, .. } => { @@ -206,11 +215,24 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value { } Value::Bool { val, .. } => Value::string(val.to_string(), span), Value::Date { val, .. } => Value::string(val.format("%c").to_string(), span), - Value::String { val, .. } => Value::string(val.to_string(), span), + Value::String { val, .. } => { + if group_digits { + let number = val.parse::().unwrap_or_default(); + let decimal_value = digits.unwrap_or(0) as usize; + Value::string(format_int(number, group_digits, decimal_value), span) + } else { + Value::string(val.to_string(), span) + } + } Value::Glob { val, .. } => Value::string(val.to_string(), span), - Value::Filesize { val: _, .. } => { - Value::string(input.to_expanded_string(", ", config), span) + Value::Filesize { val, .. } => { + if group_digits { + let decimal_value = digits.unwrap_or(0) as usize; + Value::string(format_int(val.get(), group_digits, decimal_value), span) + } else { + Value::string(input.to_expanded_string(", ", config), span) + } } Value::Duration { val: _, .. } => Value::string(input.to_expanded_string("", config), span),