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

54 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::{ReturnSuccess, Signature, UntaggedValue, Value};
2019-10-15 12:19:06 +02:00
pub struct Count;
#[derive(Deserialize)]
pub struct CountArgs {}
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
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
args.process(registry, count)?.run()
}
2020-05-12 03:00:55 +02:00
fn examples(&self) -> &[Example] {
&[Example {
description: "Count the number of files/directories in the current directory",
example: "ls | count",
}]
}
2019-10-15 12:19:06 +02:00
}
pub fn count(
CountArgs {}: CountArgs,
RunnableContext { input, name, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> {
let stream = async_stream! {
let rows: Vec<Value> = input.collect().await;
2019-10-15 12:19:06 +02:00
yield ReturnSuccess::value(UntaggedValue::int(rows.len()).into_value(name))
2019-10-15 12:19:06 +02:00
};
Ok(stream.to_output_stream())
}