From ec4941c8ac45f94ab408753b173ce991ce0fafd3 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Wed, 2 Aug 2023 10:57:58 -0500 Subject: [PATCH] update `format` signature to allow `record` to be passed in (#9898) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description This PR updates the signature of `format` to allow records to be passed in. Closes #9897 ### Before ```nushell {name: Downloads} | format "{name}" × Command does not support record input. ╭─[entry #12:1:1] 1 │ {name: Downloads} | format "{name}" · ───┬── · ╰── command doesn't support record input ╰──── ``` ### After ```nushell {name: Downloads} | format "{name}" Downloads ``` # User-Facing Changes # Tests + Formatting # After Submitting --- .../nu-cmd-extra/src/extra/strings/format/command.rs | 11 +++++++---- crates/nu-command/tests/commands/format.rs | 7 +++++++ crates/nu-command/tests/commands/platform/char_.rs | 12 ++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 crates/nu-command/tests/commands/platform/char_.rs diff --git a/crates/nu-cmd-extra/src/extra/strings/format/command.rs b/crates/nu-cmd-extra/src/extra/strings/format/command.rs index a6b4713019..d5a50e5a62 100644 --- a/crates/nu-cmd-extra/src/extra/strings/format/command.rs +++ b/crates/nu-cmd-extra/src/extra/strings/format/command.rs @@ -1,3 +1,5 @@ +use std::vec; + use nu_engine::{eval_expression, CallExt}; use nu_parser::parse_expression; use nu_protocol::ast::{Call, PathMember}; @@ -17,15 +19,16 @@ impl Command for Format { fn signature(&self) -> Signature { Signature::build("format") - .input_output_types(vec![( - Type::Table(vec![]), - Type::List(Box::new(Type::String)), - )]) + .input_output_types(vec![ + (Type::Table(vec![]), Type::List(Box::new(Type::String))), + (Type::Record(vec![]), Type::Any), + ]) .required( "pattern", SyntaxShape::String, "the pattern to output. e.g.) \"{foo}: {bar}\"", ) + .allow_variants_without_examples(true) .category(Category::Strings) } diff --git a/crates/nu-command/tests/commands/format.rs b/crates/nu-command/tests/commands/format.rs index 858e211491..1c7c827672 100644 --- a/crates/nu-command/tests/commands/format.rs +++ b/crates/nu-command/tests/commands/format.rs @@ -16,6 +16,13 @@ fn creates_the_resulting_string_from_the_given_fields() { assert_eq!(actual.out, "nu has license ISC"); } +#[test] +fn format_input_record_output_string() { + let actual = nu!(r#"{name: Downloads} | format "{name}""#); + + assert_eq!(actual.out, "Downloads"); +} + #[test] fn given_fields_can_be_column_paths() { let actual = nu!( diff --git a/crates/nu-command/tests/commands/platform/char_.rs b/crates/nu-command/tests/commands/platform/char_.rs new file mode 100644 index 0000000000..7a6bdc1309 --- /dev/null +++ b/crates/nu-command/tests/commands/platform/char_.rs @@ -0,0 +1,12 @@ +use nu_test_support::{nu, pipeline}; + +#[test] +fn test_char_list_outputs_table() { + let actual = nu!(pipeline( + r#" + char --list | length + "# + )); + + assert_eq!(actual.out, "107"); +}