mirror of
https://github.com/nushell/nushell
synced 2025-01-15 22:54:16 +00:00
Port first command to engine-q
This commit is contained in:
parent
e549c1112b
commit
ef56d482b2
3 changed files with 89 additions and 0 deletions
|
@ -32,6 +32,7 @@ pub fn create_default_context() -> EngineState {
|
||||||
Each,
|
Each,
|
||||||
ExportDef,
|
ExportDef,
|
||||||
External,
|
External,
|
||||||
|
First,
|
||||||
For,
|
For,
|
||||||
From,
|
From,
|
||||||
FromJson,
|
FromJson,
|
||||||
|
|
86
crates/nu-command/src/filters/first.rs
Normal file
86
crates/nu-command/src/filters/first.rs
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
use nu_engine::CallExt;
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{Example, IntoPipelineData, PipelineData, Signature, Span, SyntaxShape, Value};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct First;
|
||||||
|
|
||||||
|
impl Command for First {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"first"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("first").optional(
|
||||||
|
"rows",
|
||||||
|
SyntaxShape::Int,
|
||||||
|
"starting from the front, the number of rows to return",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Show only the first number of rows."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
first_helper(engine_state, stack, call, input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![
|
||||||
|
Example {
|
||||||
|
description: "Return the first item of a list/table",
|
||||||
|
example: "[1 2 3] | first",
|
||||||
|
result: Some(Value::List {
|
||||||
|
vals: vec![Value::test_int(1)],
|
||||||
|
span: Span::unknown(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Return the first 2 items of a list/table",
|
||||||
|
example: "[1 2 3] | first 2",
|
||||||
|
result: Some(Value::List {
|
||||||
|
vals: vec![Value::test_int(1), Value::test_int(2)],
|
||||||
|
span: Span::unknown(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn first_helper(
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
let rows: Option<i64> = call.opt(engine_state, stack, 0)?;
|
||||||
|
let rows_desired: usize = match rows {
|
||||||
|
Some(x) => x as usize,
|
||||||
|
None => 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Value::List {
|
||||||
|
vals: input.into_iter().take(rows_desired).collect(),
|
||||||
|
span: call.head,
|
||||||
|
}
|
||||||
|
.into_pipeline_data())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use crate::test_examples;
|
||||||
|
|
||||||
|
test_examples(First {})
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
mod each;
|
mod each;
|
||||||
|
mod first;
|
||||||
mod get;
|
mod get;
|
||||||
mod last;
|
mod last;
|
||||||
mod length;
|
mod length;
|
||||||
|
@ -10,6 +11,7 @@ mod where_;
|
||||||
mod wrap;
|
mod wrap;
|
||||||
|
|
||||||
pub use each::Each;
|
pub use each::Each;
|
||||||
|
pub use first::First;
|
||||||
pub use get::Get;
|
pub use get::Get;
|
||||||
pub use last::Last;
|
pub use last::Last;
|
||||||
pub use length::Length;
|
pub use length::Length;
|
||||||
|
|
Loading…
Reference in a new issue