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:
Michael Angerman 2021-11-07 10:18:27 -08:00 committed by GitHub
parent 00a8752c76
commit cfd40ffaf5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 0 deletions

View file

@ -86,6 +86,7 @@ pub fn create_default_context() -> EngineState {
ParEach,
Ps,
Range,
Reverse,
Rm,
Select,
Shuffle,

View file

@ -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;

View 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 {})
}
}