Merge pull request #421 from ramonsnir/reverse

add reverse
This commit is contained in:
Jonathan Turner 2019-08-26 05:41:07 +12:00 committed by GitHub
commit dda4a707a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 0 deletions

View File

@ -159,6 +159,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
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),

View File

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

38
src/commands/reverse.rs Normal file
View File

@ -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<OutputStream, ShellError> {
reverse(args, registry)
}
fn name(&self) -> &str {
"reverse"
}
fn signature(&self) -> Signature {
Signature::build("reverse")
}
}
fn reverse(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once(registry)?;
let (input, _args) = args.parts();
let output = input.values.collect::<Vec<_>>();
let output = output.map(move |mut vec| {
vec.reverse();
vec.into_iter().collect::<VecDeque<_>>()
});
Ok(output.flatten_stream().from_input_stream())
}

View File

@ -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");
}