diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index 9b1e14024e..d608703152 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -279,6 +279,7 @@ pub fn create_default_context( whole_stream_command(Autoview), whole_stream_command(Table), // Text manipulation + whole_stream_command(Split), whole_stream_command(SplitColumn), whole_stream_command(SplitRow), whole_stream_command(Lines), diff --git a/crates/nu-cli/src/commands.rs b/crates/nu-cli/src/commands.rs index cc67db0854..4dd14359e8 100644 --- a/crates/nu-cli/src/commands.rs +++ b/crates/nu-cli/src/commands.rs @@ -96,9 +96,8 @@ pub(crate) mod skip; pub(crate) mod skip_until; pub(crate) mod skip_while; pub(crate) mod sort_by; +pub(crate) mod split; pub(crate) mod split_by; -pub(crate) mod split_column; -pub(crate) mod split_row; pub(crate) mod sum; #[allow(unused)] pub(crate) mod t_sort_by; @@ -222,9 +221,10 @@ pub(crate) use skip::Skip; pub(crate) use skip_until::SkipUntil; pub(crate) use skip_while::SkipWhile; pub(crate) use sort_by::SortBy; +pub(crate) use split::Split; +pub(crate) use split::SplitColumn; +pub(crate) use split::SplitRow; pub(crate) use split_by::SplitBy; -pub(crate) use split_column::SplitColumn; -pub(crate) use split_row::SplitRow; pub(crate) use sum::Sum; #[allow(unused_imports)] pub(crate) use t_sort_by::TSortBy; diff --git a/crates/nu-cli/src/commands/headers.rs b/crates/nu-cli/src/commands/headers.rs index c9c50440c2..bc415b0d22 100644 --- a/crates/nu-cli/src/commands/headers.rs +++ b/crates/nu-cli/src/commands/headers.rs @@ -33,7 +33,7 @@ impl WholeStreamCommand for Headers { fn examples(&self) -> Vec { vec![Example { description: "Create headers for a raw string", - example: r#"echo "a b c|1 2 3" | split-row "|" | split-column " " | headers"#, + example: r#"echo "a b c|1 2 3" | split row "|" | split column " " | headers"#, result: None, }] } diff --git a/crates/nu-cli/src/commands/split_column.rs b/crates/nu-cli/src/commands/split/column.rs similarity index 93% rename from crates/nu-cli/src/commands/split_column.rs rename to crates/nu-cli/src/commands/split/column.rs index 739b1a06fc..b57b10896c 100644 --- a/crates/nu-cli/src/commands/split_column.rs +++ b/crates/nu-cli/src/commands/split/column.rs @@ -15,15 +15,15 @@ struct SplitColumnArgs { collapse_empty: bool, } -pub struct SplitColumn; +pub struct SubCommand; -impl WholeStreamCommand for SplitColumn { +impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { - "split-column" + "split column" } fn signature(&self) -> Signature { - Signature::build("split-column") + Signature::build("split column") .required( "separator", SyntaxShape::Any, @@ -34,7 +34,7 @@ impl WholeStreamCommand for SplitColumn { } fn usage(&self) -> &str { - "Split row contents across multiple columns via the separator." + "splits contents across multiple columns via the separator." } fn run( @@ -106,12 +106,12 @@ fn split_column(args: CommandArgs, registry: &CommandRegistry) -> Result &str { + "split" + } + + fn signature(&self) -> Signature { + Signature::build("split") + } + + fn usage(&self) -> &str { + "split contents across desired subcommand (like row, column) via the separator." + } + + fn run( + &self, + _args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + let registry = registry.clone(); + let stream = async_stream! { + yield Ok(ReturnSuccess::Value( + UntaggedValue::string(crate::commands::help::get_help(&Command, ®istry)) + .into_value(Tag::unknown()), + )); + }; + + Ok(stream.to_output_stream()) + } +} + +#[cfg(test)] +mod tests { + use super::Command; + + #[test] + fn examples_work_as_expected() { + use crate::examples::test as test_examples; + + test_examples(Command {}) + } +} diff --git a/crates/nu-cli/src/commands/split/mod.rs b/crates/nu-cli/src/commands/split/mod.rs new file mode 100644 index 0000000000..4c5e83026c --- /dev/null +++ b/crates/nu-cli/src/commands/split/mod.rs @@ -0,0 +1,7 @@ +pub mod column; +pub mod command; +pub mod row; + +pub use column::SubCommand as SplitColumn; +pub use command::Command as Split; +pub use row::SubCommand as SplitRow; diff --git a/crates/nu-cli/src/commands/split_row.rs b/crates/nu-cli/src/commands/split/row.rs similarity index 89% rename from crates/nu-cli/src/commands/split_row.rs rename to crates/nu-cli/src/commands/split/row.rs index d2a65e37d2..a11f52a309 100644 --- a/crates/nu-cli/src/commands/split_row.rs +++ b/crates/nu-cli/src/commands/split/row.rs @@ -10,15 +10,15 @@ struct SplitRowArgs { separator: Tagged, } -pub struct SplitRow; +pub struct SubCommand; -impl WholeStreamCommand for SplitRow { +impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { - "split-row" + "split row" } fn signature(&self) -> Signature { - Signature::build("split-row").required( + Signature::build("split row").required( "separator", SyntaxShape::Any, "the character that denotes what separates rows", @@ -26,7 +26,7 @@ impl WholeStreamCommand for SplitRow { } fn usage(&self) -> &str { - "Split row contents over multiple rows via the separator." + "splits contents over multiple rows via the separator." } fn run( @@ -73,12 +73,12 @@ fn split_row(args: CommandArgs, registry: &CommandRegistry) -> Result open /etc/passwd | lines | split-column ":" | rename user password uid gid gecos home shell +> open /etc/passwd | lines | split column ":" | rename user password uid gid gecos home shell ────┬────────┬──────────┬──────┬──────┬────────┬─────────────────┬────────────────── # │ user │ password │ uid │ gid │ gecos │ home │ shell ────┼────────┼──────────┼──────┼──────┼────────┼─────────────────┼────────────────── diff --git a/docs/commands/split-column.md b/docs/commands/split-to-column.md similarity index 92% rename from docs/commands/split-column.md rename to docs/commands/split-to-column.md index 80f1de5dae..004ea8de43 100644 --- a/docs/commands/split-column.md +++ b/docs/commands/split-to-column.md @@ -1,8 +1,8 @@ -# split-column +# split column -Split row contents across multiple columns via the separator. +splits contents across multiple columns via the separator. -Syntax: `split-column ...args{flags}` +Syntax: `split column ...args{flags}` ### Parameters @@ -31,10 +31,10 @@ If we have file structured like this: 1.0459770114942528 | 1.0925925925925926 | 0.6164383561643836 ``` -We can build a table from it using the `split-column` command +We can build a table from it using the `split column` command ```shell -> open coordinates.txt | lines | split-column " | " +> open coordinates.txt | lines | split column " | " ━━━┯━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━ # │ Column1 │ Column2 │ Column3 ───┼─────────────────────┼──────────────────────┼──────────────────── @@ -54,7 +54,7 @@ We can build a table from it using the `split-column` command And give names to the columns ```shell -> open coordinates.txt | lines | split-column " | " x y z +> open coordinates.txt | lines | split column " | " x y z ━━━┯━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━ # │ x │ y │ z ───┼─────────────────────┼──────────────────────┼──────────────────── diff --git a/docs/commands/split-row.md b/docs/commands/split-to-row.md similarity index 71% rename from docs/commands/split-row.md rename to docs/commands/split-to-row.md index c0d58f79e5..937563c901 100644 --- a/docs/commands/split-row.md +++ b/docs/commands/split-to-row.md @@ -1,8 +1,8 @@ -# split-row +# split row -Split row contents over multiple rows via the separator. +splits contents over multiple rows via the separator. -Syntax: `split-row ` +Syntax: `split row ` ### Parameters: * `` the character that denotes what separates rows @@ -17,10 +17,10 @@ We can build a table from a file that looks like this ``` -using the `split-row` command. +using the `split row` command. ```shell -open table.txt | split-row ", " +open table.txt | split row ", " ━━━┯━━━━━━━━━ # │ ───┼─────────