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

41 lines
1018 B
Rust
Raw Normal View History

2021-09-29 20:25:05 +02:00
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
2021-10-25 06:01:02 +02:00
use nu_protocol::{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")
}
fn run(
&self,
_context: &EvaluationContext,
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 {
2021-10-25 06:01:02 +02:00
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 06:01:02 +02:00
val: input.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
}
}
}