diff --git a/crates/nu-command/src/filters/length.rs b/crates/nu-command/src/filters/length.rs index 9aec125d43..ebcfb24752 100644 --- a/crates/nu-command/src/filters/length.rs +++ b/crates/nu-command/src/filters/length.rs @@ -1,9 +1,7 @@ -use nu_engine::column::get_columns; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::{ - Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError, - Signature, Span, Type, Value, + Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value, }; #[derive(Clone)] @@ -15,7 +13,7 @@ impl Command for Length { } fn usage(&self) -> &str { - "Count the number of elements in the input." + "Count the number of items in an input list or rows in a table." } fn signature(&self) -> nu_protocol::Signature { @@ -24,7 +22,6 @@ impl Command for Length { (Type::List(Box::new(Type::Any)), Type::Int), (Type::Table(vec![]), Type::Int), ]) - .switch("column", "Show the number of columns in a table", Some('c')) .category(Category::Filters) } @@ -34,17 +31,12 @@ impl Command for Length { fn run( &self, - engine_state: &EngineState, + _engine_state: &EngineState, _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { - let col = call.has_flag("column"); - if col { - length_col(engine_state, call, input) - } else { - length_row(call, input) - } + length_row(call, input) } fn examples(&self) -> Vec { @@ -55,32 +47,29 @@ impl Command for Length { result: Some(Value::test_int(5)), }, Example { - description: "Count the number of columns in a table", - example: "[{columnA: A0 columnB: B0}] | length -c", + description: "Count the number of rows in a table", + example: "[{a:1 b:2}, {a:2 b:3}] | length", result: Some(Value::test_int(2)), }, ] } } -// this simulates calling input | columns | length -fn length_col( - engine_state: &EngineState, - call: &Call, - input: PipelineData, -) -> Result { - length_row( - call, - getcol(engine_state, call.head, input) - .expect("getcol() should not fail used in column command"), - ) -} - fn length_row(call: &Call, input: PipelineData) -> Result { match input { PipelineData::Value(Value::Nothing { .. }, ..) => { Ok(Value::int(0, call.head).into_pipeline_data()) } + // I added this here because input_output_type() wasn't catching a record + // being sent in as input from echo. e.g. "echo {a:1 b:2} | length" + PipelineData::Value(Value::Record { span, .. }, ..) => { + Err(ShellError::OnlySupportsThisInputType { + exp_input_type: "list, and table".into(), + wrong_type: "record".into(), + dst_span: call.head, + src_span: span, + }) + } _ => { let mut count: i64 = 0; // Check for and propagate errors @@ -95,43 +84,6 @@ fn length_row(call: &Call, input: PipelineData) -> Result Result { - match input { - PipelineData::Empty => Ok(PipelineData::Empty), - PipelineData::Value( - Value::List { - vals: input_vals, - span, - }, - .., - ) => { - let input_cols = get_columns(&input_vals); - Ok(input_cols - .into_iter() - .map(move |x| Value::String { val: x, span }) - .into_pipeline_data(engine_state.ctrlc.clone())) - } - PipelineData::ListStream(stream, ..) => { - let v: Vec<_> = stream.into_iter().collect(); - let input_cols = get_columns(&v); - - Ok(input_cols - .into_iter() - .map(move |x| Value::String { val: x, span }) - .into_pipeline_data(engine_state.ctrlc.clone())) - } - PipelineData::Value(..) | PipelineData::ExternalStream { .. } => { - let cols = vec![]; - let vals = vec![]; - Ok(Value::Record { cols, vals, span }.into_pipeline_data()) - } - } -} - #[cfg(test)] mod test { use super::*; diff --git a/crates/nu-command/tests/commands/first.rs b/crates/nu-command/tests/commands/first.rs index 5b53007a23..b8fe816e1b 100644 --- a/crates/nu-command/tests/commands/first.rs +++ b/crates/nu-command/tests/commands/first.rs @@ -40,9 +40,10 @@ fn gets_first_row_when_no_amount_given() { Playground::setup("first_test_3", |dirs, sandbox| { sandbox.with_files(vec![EmptyFile("caballeros.txt"), EmptyFile("arepas.clu")]); - let actual = nu!(cwd: dirs.test(), "ls | first | length"); + // FIXME: We should probably change first to return a one row table instead of a record here + let actual = nu!(cwd: dirs.test(), "ls | first | values | length"); - assert_eq!(actual.out, "1"); + assert_eq!(actual.out, "4"); }) } diff --git a/crates/nu-command/tests/commands/last.rs b/crates/nu-command/tests/commands/last.rs index f84225b950..013c1e7391 100644 --- a/crates/nu-command/tests/commands/last.rs +++ b/crates/nu-command/tests/commands/last.rs @@ -33,9 +33,10 @@ fn gets_last_row_when_no_amount_given() { Playground::setup("last_test_2", |dirs, sandbox| { sandbox.with_files(vec![EmptyFile("caballeros.txt"), EmptyFile("arepas.clu")]); - let actual = nu!(cwd: dirs.test(), "ls | last | length"); + // FIXME: We should probably change last to return a one row table instead of a record here + let actual = nu!(cwd: dirs.test(), "ls | last | values | length"); - assert_eq!(actual.out, "1"); + assert_eq!(actual.out, "4"); }) } diff --git a/crates/nu-command/tests/commands/length.rs b/crates/nu-command/tests/commands/length.rs index be7332d55c..9cf073a721 100644 --- a/crates/nu-command/tests/commands/length.rs +++ b/crates/nu-command/tests/commands/length.rs @@ -2,14 +2,21 @@ use nu_test_support::nu; #[test] fn length_columns_in_cal_table() { - let actual = nu!("cal | length -c"); + let actual = nu!("cal | columns | length"); assert_eq!(actual.out, "7"); } #[test] fn length_columns_no_rows() { - let actual = nu!("echo [] | length -c"); + let actual = nu!("echo [] | length"); assert_eq!(actual.out, "0"); } + +#[test] +fn length_fails_on_echo_record() { + let actual = nu!("echo {a:1 b:2} | length"); + + assert_eq!(actual.err.contains("only_supports_this_input_type"), true); +} diff --git a/src/tests/test_table_operations.rs b/src/tests/test_table_operations.rs index ae4e36585f..b854c13655 100644 --- a/src/tests/test_table_operations.rs +++ b/src/tests/test_table_operations.rs @@ -264,7 +264,7 @@ fn update_will_insert() -> TestResult { #[test] fn length_for_columns() -> TestResult { run_test( - r#"[[name,age,grade]; [bill,20,a] [a b c]] | length -c"#, + r#"[[name,age,grade]; [bill,20,a] [a b c]] | columns | length"#, "3", ) }