mirror of
https://github.com/nushell/nushell.git
synced 2024-12-22 23:23:12 +01:00
Split split command to sub commands.
This commit is contained in:
parent
74c2daf665
commit
edbecda14d
@ -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),
|
||||
|
@ -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;
|
||||
|
@ -33,7 +33,7 @@ impl WholeStreamCommand for Headers {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
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,
|
||||
}]
|
||||
}
|
||||
|
@ -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<OutputS
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::SplitColumn;
|
||||
use super::SubCommand;
|
||||
|
||||
#[test]
|
||||
fn examples_work_as_expected() {
|
||||
use crate::examples::test as test_examples;
|
||||
|
||||
test_examples(SplitColumn {})
|
||||
test_examples(SubCommand {})
|
||||
}
|
||||
}
|
49
crates/nu-cli/src/commands/split/command.rs
Normal file
49
crates/nu-cli/src/commands/split/command.rs
Normal file
@ -0,0 +1,49 @@
|
||||
use crate::commands::WholeStreamCommand;
|
||||
use crate::prelude::*;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Command;
|
||||
|
||||
impl WholeStreamCommand for Command {
|
||||
fn name(&self) -> &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<OutputStream, ShellError> {
|
||||
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 {})
|
||||
}
|
||||
}
|
7
crates/nu-cli/src/commands/split/mod.rs
Normal file
7
crates/nu-cli/src/commands/split/mod.rs
Normal file
@ -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;
|
@ -10,15 +10,15 @@ struct SplitRowArgs {
|
||||
separator: Tagged<String>,
|
||||
}
|
||||
|
||||
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<OutputStre
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::SplitRow;
|
||||
use super::SubCommand;
|
||||
|
||||
#[test]
|
||||
fn examples_work_as_expected() {
|
||||
use crate::examples::test as test_examples;
|
||||
|
||||
test_examples(SplitRow {})
|
||||
test_examples(SubCommand {})
|
||||
}
|
||||
}
|
@ -35,7 +35,7 @@ fn condition_is_met() {
|
||||
open --raw caballeros.txt
|
||||
| lines
|
||||
| skip 2
|
||||
| split-column ','
|
||||
| split column ','
|
||||
| headers
|
||||
| skip-while "Chickens Collction" != "Blue Chickens"
|
||||
| keep-until "Chicken Collection" == "Red Chickens"
|
||||
|
@ -35,7 +35,7 @@ fn condition_is_met() {
|
||||
open --raw caballeros.txt
|
||||
| lines
|
||||
| skip 2
|
||||
| split-column ','
|
||||
| split column ','
|
||||
| headers
|
||||
| skip 1
|
||||
| keep-while "Chicken Collection" != "Blue Chickens"
|
||||
|
@ -10,7 +10,7 @@ fn lines() {
|
||||
| skip-while $it != "[dependencies]"
|
||||
| skip 1
|
||||
| first 1
|
||||
| split-column "="
|
||||
| split column "="
|
||||
| get Column1
|
||||
| trim
|
||||
| echo $it
|
||||
|
@ -41,6 +41,7 @@ mod skip_until;
|
||||
mod sort_by;
|
||||
mod split_by;
|
||||
mod split_column;
|
||||
mod split_row;
|
||||
mod sum;
|
||||
mod touch;
|
||||
mod trim;
|
||||
|
@ -35,7 +35,7 @@ fn condition_is_met() {
|
||||
open --raw caballeros.txt
|
||||
| lines
|
||||
| skip 2
|
||||
| split-column ','
|
||||
| split column ','
|
||||
| headers
|
||||
| skip-until "Chicken Collection" == "Red Chickens"
|
||||
| str "31/04/2020" --to-int
|
||||
|
@ -9,7 +9,7 @@ fn by_column() {
|
||||
| lines
|
||||
| skip 1
|
||||
| first 4
|
||||
| split-column "="
|
||||
| split column "="
|
||||
| sort-by Column1
|
||||
| skip 1
|
||||
| first 1
|
||||
|
@ -1,20 +1,30 @@
|
||||
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
|
||||
use nu_test_support::playground::Playground;
|
||||
use nu_test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn by_column() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open cargo_sample.toml --raw
|
||||
| lines
|
||||
| skip 1
|
||||
| first 1
|
||||
| split-column "="
|
||||
| get Column1
|
||||
| trim
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
fn to_column() {
|
||||
Playground::setup("split_column_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"sample.txt",
|
||||
r#"
|
||||
importer,shipper,tariff_item,name,origin
|
||||
"#,
|
||||
)]);
|
||||
|
||||
assert_eq!(actual.out, "name");
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open sample.txt
|
||||
| lines
|
||||
| trim
|
||||
| split column ","
|
||||
| pivot
|
||||
| nth 1
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert!(actual.out.contains("shipper"));
|
||||
})
|
||||
}
|
||||
|
29
crates/nu-cli/tests/commands/split_row.rs
Normal file
29
crates/nu-cli/tests/commands/split_row.rs
Normal file
@ -0,0 +1,29 @@
|
||||
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
|
||||
use nu_test_support::playground::Playground;
|
||||
use nu_test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn to_row() {
|
||||
Playground::setup("split_row_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"sample.txt",
|
||||
r#"
|
||||
importer,shipper,tariff_item,name,origin
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open sample.txt
|
||||
| lines
|
||||
| trim
|
||||
| split row ","
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert!(actual.out.contains("5"));
|
||||
})
|
||||
}
|
@ -30,7 +30,7 @@ fn table_to_csv_text() {
|
||||
open csv_text_sample.txt
|
||||
| lines
|
||||
| trim
|
||||
| split-column "," a b c d origin
|
||||
| split column "," a b c d origin
|
||||
| last 1
|
||||
| to csv
|
||||
| lines
|
||||
@ -63,7 +63,7 @@ fn table_to_csv_text_skipping_headers_after_conversion() {
|
||||
open csv_text_sample.txt
|
||||
| lines
|
||||
| trim
|
||||
| split-column "," a b c d origin
|
||||
| split column "," a b c d origin
|
||||
| last 1
|
||||
| to csv --headerless
|
||||
| echo $it
|
||||
|
@ -88,7 +88,7 @@ fn table_to_json_text() {
|
||||
r#"
|
||||
open sample.txt
|
||||
| lines
|
||||
| split-column "," name luck
|
||||
| split column "," name luck
|
||||
| select name
|
||||
| to json
|
||||
| from json
|
||||
|
@ -39,7 +39,7 @@ fn table_to_tsv_text() {
|
||||
r#"
|
||||
open tsv_text_sample.txt
|
||||
| lines
|
||||
| split-column "\t" a b c d origin
|
||||
| split column "\t" a b c d origin
|
||||
| last 1
|
||||
| to tsv
|
||||
| lines
|
||||
@ -69,7 +69,7 @@ fn table_to_tsv_text_skipping_headers_after_conversion() {
|
||||
r#"
|
||||
open tsv_text_sample.txt
|
||||
| lines
|
||||
| split-column "\t" a b c d origin
|
||||
| split column "\t" a b c d origin
|
||||
| last 1
|
||||
| to tsv --headerless
|
||||
| echo $it
|
||||
|
@ -312,7 +312,7 @@ pub fn insert_data_at_column_path(
|
||||
Ok(original)
|
||||
} else {
|
||||
Err(ShellError::untagged_runtime_error(
|
||||
"Internal error: could not split column-path correctly",
|
||||
"Internal error: could not split column path correctly",
|
||||
))
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ Use `rename` to give columns more appropriate names.
|
||||
## Examples
|
||||
|
||||
```shell
|
||||
> 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
|
||||
────┼────────┼──────────┼──────┼──────┼────────┼─────────────────┼──────────────────
|
||||
|
@ -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 <separator> ...args{flags}`
|
||||
Syntax: `split column <separator> ...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
|
||||
───┼─────────────────────┼──────────────────────┼────────────────────
|
@ -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 <separator>`
|
||||
Syntax: `split row <separator>`
|
||||
|
||||
### Parameters:
|
||||
* `<separator>` 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 ", "
|
||||
━━━┯━━━━━━━━━
|
||||
# │ <value>
|
||||
───┼─────────
|
Loading…
Reference in New Issue
Block a user