diff --git a/crates/nu-command/src/core_commands/describe.rs b/crates/nu-command/src/core_commands/describe.rs index e44268ee9e..8b92454793 100644 --- a/crates/nu-command/src/core_commands/describe.rs +++ b/crates/nu-command/src/core_commands/describe.rs @@ -19,6 +19,11 @@ impl Command for Describe { fn signature(&self) -> Signature { Signature::build("describe") .input_output_types(vec![(Type::Any, Type::String)]) + .switch( + "no-collect", + "do not collect streams of structured data", + Some('n'), + ) .category(Category::Core) } @@ -30,32 +35,58 @@ impl Command for Describe { input: PipelineData, ) -> Result { let head = call.head; - if matches!(input, PipelineData::ExternalStream { .. }) { - Ok(PipelineData::Value( - Value::string("raw input", call.head), - None, - )) - } else { - let value = input.into_value(call.head); - let description = match value { - Value::CustomValue { val, .. } => val.value_string(), - _ => value.get_type().to_string(), - }; - Ok(Value::String { - val: description, - span: head, + let no_collect: bool = call.has_flag("no-collect"); + + let description = match input { + PipelineData::ExternalStream { .. } => "raw input".into(), + PipelineData::ListStream(_, _) => { + if no_collect { + "stream".into() + } else { + let value = input.into_value(head); + let base_description = match value { + Value::CustomValue { val, .. } => val.value_string(), + _ => value.get_type().to_string(), + }; + + format!("{base_description} (stream)") + } } - .into_pipeline_data()) + _ => { + let value = input.into_value(head); + match value { + Value::CustomValue { val, .. } => val.value_string(), + _ => value.get_type().to_string(), + } + } + }; + + Ok(Value::String { + val: description, + span: head, } + .into_pipeline_data()) } fn examples(&self) -> Vec { - vec![Example { - description: "Describe the type of a string", - example: "'hello' | describe", - result: Some(Value::test_string("string")), - }] + vec![ + Example { + description: "Describe the type of a string", + example: "'hello' | describe", + result: Some(Value::test_string("string")), + }, + Example { + description: "Describe a stream of data, collecting it first", + example: "[1 2 3] | each {|i| $i} | describe", + result: Some(Value::test_string("list (stream)")), + }, + Example { + description: "Describe the input but do not collect streams", + example: "[1 2 3] | each {|i| $i} | describe --no-collect", + result: Some(Value::test_string("stream")), + }, + ] } fn search_terms(&self) -> Vec<&str> { diff --git a/crates/nu-command/src/example_test.rs b/crates/nu-command/src/example_test.rs index 65584acac3..1346bd0ad1 100644 --- a/crates/nu-command/src/example_test.rs +++ b/crates/nu-command/src/example_test.rs @@ -13,7 +13,7 @@ mod test_examples { MathRound, Path, Random, Split, SplitColumn, SplitRow, Str, StrJoin, StrLength, StrReplace, Url, Values, Wrap, }; - use crate::{Break, Mut, To}; + use crate::{Break, Each, Mut, To}; use itertools::Itertools; use nu_protocol::{ ast::Block, @@ -61,6 +61,7 @@ mod test_examples { // Base functions that are needed for testing // Try to keep this working set small to keep tests running as fast as possible let mut working_set = StateWorkingSet::new(&engine_state); + working_set.add_decl(Box::new(Each)); working_set.add_decl(Box::new(Let)); working_set.add_decl(Box::new(Str)); working_set.add_decl(Box::new(StrJoin)); diff --git a/crates/nu-command/src/filters/par_each.rs b/crates/nu-command/src/filters/par_each.rs index d2f87b15dd..8d561bccce 100644 --- a/crates/nu-command/src/filters/par_each.rs +++ b/crates/nu-command/src/filters/par_each.rs @@ -375,7 +375,7 @@ mod test { r#"[7,8,9,10] | par-each {|el ind| $ind } | describe"# )); - assert_eq!(actual.out, "list"); + assert_eq!(actual.out, "list (stream)"); } #[test] diff --git a/crates/nu-command/tests/commands/first.rs b/crates/nu-command/tests/commands/first.rs index 4c3e1bc05b..fb53b6b023 100644 --- a/crates/nu-command/tests/commands/first.rs +++ b/crates/nu-command/tests/commands/first.rs @@ -77,7 +77,7 @@ fn gets_first_row_as_list_when_amount_given() { "# )); - assert_eq!(actual.out, "list"); + assert_eq!(actual.out, "list (stream)"); } #[test] diff --git a/crates/nu-command/tests/commands/last.rs b/crates/nu-command/tests/commands/last.rs index d9eb651648..cf7cc6048e 100644 --- a/crates/nu-command/tests/commands/last.rs +++ b/crates/nu-command/tests/commands/last.rs @@ -76,7 +76,7 @@ fn gets_last_row_as_list_when_amount_given() { "# )); - assert_eq!(actual.out, "list"); + assert_eq!(actual.out, "list (stream)"); } #[test] diff --git a/crates/nu-command/tests/commands/seq.rs b/crates/nu-command/tests/commands/seq.rs index 5de7875166..2994051125 100644 --- a/crates/nu-command/tests/commands/seq.rs +++ b/crates/nu-command/tests/commands/seq.rs @@ -9,7 +9,7 @@ fn float_in_seq_leads_to_lists_of_floats() { "# )); - assert_eq!(actual.out, "list"); + assert_eq!(actual.out, "list (stream)"); } #[test] @@ -21,5 +21,5 @@ fn ints_in_seq_leads_to_lists_of_ints() { "# )); - assert_eq!(actual.out, "list"); + assert_eq!(actual.out, "list (stream)"); }