sort_by: support -r flag for reverse (#3025)

* sort_by: support -r flag for reverse

* Update sort_by.rs

Fix reverse test

Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
This commit is contained in:
Saeed Rasooli 2021-02-08 01:40:06 +03:30 committed by GitHub
parent d883ab250a
commit 233161d56e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,6 +12,7 @@ pub struct SortBy;
pub struct SortByArgs { pub struct SortByArgs {
rest: Vec<Tagged<String>>, rest: Vec<Tagged<String>>,
insensitive: bool, insensitive: bool,
reverse: bool,
} }
#[async_trait] #[async_trait]
@ -24,9 +25,10 @@ impl WholeStreamCommand for SortBy {
Signature::build("sort-by") Signature::build("sort-by")
.switch( .switch(
"insensitive", "insensitive",
"Sort string-based columns case insensitively", "Sort string-based columns case-insensitively",
Some('i'), Some('i'),
) )
.switch("reverse", "Sort in reverse order", Some('r'))
.rest(SyntaxShape::String, "the column(s) to sort by") .rest(SyntaxShape::String, "the column(s) to sort by")
} }
@ -50,6 +52,16 @@ impl WholeStreamCommand for SortBy {
UntaggedValue::int(4).into(), UntaggedValue::int(4).into(),
]), ]),
}, },
Example {
description: "Sort list by decreasing value",
example: "echo [2 3 4 1] | sort-by -r",
result: Some(vec![
UntaggedValue::int(4).into(),
UntaggedValue::int(3).into(),
UntaggedValue::int(2).into(),
UntaggedValue::int(1).into(),
]),
},
Example { Example {
description: "Sort output by increasing file size", description: "Sort output by increasing file size",
example: "ls | sort-by size", example: "ls | sort-by size",
@ -61,7 +73,7 @@ impl WholeStreamCommand for SortBy {
result: None, result: None,
}, },
Example { Example {
description: "Sort strings (case sensitive)", description: "Sort strings (case-sensitive)",
example: "echo [airplane Truck Car] | sort-by", example: "echo [airplane Truck Car] | sort-by",
result: Some(vec![ result: Some(vec![
UntaggedValue::string("Car").into(), UntaggedValue::string("Car").into(),
@ -70,7 +82,16 @@ impl WholeStreamCommand for SortBy {
]), ]),
}, },
Example { Example {
description: "Sort strings (case insensitive)", description: "Sort strings (reversed case-sensitive)",
example: "echo [airplane Truck Car] | sort-by -r",
result: Some(vec![
UntaggedValue::string("airplane").into(),
UntaggedValue::string("Truck").into(),
UntaggedValue::string("Car").into(),
]),
},
Example {
description: "Sort strings (case-insensitive)",
example: "echo [airplane Truck Car] | sort-by -i", example: "echo [airplane Truck Car] | sort-by -i",
result: Some(vec![ result: Some(vec![
UntaggedValue::string("airplane").into(), UntaggedValue::string("airplane").into(),
@ -78,6 +99,15 @@ impl WholeStreamCommand for SortBy {
UntaggedValue::string("Truck").into(), UntaggedValue::string("Truck").into(),
]), ]),
}, },
Example {
description: "Sort strings (reversed case-insensitive)",
example: "echo [airplane Truck Car] | sort-by -i -r",
result: Some(vec![
UntaggedValue::string("Truck").into(),
UntaggedValue::string("Car").into(),
UntaggedValue::string("airplane").into(),
]),
},
] ]
} }
} }
@ -85,11 +115,22 @@ impl WholeStreamCommand for SortBy {
async fn sort_by(args: CommandArgs) -> Result<OutputStream, ShellError> { async fn sort_by(args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone(); let tag = args.call_info.name_tag.clone();
let (SortByArgs { rest, insensitive }, mut input) = args.process().await?; let (
SortByArgs {
rest,
insensitive,
reverse,
},
mut input,
) = args.process().await?;
let mut vec = input.drain_vec().await; let mut vec = input.drain_vec().await;
sort(&mut vec, &rest, &tag, insensitive)?; sort(&mut vec, &rest, &tag, insensitive)?;
if reverse {
vec.reverse()
}
Ok(futures::stream::iter(vec.into_iter()).to_output_stream()) Ok(futures::stream::iter(vec.into_iter()).to_output_stream())
} }