nushell/crates/nu-cli/src/commands/count.rs

57 lines
1.3 KiB
Rust
Raw Normal View History

2019-10-15 12:19:06 +02:00
use crate::commands::WholeStreamCommand;
use crate::context::CommandRegistry;
2019-10-15 12:19:06 +02:00
use crate::prelude::*;
use futures::stream::StreamExt;
use nu_errors::ShellError;
use nu_protocol::{Signature, UntaggedValue, Value};
2019-10-15 12:19:06 +02:00
pub struct Count;
2020-05-29 10:22:52 +02:00
#[async_trait]
2019-10-15 12:19:06 +02:00
impl WholeStreamCommand for Count {
fn name(&self) -> &str {
"count"
}
fn signature(&self) -> Signature {
Signature::build("count")
}
fn usage(&self) -> &str {
2020-05-12 03:00:55 +02:00
"Show the total number of rows or items."
2019-10-15 12:19:06 +02:00
}
2020-05-29 10:22:52 +02:00
async fn run(
2019-10-15 12:19:06 +02:00
&self,
args: CommandArgs,
_registry: &CommandRegistry,
2019-10-15 12:19:06 +02:00
) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let rows: Vec<Value> = args.input.collect().await;
Ok(OutputStream::one(
UntaggedValue::int(rows.len()).into_value(name),
))
2019-10-15 12:19:06 +02:00
}
2020-05-12 03:00:55 +02:00
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Count the number of entries in a list",
example: "echo [1 2 3 4 5] | count",
result: Some(vec![UntaggedValue::int(5).into()]),
2020-05-12 03:00:55 +02:00
}]
}
2019-10-15 12:19:06 +02:00
}
#[cfg(test)]
mod tests {
use super::Count;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Count {})
}
}