From 17e6c53b62b2623ac6e60d4d2db9aee474f8b0d7 Mon Sep 17 00:00:00 2001 From: "Joseph T. Lyons" Date: Tue, 14 Jul 2020 16:47:04 -0400 Subject: [PATCH] Add str reverse subcommand (#2170) * Add str reverse subcommand * rustfmt --- crates/nu-cli/src/cli.rs | 1 + crates/nu-cli/src/commands.rs | 4 +- crates/nu-cli/src/commands/str_/mod.rs | 2 + crates/nu-cli/src/commands/str_/reverse.rs | 58 ++++++++++++++++++++++ crates/nu-cli/tests/commands/str_.rs | 12 +++++ docs/commands/str.md | 5 ++ 6 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 crates/nu-cli/src/commands/str_/reverse.rs diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index 88a646f19..d5e35886f 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -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), diff --git a/crates/nu-cli/src/commands.rs b/crates/nu-cli/src/commands.rs index d09992cc0..73d69cdd2 100644 --- a/crates/nu-cli/src/commands.rs +++ b/crates/nu-cli/src/commands.rs @@ -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; diff --git a/crates/nu-cli/src/commands/str_/mod.rs b/crates/nu-cli/src/commands/str_/mod.rs index 794ff4977..759d2e38c 100644 --- a/crates/nu-cli/src/commands/str_/mod.rs +++ b/crates/nu-cli/src/commands/str_/mod.rs @@ -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; diff --git a/crates/nu-cli/src/commands/str_/reverse.rs b/crates/nu-cli/src/commands/str_/reverse.rs new file mode 100644 index 000000000..fa9a3dc8a --- /dev/null +++ b/crates/nu-cli/src/commands/str_/reverse.rs @@ -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 { + Ok(args + .input + .map(move |x| match x.as_string() { + Ok(s) => ReturnSuccess::value( + UntaggedValue::string(s.chars().rev().collect::()) + .into_untagged_value(), + ), + Err(err) => Err(err), + }) + .to_output_stream()) + } + + fn examples(&self) -> Vec { + 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 {}) + } +} diff --git a/crates/nu-cli/tests/commands/str_.rs b/crates/nu-cli/tests/commands/str_.rs index 62031a22c..eb3f25cdd 100644 --- a/crates/nu-cli/tests/commands/str_.rs +++ b/crates/nu-cli/tests/commands/str_.rs @@ -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")); +} diff --git a/docs/commands/str.md b/docs/commands/str.md index 0518ed38e..57f69cc97 100644 --- a/docs/commands/str.md +++ b/docs/commands/str.md @@ -69,6 +69,11 @@ Nu Nu ``` +```shell +> echo "Nushell" | str reverse +llehsuN +``` + ```shell > shells | str find-replace "TUX" "skipper" path ━━━┯━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━