diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index a5946fb18b..193b8bcb0d 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -86,6 +86,7 @@ pub fn create_default_context() -> EngineState { ParEach, Ps, Range, + Reverse, Rm, Select, Shuffle, diff --git a/crates/nu-command/src/filters/mod.rs b/crates/nu-command/src/filters/mod.rs index 0adfe1128d..4b30539b5f 100644 --- a/crates/nu-command/src/filters/mod.rs +++ b/crates/nu-command/src/filters/mod.rs @@ -6,6 +6,7 @@ mod length; mod lines; mod par_each; mod range; +mod reverse; mod select; mod shuffle; mod where_; @@ -20,6 +21,7 @@ pub use length::Length; pub use lines::Lines; pub use par_each::ParEach; pub use range::Range; +pub use reverse::Reverse; pub use select::Select; pub use shuffle::Shuffle; pub use where_::Where; diff --git a/crates/nu-command/src/filters/reverse.rs b/crates/nu-command/src/filters/reverse.rs new file mode 100644 index 0000000000..c62d1e2bb1 --- /dev/null +++ b/crates/nu-command/src/filters/reverse.rs @@ -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 { + 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 { + #[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 {}) + } +}