forked from extern/nushell
Add str reverse subcommand (#2170)
* Add str reverse subcommand * rustfmt
This commit is contained in:
parent
80d2a7ee7a
commit
17e6c53b62
@ -314,6 +314,7 @@ pub fn create_default_context(
|
|||||||
whole_stream_command(StrTrim),
|
whole_stream_command(StrTrim),
|
||||||
whole_stream_command(StrCollect),
|
whole_stream_command(StrCollect),
|
||||||
whole_stream_command(StrLength),
|
whole_stream_command(StrLength),
|
||||||
|
whole_stream_command(StrReverse),
|
||||||
whole_stream_command(BuildString),
|
whole_stream_command(BuildString),
|
||||||
whole_stream_command(Ansi),
|
whole_stream_command(Ansi),
|
||||||
whole_stream_command(Char),
|
whole_stream_command(Char),
|
||||||
|
@ -252,8 +252,8 @@ pub(crate) use sort_by::SortBy;
|
|||||||
pub(crate) use split::{Split, SplitChars, SplitColumn, SplitRow};
|
pub(crate) use split::{Split, SplitChars, SplitColumn, SplitRow};
|
||||||
pub(crate) use split_by::SplitBy;
|
pub(crate) use split_by::SplitBy;
|
||||||
pub(crate) use str_::{
|
pub(crate) use str_::{
|
||||||
Str, StrCapitalize, StrCollect, StrDowncase, StrFindReplace, StrFrom, StrLength, StrSet,
|
Str, StrCapitalize, StrCollect, StrDowncase, StrFindReplace, StrFrom, StrLength, StrReverse,
|
||||||
StrSubstring, StrToDatetime, StrToDecimal, StrToInteger, StrTrim, StrUpcase,
|
StrSet, StrSubstring, StrToDatetime, StrToDecimal, StrToInteger, StrTrim, StrUpcase,
|
||||||
};
|
};
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
pub(crate) use t_sort_by::TSortBy;
|
pub(crate) use t_sort_by::TSortBy;
|
||||||
|
@ -5,6 +5,7 @@ mod downcase;
|
|||||||
mod find_replace;
|
mod find_replace;
|
||||||
mod from;
|
mod from;
|
||||||
mod length;
|
mod length;
|
||||||
|
mod reverse;
|
||||||
mod set;
|
mod set;
|
||||||
mod substring;
|
mod substring;
|
||||||
mod to_datetime;
|
mod to_datetime;
|
||||||
@ -20,6 +21,7 @@ pub use downcase::SubCommand as StrDowncase;
|
|||||||
pub use find_replace::SubCommand as StrFindReplace;
|
pub use find_replace::SubCommand as StrFindReplace;
|
||||||
pub use from::SubCommand as StrFrom;
|
pub use from::SubCommand as StrFrom;
|
||||||
pub use length::SubCommand as StrLength;
|
pub use length::SubCommand as StrLength;
|
||||||
|
pub use reverse::SubCommand as StrReverse;
|
||||||
pub use set::SubCommand as StrSet;
|
pub use set::SubCommand as StrSet;
|
||||||
pub use substring::SubCommand as StrSubstring;
|
pub use substring::SubCommand as StrSubstring;
|
||||||
pub use to_datetime::SubCommand as StrToDatetime;
|
pub use to_datetime::SubCommand as StrToDatetime;
|
||||||
|
58
crates/nu-cli/src/commands/str_/reverse.rs
Normal file
58
crates/nu-cli/src/commands/str_/reverse.rs
Normal 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 {})
|
||||||
|
}
|
||||||
|
}
|
@ -404,3 +404,15 @@ fn from_table() {
|
|||||||
assert!(actual.out.contains("32.38"));
|
assert!(actual.out.contains("32.38"));
|
||||||
assert!(actual.out.contains("15.20"));
|
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"));
|
||||||
|
}
|
||||||
|
@ -69,6 +69,11 @@ Nu
|
|||||||
Nu
|
Nu
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```shell
|
||||||
|
> echo "Nushell" | str reverse
|
||||||
|
llehsuN
|
||||||
|
```
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
> shells | str find-replace "TUX" "skipper" path
|
> shells | str find-replace "TUX" "skipper" path
|
||||||
━━━┯━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
━━━┯━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||||
|
Loading…
Reference in New Issue
Block a user