nushell/crates/nu-command/src/filters/length.rs

42 lines
1.1 KiB
Rust
Raw Normal View History

2021-09-29 20:25:05 +02:00
use nu_protocol::ast::Call;
2021-10-25 18:58:58 +02:00
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, IntoPipelineData, PipelineData, Signature, Value};
2021-09-29 20:25:05 +02:00
2021-10-25 06:01:02 +02:00
#[derive(Clone)]
2021-09-29 20:25:05 +02:00
pub struct Length;
impl Command for Length {
fn name(&self) -> &str {
"length"
}
fn usage(&self) -> &str {
"Count the number of elements in the input."
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("length").category(Category::Filters)
2021-09-29 20:25:05 +02:00
}
fn run(
&self,
2021-10-25 08:31:39 +02:00
_engine_state: &EngineState,
_stack: &mut Stack,
2021-09-29 20:25:05 +02:00
call: &Call,
2021-10-25 06:01:02 +02:00
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
2021-09-29 20:25:05 +02:00
match input {
PipelineData::Value(Value::Nothing { .. }, ..) => Ok(Value::Int {
2021-09-29 20:25:05 +02:00
val: 0,
span: call.head,
2021-10-25 06:01:02 +02:00
}
.into_pipeline_data()),
2021-09-29 20:25:05 +02:00
_ => Ok(Value::Int {
2021-10-25 23:14:21 +02:00
val: input.into_iter().count() as i64,
2021-09-29 20:25:05 +02:00
span: call.head,
2021-10-25 06:01:02 +02:00
}
.into_pipeline_data()),
2021-09-29 20:25:05 +02:00
}
}
}