From 9fa2b7761112afab5ad993667a1714578f819fa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20B=C3=BCsch?= Date: Thu, 8 Feb 2024 10:21:47 +1100 Subject: [PATCH] Allow specifying a cellpath in `input list` to get the value to display (#11748) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description When using a table (or a list of records) as input to `input list`, allow specifying a cellpath for the field/column to use as the display value. For instance, at the moment, using a table as input results in the following: ``` ❯ [[name price]; [Banana 12] [Kiwi 4] [Pear 7]] | input list > {name: Banana, price: 12} {name: Kiwi, price: 4} {name: Pear, price: 7} ``` With the new `--display` flag introduced by this PR, you can do the following: ``` ❯ [[name price]; [Banana 12] [Kiwi 4] [Pear 7]] | input list -d name > Banana Kiwi Pear ``` Note that it doesn't change what gets returned after selecting an item: the full row/record is still returned. # User-Facing Changes A new optional flag is allowed. # Tests + Formatting # After Submitting --- crates/nu-command/src/platform/input/list.rs | 31 ++++++++++++++++---- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/crates/nu-command/src/platform/input/list.rs b/crates/nu-command/src/platform/input/list.rs index ea88997dc4..990d376fb6 100644 --- a/crates/nu-command/src/platform/input/list.rs +++ b/crates/nu-command/src/platform/input/list.rs @@ -1,7 +1,7 @@ use dialoguer::{console::Term, Select}; use dialoguer::{FuzzySelect, MultiSelect}; use nu_engine::CallExt; -use nu_protocol::ast::Call; +use nu_protocol::ast::{Call, CellPath}; use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::{ Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type, @@ -50,6 +50,12 @@ impl Command for InputList { ) .switch("fuzzy", "Use a fuzzy select.", Some('f')) .switch("index", "Returns list indexes.", Some('i')) + .named( + "display", + SyntaxShape::CellPath, + "Field to use as display value", + Some('d'), + ) .allow_variants_without_examples(true) .category(Category::Platform) } @@ -78,17 +84,27 @@ impl Command for InputList { let multi = call.has_flag(engine_state, stack, "multi")?; let fuzzy = call.has_flag(engine_state, stack, "fuzzy")?; let index = call.has_flag(engine_state, stack, "index")?; + let display_path: Option = call.get_flag(engine_state, stack, "display")?; let options: Vec = match input { PipelineData::Value(Value::Range { .. }, ..) | PipelineData::Value(Value::List { .. }, ..) | PipelineData::ListStream { .. } => input .into_iter() - .map(move |val| Options { - name: val.into_string(", ", engine_state.get_config()), - value: val, + .map(move |val| { + let display_value = if let Some(ref cellpath) = display_path { + val.clone() + .follow_cell_path(&cellpath.members, false)? + .into_string(", ", engine_state.get_config()) + } else { + val.into_string(", ", engine_state.get_config()) + }; + Ok(Options { + name: display_value, + value: val, + }) }) - .collect(), + .collect::, ShellError>>()?, _ => { return Err(ShellError::TypeMismatch { @@ -235,6 +251,11 @@ impl Command for InputList { example: r#"[Banana Kiwi Pear Peach Strawberry] | input list --index"#, result: None, }, + Example { + description: "Choose an item from a table using a column as display value", + example: r#"[[name price]; [Banana 12] [Kiwi 4] [Pear 7]] | input list -d name"#, + result: None, + }, ] } }