diff --git a/crates/nu-cli/src/commands/count.rs b/crates/nu-cli/src/commands/count.rs index 969b79f31e..95dc2da116 100644 --- a/crates/nu-cli/src/commands/count.rs +++ b/crates/nu-cli/src/commands/count.rs @@ -39,23 +39,26 @@ impl WholeStreamCommand for Count { let (CountArgs { column }, input) = args.process(®istry).await?; let rows: Vec = input.collect().await; - if column { - if let UntaggedValue::Row(dict) = &rows[0].value { - return Ok(OutputStream::one( - UntaggedValue::int(dict.length()).into_value(tag), - )); + let count = if column { + if rows.is_empty() { + 0 } else { - return Err(ShellError::labeled_error( - "Cannot obtain column count", - "cannot obtain column count", - tag, - )); + match &rows[0].value { + UntaggedValue::Row(dictionary) => dictionary.length(), + _ => { + return Err(ShellError::labeled_error( + "Cannot obtain column count", + "cannot obtain column count", + tag, + )); + } + } } - } + } else { + rows.len() + }; - Ok(OutputStream::one( - UntaggedValue::int(rows.len()).into_value(tag), - )) + Ok(OutputStream::one(UntaggedValue::int(count).into_value(tag))) } fn examples(&self) -> Vec { diff --git a/crates/nu-cli/tests/commands/count.rs b/crates/nu-cli/tests/commands/count.rs index aecadd6119..33f7663cff 100644 --- a/crates/nu-cli/tests/commands/count.rs +++ b/crates/nu-cli/tests/commands/count.rs @@ -11,3 +11,15 @@ fn count_columns_in_cal_table() { assert_eq!(actual.out, "7"); } + +#[test] +fn count_columns_no_rows() { + let actual = nu!( + cwd: ".", pipeline( + r#" + echo [] | count -c + "# + )); + + assert_eq!(actual.out, "0"); +}