mirror of
https://github.com/nushell/nushell
synced 2025-01-15 06:34:15 +00:00
Port over the reverse command from nushell (#303)
* initial commit of reverse * reverse is working, now move on to the examples * add in working examples for reverse * #[allow(clippy::needless_collect)]
This commit is contained in:
parent
00a8752c76
commit
cfd40ffaf5
3 changed files with 66 additions and 0 deletions
|
@ -86,6 +86,7 @@ pub fn create_default_context() -> EngineState {
|
||||||
ParEach,
|
ParEach,
|
||||||
Ps,
|
Ps,
|
||||||
Range,
|
Range,
|
||||||
|
Reverse,
|
||||||
Rm,
|
Rm,
|
||||||
Select,
|
Select,
|
||||||
Shuffle,
|
Shuffle,
|
||||||
|
|
|
@ -6,6 +6,7 @@ mod length;
|
||||||
mod lines;
|
mod lines;
|
||||||
mod par_each;
|
mod par_each;
|
||||||
mod range;
|
mod range;
|
||||||
|
mod reverse;
|
||||||
mod select;
|
mod select;
|
||||||
mod shuffle;
|
mod shuffle;
|
||||||
mod where_;
|
mod where_;
|
||||||
|
@ -20,6 +21,7 @@ pub use length::Length;
|
||||||
pub use lines::Lines;
|
pub use lines::Lines;
|
||||||
pub use par_each::ParEach;
|
pub use par_each::ParEach;
|
||||||
pub use range::Range;
|
pub use range::Range;
|
||||||
|
pub use reverse::Reverse;
|
||||||
pub use select::Select;
|
pub use select::Select;
|
||||||
pub use shuffle::Shuffle;
|
pub use shuffle::Shuffle;
|
||||||
pub use where_::Where;
|
pub use where_::Where;
|
||||||
|
|
63
crates/nu-command/src/filters/reverse.rs
Normal file
63
crates/nu-command/src/filters/reverse.rs
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{
|
||||||
|
Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Span, Value,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Reverse;
|
||||||
|
|
||||||
|
impl Command for Reverse {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"reverse"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> nu_protocol::Signature {
|
||||||
|
Signature::build("reverse")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Reverses the table."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![Example {
|
||||||
|
example: "[0,1,2,3] | reverse",
|
||||||
|
description: "Reverse the items",
|
||||||
|
result: Some(Value::List {
|
||||||
|
vals: vec![
|
||||||
|
Value::test_int(3),
|
||||||
|
Value::test_int(2),
|
||||||
|
Value::test_int(1),
|
||||||
|
Value::test_int(0),
|
||||||
|
],
|
||||||
|
span: Span::unknown(),
|
||||||
|
}),
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
_stack: &mut Stack,
|
||||||
|
_call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
#[allow(clippy::needless_collect)]
|
||||||
|
let v: Vec<_> = input.into_iter().collect();
|
||||||
|
let iter = v.into_iter().rev();
|
||||||
|
Ok(iter.into_pipeline_data(engine_state.ctrlc.clone()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use crate::test_examples;
|
||||||
|
|
||||||
|
test_examples(Reverse {})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue