port over from nushell drop nth (#498)

This commit is contained in:
Michael Angerman 2021-12-15 04:26:15 -08:00 committed by GitHub
parent e9525627e6
commit 0450cc25e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 125 additions and 0 deletions

View File

@ -51,6 +51,7 @@ pub fn create_default_context() -> EngineState {
Collect,
Drop,
DropColumn,
DropNth,
Each,
First,
Get,

View File

@ -1,5 +1,7 @@
pub mod column;
pub mod command;
pub mod nth;
pub use column::DropColumn;
pub use command::Drop;
pub use nth::DropNth;

View File

@ -0,0 +1,122 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, PipelineIterator, ShellError,
Signature, Span, SyntaxShape, Value,
};
#[derive(Clone)]
pub struct DropNth;
impl Command for DropNth {
fn name(&self) -> &str {
"drop nth"
}
fn signature(&self) -> Signature {
Signature::build("drop nth")
.rest("rest", SyntaxShape::Int, "the number of the row to drop")
.category(Category::Filters)
}
fn usage(&self) -> &str {
"Drop the selected rows."
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
example: "[sam,sarah,2,3,4,5] | drop nth 0 1 2",
description: "Drop the first, second, and third row",
result: Some(Value::List {
vals: vec![Value::test_int(3), Value::test_int(4), Value::test_int(5)],
span: Span::unknown(),
}),
},
Example {
example: "[0,1,2,3,4,5] | drop nth 0 1 2",
description: "Drop the first, second, and third row",
result: Some(Value::List {
vals: vec![Value::test_int(3), Value::test_int(4), Value::test_int(5)],
span: Span::unknown(),
}),
},
Example {
example: "[0,1,2,3,4,5] | drop nth 0 2 4",
description: "Drop rows 0 2 4",
result: Some(Value::List {
vals: vec![Value::test_int(1), Value::test_int(3), Value::test_int(5)],
span: Span::unknown(),
}),
},
Example {
example: "[0,1,2,3,4,5] | drop nth 2 0 4",
description: "Drop rows 2 0 4",
result: Some(Value::List {
vals: vec![Value::test_int(1), Value::test_int(3), Value::test_int(5)],
span: Span::unknown(),
}),
},
]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let mut rows: Vec<usize> = call.rest(engine_state, stack, 0)?;
rows.sort_unstable();
let pipeline_iter: PipelineIterator = input.into_iter();
Ok(DropNthIterator {
input: pipeline_iter,
rows,
current: 0,
}
.into_pipeline_data(engine_state.ctrlc.clone()))
}
}
struct DropNthIterator {
input: PipelineIterator,
rows: Vec<usize>,
current: usize,
}
impl Iterator for DropNthIterator {
type Item = Value;
fn next(&mut self) -> Option<Self::Item> {
loop {
if let Some(row) = self.rows.get(0) {
if self.current == *row {
self.rows.remove(0);
self.current += 1;
let _ = self.input.next();
continue;
} else {
self.current += 1;
return self.input.next();
}
} else {
return self.input.next();
}
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(DropNth {})
}
}