add reverse

This commit is contained in:
Ramon Snir 2019-08-25 12:14:17 -04:00
parent dd74657385
commit 9735c3fcea
No known key found for this signature in database
GPG Key ID: F8E9B7EF846B06B4
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

@ -34,6 +34,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;
@ -89,6 +90,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");
}