nushell/crates/nu-command/src/strings/split/row.rs

183 lines
5.5 KiB
Rust
Raw Normal View History

2021-10-09 04:45:25 +02:00
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
2021-10-25 18:58:58 +02:00
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type,
Value,
2021-10-09 04:45:25 +02:00
};
Add regex separators for split row/list/column (#8707) # Description Verified on discord with maintainer Change adds regex separators in split rows/column/list. The primary motivating reason was to make it easier to split on separators with unbounded whitespace without requiring a lot of trim jiggery. But, secondary motivation is the same as the set of all motivations for adding split regex features to most languages. # User-Facing Changes Adds -r option to split rows/column/list. # Tests + Formatting Ran tests, however tests.nu fails with unrelated errors: ``` ~/src/nushell> cargo run -- crates/nu-utils/standard_library/tests.nu 04/02/2023 02:07:25 AM Finished dev [unoptimized + debuginfo] target(s) in 0.24s Running `target/debug/nu crates/nu-utils/standard_library/tests.nu` INF|2023-04-02T02:07:27.060|Running tests in test_asserts INF|2023-04-02T02:07:27.141|Running tests in test_dirs Error: × list is just pwd after initialization INF|2023-04-02T02:07:27.167|Running tests in test_logger INF|2023-04-02T02:07:27.286|Running tests in test_std Error: × some tests did not pass (see complete errors above): │ │ test_asserts test_assert │ test_asserts test_assert_equal │ test_asserts test_assert_error │ test_asserts test_assert_greater │ test_asserts test_assert_greater_or_equal │ test_asserts test_assert_length │ test_asserts test_assert_less │ test_asserts test_assert_less_or_equal │ test_asserts test_assert_not_equal │ ⨯ test_dirs test_dirs_command │ test_logger test_critical │ test_logger test_debug │ test_logger test_error │ test_logger test_info │ test_logger test_warning │ test_std test_path_add │ ``` Upon investigating seeing this difference: ``` ╭───┬─────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ 0 │ /var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ │ 1 │ /private/var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ ╰───┴─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ``` This seems unrelated to my changes, but can investigate further if desired. # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. Co-authored-by: Robert Waugh <robert@waugh.io>
2023-04-07 13:46:11 +02:00
use regex::Regex;
2021-10-25 06:01:02 +02:00
#[derive(Clone)]
2021-10-09 04:45:25 +02:00
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"split row"
}
fn signature(&self) -> Signature {
Signature::build("split row")
.input_output_types(vec![(Type::String, Type::List(Box::new(Type::String)))])
.vectorizes_over_list(true)
.required(
"separator",
SyntaxShape::String,
Add regex separators for split row/list/column (#8707) # Description Verified on discord with maintainer Change adds regex separators in split rows/column/list. The primary motivating reason was to make it easier to split on separators with unbounded whitespace without requiring a lot of trim jiggery. But, secondary motivation is the same as the set of all motivations for adding split regex features to most languages. # User-Facing Changes Adds -r option to split rows/column/list. # Tests + Formatting Ran tests, however tests.nu fails with unrelated errors: ``` ~/src/nushell> cargo run -- crates/nu-utils/standard_library/tests.nu 04/02/2023 02:07:25 AM Finished dev [unoptimized + debuginfo] target(s) in 0.24s Running `target/debug/nu crates/nu-utils/standard_library/tests.nu` INF|2023-04-02T02:07:27.060|Running tests in test_asserts INF|2023-04-02T02:07:27.141|Running tests in test_dirs Error: × list is just pwd after initialization INF|2023-04-02T02:07:27.167|Running tests in test_logger INF|2023-04-02T02:07:27.286|Running tests in test_std Error: × some tests did not pass (see complete errors above): │ │ test_asserts test_assert │ test_asserts test_assert_equal │ test_asserts test_assert_error │ test_asserts test_assert_greater │ test_asserts test_assert_greater_or_equal │ test_asserts test_assert_length │ test_asserts test_assert_less │ test_asserts test_assert_less_or_equal │ test_asserts test_assert_not_equal │ ⨯ test_dirs test_dirs_command │ test_logger test_critical │ test_logger test_debug │ test_logger test_error │ test_logger test_info │ test_logger test_warning │ test_std test_path_add │ ``` Upon investigating seeing this difference: ``` ╭───┬─────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ 0 │ /var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ │ 1 │ /private/var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ ╰───┴─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ``` This seems unrelated to my changes, but can investigate further if desired. # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. Co-authored-by: Robert Waugh <robert@waugh.io>
2023-04-07 13:46:11 +02:00
"a character or regex that denotes what separates rows",
)
.named(
"number",
SyntaxShape::Int,
"Split into maximum number of items",
Some('n'),
)
Add regex separators for split row/list/column (#8707) # Description Verified on discord with maintainer Change adds regex separators in split rows/column/list. The primary motivating reason was to make it easier to split on separators with unbounded whitespace without requiring a lot of trim jiggery. But, secondary motivation is the same as the set of all motivations for adding split regex features to most languages. # User-Facing Changes Adds -r option to split rows/column/list. # Tests + Formatting Ran tests, however tests.nu fails with unrelated errors: ``` ~/src/nushell> cargo run -- crates/nu-utils/standard_library/tests.nu 04/02/2023 02:07:25 AM Finished dev [unoptimized + debuginfo] target(s) in 0.24s Running `target/debug/nu crates/nu-utils/standard_library/tests.nu` INF|2023-04-02T02:07:27.060|Running tests in test_asserts INF|2023-04-02T02:07:27.141|Running tests in test_dirs Error: × list is just pwd after initialization INF|2023-04-02T02:07:27.167|Running tests in test_logger INF|2023-04-02T02:07:27.286|Running tests in test_std Error: × some tests did not pass (see complete errors above): │ │ test_asserts test_assert │ test_asserts test_assert_equal │ test_asserts test_assert_error │ test_asserts test_assert_greater │ test_asserts test_assert_greater_or_equal │ test_asserts test_assert_length │ test_asserts test_assert_less │ test_asserts test_assert_less_or_equal │ test_asserts test_assert_not_equal │ ⨯ test_dirs test_dirs_command │ test_logger test_critical │ test_logger test_debug │ test_logger test_error │ test_logger test_info │ test_logger test_warning │ test_std test_path_add │ ``` Upon investigating seeing this difference: ``` ╭───┬─────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ 0 │ /var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ │ 1 │ /private/var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ ╰───┴─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ``` This seems unrelated to my changes, but can investigate further if desired. # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. Co-authored-by: Robert Waugh <robert@waugh.io>
2023-04-07 13:46:11 +02:00
.switch("regex", "use regex syntax for separator", Some('r'))
.category(Category::Strings)
2021-10-09 04:45:25 +02:00
}
fn usage(&self) -> &str {
"Split a string into multiple rows using a separator."
2021-10-09 04:45:25 +02:00
}
fn search_terms(&self) -> Vec<&str> {
vec!["separate", "divide"]
}
2021-10-09 04:45:25 +02:00
fn run(
&self,
2021-10-25 08:31:39 +02:00
engine_state: &EngineState,
stack: &mut Stack,
2021-10-09 04:45:25 +02:00
call: &Call,
2021-10-25 06:01:02 +02:00
input: PipelineData,
) -> Result<PipelineData, ShellError> {
2021-10-25 08:31:39 +02:00
split_row(engine_state, stack, call, input)
2021-10-09 04:45:25 +02:00
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Split a string into rows of char",
example: "'abc' | split row ''",
result: Some(Value::List {
vals: vec![
Value::test_string(""),
Value::test_string("a"),
Value::test_string("b"),
Value::test_string("c"),
Value::test_string(""),
],
span: Span::test_data(),
}),
},
Example {
description: "Split a string into rows by the specified separator",
example: "'a--b--c' | split row '--'",
result: Some(Value::List {
vals: vec![
Value::test_string("a"),
Value::test_string("b"),
Value::test_string("c"),
],
span: Span::test_data(),
}),
},
Example {
description: "Split a string by '-'",
example: "'-a-b-c-' | split row '-'",
result: Some(Value::List {
vals: vec![
Value::test_string(""),
Value::test_string("a"),
Value::test_string("b"),
Value::test_string("c"),
Value::test_string(""),
],
span: Span::test_data(),
}),
},
Add regex separators for split row/list/column (#8707) # Description Verified on discord with maintainer Change adds regex separators in split rows/column/list. The primary motivating reason was to make it easier to split on separators with unbounded whitespace without requiring a lot of trim jiggery. But, secondary motivation is the same as the set of all motivations for adding split regex features to most languages. # User-Facing Changes Adds -r option to split rows/column/list. # Tests + Formatting Ran tests, however tests.nu fails with unrelated errors: ``` ~/src/nushell> cargo run -- crates/nu-utils/standard_library/tests.nu 04/02/2023 02:07:25 AM Finished dev [unoptimized + debuginfo] target(s) in 0.24s Running `target/debug/nu crates/nu-utils/standard_library/tests.nu` INF|2023-04-02T02:07:27.060|Running tests in test_asserts INF|2023-04-02T02:07:27.141|Running tests in test_dirs Error: × list is just pwd after initialization INF|2023-04-02T02:07:27.167|Running tests in test_logger INF|2023-04-02T02:07:27.286|Running tests in test_std Error: × some tests did not pass (see complete errors above): │ │ test_asserts test_assert │ test_asserts test_assert_equal │ test_asserts test_assert_error │ test_asserts test_assert_greater │ test_asserts test_assert_greater_or_equal │ test_asserts test_assert_length │ test_asserts test_assert_less │ test_asserts test_assert_less_or_equal │ test_asserts test_assert_not_equal │ ⨯ test_dirs test_dirs_command │ test_logger test_critical │ test_logger test_debug │ test_logger test_error │ test_logger test_info │ test_logger test_warning │ test_std test_path_add │ ``` Upon investigating seeing this difference: ``` ╭───┬─────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ 0 │ /var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ │ 1 │ /private/var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ ╰───┴─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ``` This seems unrelated to my changes, but can investigate further if desired. # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. Co-authored-by: Robert Waugh <robert@waugh.io>
2023-04-07 13:46:11 +02:00
Example {
description: "Split a string by regex",
example: r"'a b c' | split row -r '\s+'",
result: Some(Value::List {
vals: vec![
Value::test_string("a"),
Value::test_string("b"),
Value::test_string("c"),
],
span: Span::test_data(),
}),
},
]
}
2021-10-09 04:45:25 +02:00
}
fn split_row(
2021-10-25 08:31:39 +02:00
engine_state: &EngineState,
stack: &mut Stack,
2021-10-09 04:45:25 +02:00
call: &Call,
2021-10-25 06:01:02 +02:00
input: PipelineData,
) -> Result<PipelineData, ShellError> {
2021-10-09 04:45:25 +02:00
let name_span = call.head;
2021-10-25 08:31:39 +02:00
let separator: Spanned<String> = call.req(engine_state, stack, 0)?;
Add regex separators for split row/list/column (#8707) # Description Verified on discord with maintainer Change adds regex separators in split rows/column/list. The primary motivating reason was to make it easier to split on separators with unbounded whitespace without requiring a lot of trim jiggery. But, secondary motivation is the same as the set of all motivations for adding split regex features to most languages. # User-Facing Changes Adds -r option to split rows/column/list. # Tests + Formatting Ran tests, however tests.nu fails with unrelated errors: ``` ~/src/nushell> cargo run -- crates/nu-utils/standard_library/tests.nu 04/02/2023 02:07:25 AM Finished dev [unoptimized + debuginfo] target(s) in 0.24s Running `target/debug/nu crates/nu-utils/standard_library/tests.nu` INF|2023-04-02T02:07:27.060|Running tests in test_asserts INF|2023-04-02T02:07:27.141|Running tests in test_dirs Error: × list is just pwd after initialization INF|2023-04-02T02:07:27.167|Running tests in test_logger INF|2023-04-02T02:07:27.286|Running tests in test_std Error: × some tests did not pass (see complete errors above): │ │ test_asserts test_assert │ test_asserts test_assert_equal │ test_asserts test_assert_error │ test_asserts test_assert_greater │ test_asserts test_assert_greater_or_equal │ test_asserts test_assert_length │ test_asserts test_assert_less │ test_asserts test_assert_less_or_equal │ test_asserts test_assert_not_equal │ ⨯ test_dirs test_dirs_command │ test_logger test_critical │ test_logger test_debug │ test_logger test_error │ test_logger test_info │ test_logger test_warning │ test_std test_path_add │ ``` Upon investigating seeing this difference: ``` ╭───┬─────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ 0 │ /var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ │ 1 │ /private/var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ ╰───┴─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ``` This seems unrelated to my changes, but can investigate further if desired. # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. Co-authored-by: Robert Waugh <robert@waugh.io>
2023-04-07 13:46:11 +02:00
let regex = if call.has_flag("regex") {
Regex::new(&separator.item)
} else {
let escaped = regex::escape(&separator.item);
Regex::new(&escaped)
}
.map_err(|err| {
ShellError::GenericError(
"Error with regular expression".into(),
err.to_string(),
Some(separator.span),
None,
Vec::new(),
)
})?;
let max_split: Option<usize> = call.get_flag(engine_state, stack, "number")?;
2021-10-28 06:13:10 +02:00
input.flat_map(
Add regex separators for split row/list/column (#8707) # Description Verified on discord with maintainer Change adds regex separators in split rows/column/list. The primary motivating reason was to make it easier to split on separators with unbounded whitespace without requiring a lot of trim jiggery. But, secondary motivation is the same as the set of all motivations for adding split regex features to most languages. # User-Facing Changes Adds -r option to split rows/column/list. # Tests + Formatting Ran tests, however tests.nu fails with unrelated errors: ``` ~/src/nushell> cargo run -- crates/nu-utils/standard_library/tests.nu 04/02/2023 02:07:25 AM Finished dev [unoptimized + debuginfo] target(s) in 0.24s Running `target/debug/nu crates/nu-utils/standard_library/tests.nu` INF|2023-04-02T02:07:27.060|Running tests in test_asserts INF|2023-04-02T02:07:27.141|Running tests in test_dirs Error: × list is just pwd after initialization INF|2023-04-02T02:07:27.167|Running tests in test_logger INF|2023-04-02T02:07:27.286|Running tests in test_std Error: × some tests did not pass (see complete errors above): │ │ test_asserts test_assert │ test_asserts test_assert_equal │ test_asserts test_assert_error │ test_asserts test_assert_greater │ test_asserts test_assert_greater_or_equal │ test_asserts test_assert_length │ test_asserts test_assert_less │ test_asserts test_assert_less_or_equal │ test_asserts test_assert_not_equal │ ⨯ test_dirs test_dirs_command │ test_logger test_critical │ test_logger test_debug │ test_logger test_error │ test_logger test_info │ test_logger test_warning │ test_std test_path_add │ ``` Upon investigating seeing this difference: ``` ╭───┬─────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ 0 │ /var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ │ 1 │ /private/var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ ╰───┴─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ``` This seems unrelated to my changes, but can investigate further if desired. # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. Co-authored-by: Robert Waugh <robert@waugh.io>
2023-04-07 13:46:11 +02:00
move |x| split_row_helper(&x, &regex, max_split, name_span),
2021-10-28 06:13:10 +02:00
engine_state.ctrlc.clone(),
)
2021-10-09 04:45:25 +02:00
}
Add regex separators for split row/list/column (#8707) # Description Verified on discord with maintainer Change adds regex separators in split rows/column/list. The primary motivating reason was to make it easier to split on separators with unbounded whitespace without requiring a lot of trim jiggery. But, secondary motivation is the same as the set of all motivations for adding split regex features to most languages. # User-Facing Changes Adds -r option to split rows/column/list. # Tests + Formatting Ran tests, however tests.nu fails with unrelated errors: ``` ~/src/nushell> cargo run -- crates/nu-utils/standard_library/tests.nu 04/02/2023 02:07:25 AM Finished dev [unoptimized + debuginfo] target(s) in 0.24s Running `target/debug/nu crates/nu-utils/standard_library/tests.nu` INF|2023-04-02T02:07:27.060|Running tests in test_asserts INF|2023-04-02T02:07:27.141|Running tests in test_dirs Error: × list is just pwd after initialization INF|2023-04-02T02:07:27.167|Running tests in test_logger INF|2023-04-02T02:07:27.286|Running tests in test_std Error: × some tests did not pass (see complete errors above): │ │ test_asserts test_assert │ test_asserts test_assert_equal │ test_asserts test_assert_error │ test_asserts test_assert_greater │ test_asserts test_assert_greater_or_equal │ test_asserts test_assert_length │ test_asserts test_assert_less │ test_asserts test_assert_less_or_equal │ test_asserts test_assert_not_equal │ ⨯ test_dirs test_dirs_command │ test_logger test_critical │ test_logger test_debug │ test_logger test_error │ test_logger test_info │ test_logger test_warning │ test_std test_path_add │ ``` Upon investigating seeing this difference: ``` ╭───┬─────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ 0 │ /var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ │ 1 │ /private/var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ ╰───┴─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ``` This seems unrelated to my changes, but can investigate further if desired. # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. Co-authored-by: Robert Waugh <robert@waugh.io>
2023-04-07 13:46:11 +02:00
fn split_row_helper(v: &Value, regex: &Regex, max_split: Option<usize>, name: Span) -> Vec<Value> {
2021-10-11 20:45:31 +02:00
match v.span() {
Ok(v_span) => {
if let Ok(s) = v.as_string() {
match max_split {
Add regex separators for split row/list/column (#8707) # Description Verified on discord with maintainer Change adds regex separators in split rows/column/list. The primary motivating reason was to make it easier to split on separators with unbounded whitespace without requiring a lot of trim jiggery. But, secondary motivation is the same as the set of all motivations for adding split regex features to most languages. # User-Facing Changes Adds -r option to split rows/column/list. # Tests + Formatting Ran tests, however tests.nu fails with unrelated errors: ``` ~/src/nushell> cargo run -- crates/nu-utils/standard_library/tests.nu 04/02/2023 02:07:25 AM Finished dev [unoptimized + debuginfo] target(s) in 0.24s Running `target/debug/nu crates/nu-utils/standard_library/tests.nu` INF|2023-04-02T02:07:27.060|Running tests in test_asserts INF|2023-04-02T02:07:27.141|Running tests in test_dirs Error: × list is just pwd after initialization INF|2023-04-02T02:07:27.167|Running tests in test_logger INF|2023-04-02T02:07:27.286|Running tests in test_std Error: × some tests did not pass (see complete errors above): │ │ test_asserts test_assert │ test_asserts test_assert_equal │ test_asserts test_assert_error │ test_asserts test_assert_greater │ test_asserts test_assert_greater_or_equal │ test_asserts test_assert_length │ test_asserts test_assert_less │ test_asserts test_assert_less_or_equal │ test_asserts test_assert_not_equal │ ⨯ test_dirs test_dirs_command │ test_logger test_critical │ test_logger test_debug │ test_logger test_error │ test_logger test_info │ test_logger test_warning │ test_std test_path_add │ ``` Upon investigating seeing this difference: ``` ╭───┬─────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ 0 │ /var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ │ 1 │ /private/var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ ╰───┴─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ``` This seems unrelated to my changes, but can investigate further if desired. # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. Co-authored-by: Robert Waugh <robert@waugh.io>
2023-04-07 13:46:11 +02:00
Some(max_split) => regex
.splitn(&s, max_split)
.map(|x: &str| Value::string(x, v_span))
.collect(),
Add regex separators for split row/list/column (#8707) # Description Verified on discord with maintainer Change adds regex separators in split rows/column/list. The primary motivating reason was to make it easier to split on separators with unbounded whitespace without requiring a lot of trim jiggery. But, secondary motivation is the same as the set of all motivations for adding split regex features to most languages. # User-Facing Changes Adds -r option to split rows/column/list. # Tests + Formatting Ran tests, however tests.nu fails with unrelated errors: ``` ~/src/nushell> cargo run -- crates/nu-utils/standard_library/tests.nu 04/02/2023 02:07:25 AM Finished dev [unoptimized + debuginfo] target(s) in 0.24s Running `target/debug/nu crates/nu-utils/standard_library/tests.nu` INF|2023-04-02T02:07:27.060|Running tests in test_asserts INF|2023-04-02T02:07:27.141|Running tests in test_dirs Error: × list is just pwd after initialization INF|2023-04-02T02:07:27.167|Running tests in test_logger INF|2023-04-02T02:07:27.286|Running tests in test_std Error: × some tests did not pass (see complete errors above): │ │ test_asserts test_assert │ test_asserts test_assert_equal │ test_asserts test_assert_error │ test_asserts test_assert_greater │ test_asserts test_assert_greater_or_equal │ test_asserts test_assert_length │ test_asserts test_assert_less │ test_asserts test_assert_less_or_equal │ test_asserts test_assert_not_equal │ ⨯ test_dirs test_dirs_command │ test_logger test_critical │ test_logger test_debug │ test_logger test_error │ test_logger test_info │ test_logger test_warning │ test_std test_path_add │ ``` Upon investigating seeing this difference: ``` ╭───┬─────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ 0 │ /var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ │ 1 │ /private/var/folders/1f/ltbr1m8s5s1811k6n1rhpc0r0000gn/T/test_dirs_c1ed89d6-19f7-47c7-9e1f-74c39f3623b5 │ ╰───┴─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ``` This seems unrelated to my changes, but can investigate further if desired. # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. Co-authored-by: Robert Waugh <robert@waugh.io>
2023-04-07 13:46:11 +02:00
None => regex
.split(&s)
.map(|x: &str| Value::string(x, v_span))
.collect(),
}
2021-10-11 20:45:31 +02:00
} else {
vec![Value::Error {
error: Box::new(ShellError::PipelineMismatch {
exp_input_type: "string".into(),
dst_span: name,
src_span: v_span,
}),
2021-10-11 20:45:31 +02:00
}]
}
}
Err(error) => vec![Value::Error {
error: Box::new(error),
}],
2021-10-09 04:45:25 +02:00
}
}
#[cfg(test)]
mod test {
use super::*;
2021-10-09 04:45:25 +02:00
#[test]
fn test_examples() {
use crate::test_examples;
2021-10-09 04:45:25 +02:00
test_examples(SubCommand {})
}
}