diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index 18c3eb8c9..fd6f0f3e6 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -305,6 +305,7 @@ pub fn create_default_context( whole_stream_command(StrToDatetime), whole_stream_command(StrTrim), whole_stream_command(StrCollect), + whole_stream_command(StrLength), 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 43f4feddc..55d21dc4d 100644 --- a/crates/nu-cli/src/commands.rs +++ b/crates/nu-cli/src/commands.rs @@ -244,7 +244,7 @@ pub(crate) use split::SplitColumn; pub(crate) use split::SplitRow; pub(crate) use split_by::SplitBy; pub(crate) use str_::{ - Str, StrCapitalize, StrCollect, StrDowncase, StrFindReplace, StrSet, StrSubstring, + Str, StrCapitalize, StrCollect, StrDowncase, StrFindReplace, StrLength, StrSet, StrSubstring, StrToDatetime, StrToDecimal, StrToInteger, StrTrim, StrUpcase, }; #[allow(unused_imports)] diff --git a/crates/nu-cli/src/commands/str_/length.rs b/crates/nu-cli/src/commands/str_/length.rs new file mode 100644 index 000000000..1cd22434a --- /dev/null +++ b/crates/nu-cli/src/commands/str_/length.rs @@ -0,0 +1,65 @@ +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 length" + } + + fn signature(&self) -> Signature { + Signature::build("str length") + } + + fn usage(&self) -> &str { + "outputs the lengths 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::int(s.len()).into_untagged_value()), + Err(err) => Err(err), + }) + .to_output_stream()) + } + + fn examples(&self) -> Vec { + vec![ + Example { + description: "Return the lengths of multiple strings", + example: "echo 'hello' | str length", + result: Some(vec![UntaggedValue::int(5).into_untagged_value()]), + }, + Example { + description: "Return the lengths of multiple strings", + example: "echo 'hi' 'there' | str length", + result: Some(vec![ + UntaggedValue::int(2).into_untagged_value(), + UntaggedValue::int(5).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/src/commands/str_/mod.rs b/crates/nu-cli/src/commands/str_/mod.rs index 3c1212e62..20ac62184 100644 --- a/crates/nu-cli/src/commands/str_/mod.rs +++ b/crates/nu-cli/src/commands/str_/mod.rs @@ -3,6 +3,7 @@ mod collect; mod command; mod downcase; mod find_replace; +mod length; mod set; mod substring; mod to_datetime; @@ -16,6 +17,7 @@ pub use collect::SubCommand as StrCollect; pub use command::Command as Str; pub use downcase::SubCommand as StrDowncase; pub use find_replace::SubCommand as StrFindReplace; +pub use length::SubCommand as StrLength; pub use set::SubCommand as StrSet; pub use substring::SubCommand as StrSubstring; pub use to_datetime::SubCommand as StrToDatetime;