mirror of
https://github.com/nushell/nushell.git
synced 2025-02-16 18:41:44 +01:00
Fix column count to not break on empty tables (#2374)
This commit is contained in:
parent
43e061f8c6
commit
6126209f57
@ -39,23 +39,26 @@ impl WholeStreamCommand for Count {
|
|||||||
let (CountArgs { column }, input) = args.process(®istry).await?;
|
let (CountArgs { column }, input) = args.process(®istry).await?;
|
||||||
let rows: Vec<Value> = input.collect().await;
|
let rows: Vec<Value> = input.collect().await;
|
||||||
|
|
||||||
if column {
|
let count = if column {
|
||||||
if let UntaggedValue::Row(dict) = &rows[0].value {
|
if rows.is_empty() {
|
||||||
return Ok(OutputStream::one(
|
0
|
||||||
UntaggedValue::int(dict.length()).into_value(tag),
|
|
||||||
));
|
|
||||||
} else {
|
} else {
|
||||||
return Err(ShellError::labeled_error(
|
match &rows[0].value {
|
||||||
"Cannot obtain column count",
|
UntaggedValue::Row(dictionary) => dictionary.length(),
|
||||||
"cannot obtain column count",
|
_ => {
|
||||||
tag,
|
return Err(ShellError::labeled_error(
|
||||||
));
|
"Cannot obtain column count",
|
||||||
|
"cannot obtain column count",
|
||||||
|
tag,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
rows.len()
|
||||||
|
};
|
||||||
|
|
||||||
Ok(OutputStream::one(
|
Ok(OutputStream::one(UntaggedValue::int(count).into_value(tag)))
|
||||||
UntaggedValue::int(rows.len()).into_value(tag),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
@ -11,3 +11,15 @@ fn count_columns_in_cal_table() {
|
|||||||
|
|
||||||
assert_eq!(actual.out, "7");
|
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");
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user