diff --git a/src/cli.rs b/src/cli.rs index 03fb9ac24b..a76ba20845 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -159,6 +159,7 @@ pub async fn cli() -> Result<(), Box> { whole_stream_command(SplitRow), whole_stream_command(Lines), whole_stream_command(Reject), + whole_stream_command(Reverse), whole_stream_command(Trim), whole_stream_command(ToArray), whole_stream_command(ToCSV), diff --git a/src/commands.rs b/src/commands.rs index a57219a22b..bde49d804f 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -35,6 +35,7 @@ crate mod pick; crate mod plugin; crate mod prev; crate mod ps; +crate mod reverse; crate mod reject; crate mod rm; crate mod save; @@ -91,6 +92,7 @@ crate use pick::Pick; crate use prev::Previous; crate use ps::PS; crate use reject::Reject; +crate use reverse::Reverse; crate use rm::Remove; crate use save::Save; crate use shells::Shells; diff --git a/src/commands/reverse.rs b/src/commands/reverse.rs new file mode 100644 index 0000000000..2b43f01470 --- /dev/null +++ b/src/commands/reverse.rs @@ -0,0 +1,38 @@ +use crate::commands::WholeStreamCommand; +use crate::errors::ShellError; +use crate::parser::CommandRegistry; +use crate::prelude::*; + +pub struct Reverse; + +impl WholeStreamCommand for Reverse { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + reverse(args, registry) + } + + fn name(&self) -> &str { + "reverse" + } + + fn signature(&self) -> Signature { + Signature::build("reverse") + } +} + +fn reverse(args: CommandArgs, registry: &CommandRegistry) -> Result { + let args = args.evaluate_once(registry)?; + let (input, _args) = args.parts(); + + let output = input.values.collect::>(); + + let output = output.map(move |mut vec| { + vec.reverse(); + vec.into_iter().collect::>() + }); + + Ok(output.flatten_stream().from_input_stream()) +} diff --git a/tests/filters_test.rs b/tests/filters_test.rs index c2678b22be..0fbb5635be 100644 --- a/tests/filters_test.rs +++ b/tests/filters_test.rs @@ -111,3 +111,14 @@ fn can_get_last() { assert_eq!(output, "utf16.ini"); } + +#[test] +fn can_get_reverse_first() { + nu!( + output, + cwd("tests/fixtures/formats"), + "ls | sort-by name | reverse | first 1 | get name | trim | echo $it" + ); + + assert_eq!(output, "utf16.ini"); +}