Add str reverse subcommand (#2170)

* Add str reverse subcommand

* rustfmt
This commit is contained in:
Joseph T. Lyons 2020-07-14 16:47:04 -04:00 committed by GitHub
parent 80d2a7ee7a
commit 17e6c53b62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 2 deletions

View File

@ -314,6 +314,7 @@ pub fn create_default_context(
whole_stream_command(StrTrim),
whole_stream_command(StrCollect),
whole_stream_command(StrLength),
whole_stream_command(StrReverse),
whole_stream_command(BuildString),
whole_stream_command(Ansi),
whole_stream_command(Char),

View File

@ -252,8 +252,8 @@ pub(crate) use sort_by::SortBy;
pub(crate) use split::{Split, SplitChars, SplitColumn, SplitRow};
pub(crate) use split_by::SplitBy;
pub(crate) use str_::{
Str, StrCapitalize, StrCollect, StrDowncase, StrFindReplace, StrFrom, StrLength, StrSet,
StrSubstring, StrToDatetime, StrToDecimal, StrToInteger, StrTrim, StrUpcase,
Str, StrCapitalize, StrCollect, StrDowncase, StrFindReplace, StrFrom, StrLength, StrReverse,
StrSet, StrSubstring, StrToDatetime, StrToDecimal, StrToInteger, StrTrim, StrUpcase,
};
#[allow(unused_imports)]
pub(crate) use t_sort_by::TSortBy;

View File

@ -5,6 +5,7 @@ mod downcase;
mod find_replace;
mod from;
mod length;
mod reverse;
mod set;
mod substring;
mod to_datetime;
@ -20,6 +21,7 @@ pub use downcase::SubCommand as StrDowncase;
pub use find_replace::SubCommand as StrFindReplace;
pub use from::SubCommand as StrFrom;
pub use length::SubCommand as StrLength;
pub use reverse::SubCommand as StrReverse;
pub use set::SubCommand as StrSet;
pub use substring::SubCommand as StrSubstring;
pub use to_datetime::SubCommand as StrToDatetime;

View File

@ -0,0 +1,58 @@
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
pub struct SubCommand;
#[async_trait]
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"str reverse"
}
fn signature(&self) -> Signature {
Signature::build("str reverse")
}
fn usage(&self) -> &str {
"outputs the reversals of the strings in the pipeline"
}
async fn run(
&self,
args: CommandArgs,
_registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
Ok(args
.input
.map(move |x| match x.as_string() {
Ok(s) => ReturnSuccess::value(
UntaggedValue::string(s.chars().rev().collect::<String>())
.into_untagged_value(),
),
Err(err) => Err(err),
})
.to_output_stream())
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Return the reversals of multiple strings",
example: "echo 'Nushell' | str reverse",
result: Some(vec![UntaggedValue::string("llehsuN").into_untagged_value()]),
}]
}
}
#[cfg(test)]
mod tests {
use super::SubCommand;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(SubCommand {})
}
}

View File

@ -404,3 +404,15 @@ fn from_table() {
assert!(actual.out.contains("32.38"));
assert!(actual.out.contains("15.20"));
}
#[test]
fn str_reverse() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo "nushell" | str reverse
"#
));
assert!(actual.out.contains("llehsun"));
}

View File

@ -69,6 +69,11 @@ Nu
Nu
```
```shell
> echo "Nushell" | str reverse
llehsuN
```
```shell
> shells | str find-replace "TUX" "skipper" path
━━━┯━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━