Skip to content

Commit

Permalink
Add --number flag to split column (#13831)
Browse files Browse the repository at this point in the history
This allows parsing of data (e.g. key-value pairs) where the last column
may contain the delimiter.

- this PR should close #13742

# Description

Adds a `--number (-n)` flag to `split column`, analogous to `split row
--number`.

```
~> ['author: Salina Yoon' r#'title: Where's Ellie?: A Hide-and-Seek Book'#] | split column --number 2 ': ' key value
╭───┬────────┬──────────────────────────────────────╮
│ # │  key   │                value                 │
├───┼────────┼──────────────────────────────────────┤
│ 0 │ author │ Salina Yoon                          │
│ 1 │ title  │ Where's Ellie?: A Hide-and-Seek Book │
╰───┴────────┴──────────────────────────────────────╯
```

# User-Facing Changes
* `split column` gains a `--number` option

# Tests   Formatting
Tests included in strings::split::column::test::test_examples and
commands::split_column::to_column.

# After Submitting
Reference documentation is auto-generated from code. No other
documentation updates necessary.
  • Loading branch information
nome committed Sep 12, 2024
1 parent fb34a4f commit 5101b5e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
51 changes: 46 additions & 5 deletions crates/nu-command/src/strings/split/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 26,12 @@ impl Command for SubCommand {
"The character or string that denotes what separates columns.",
)
.switch("collapse-empty", "remove empty columns", Some('c'))
.named(
"number",
SyntaxShape::Int,
"Split into maximum number of items",
Some('n'),
)
.switch("regex", "separator is a regular expression", Some('r'))
.rest(
"rest",
Expand Down Expand Up @@ -91,6 97,20 @@ impl Command for SubCommand {
}),
])),
},
Example {
description: "Split into columns, last column may contain the delimiter",
example: r"['author: Salina Yoon' r#'title: Where's Ellie?: A Hide-and-Seek Book'#] | split column --number 2 ': ' key value",
result: Some(Value::test_list(vec![
Value::test_record(record! {
"key" => Value::test_string("author"),
"value" => Value::test_string("Salina Yoon"),
}),
Value::test_record(record! {
"key" => Value::test_string("title"),
"value" => Value::test_string("Where's Ellie?: A Hide-and-Seek Book"),
}),
])),
},
]
}

Expand All @@ -108,12 128,14 @@ impl Command for SubCommand {
let separator: Spanned<String> = call.req(engine_state, stack, 0)?;
let rest: Vec<Spanned<String>> = call.rest(engine_state, stack, 1)?;
let collapse_empty = call.has_flag(engine_state, stack, "collapse-empty")?;
let max_split: Option<usize> = call.get_flag(engine_state, stack, "number")?;
let has_regex = call.has_flag(engine_state, stack, "regex")?;

let args = Arguments {
separator,
rest,
collapse_empty,
max_split,
has_regex,
};
split_column(engine_state, call, input, args)
Expand All @@ -128,12 150,14 @@ impl Command for SubCommand {
let separator: Spanned<String> = call.req_const(working_set, 0)?;
let rest: Vec<Spanned<String>> = call.rest_const(working_set, 1)?;
let collapse_empty = call.has_flag_const(working_set, "collapse-empty")?;
let max_split: Option<usize> = call.get_flag_const(working_set, "number")?;
let has_regex = call.has_flag_const(working_set, "regex")?;

let args = Arguments {
separator,
rest,
collapse_empty,
max_split,
has_regex,
};
split_column(working_set.permanent(), call, input, args)
Expand All @@ -144,6 168,7 @@ struct Arguments {
separator: Spanned<String>,
rest: Vec<Spanned<String>>,
collapse_empty: bool,
max_split: Option<usize>,
has_regex: bool,
}

Expand All @@ -169,7 194,16 @@ fn split_column(
})?;

input.flat_map(
move |x| split_column_helper(&x, &regex, &args.rest, args.collapse_empty, name_span),
move |x| {
split_column_helper(
&x,
&regex,
&args.rest,
args.collapse_empty,
args.max_split,
name_span,
)
},
engine_state.signals(),
)
}
Expand All @@ -179,13 213,20 @@ fn split_column_helper(
separator: &Regex,
rest: &[Spanned<String>],
collapse_empty: bool,
max_split: Option<usize>,
head: Span,
) -> Vec<Value> {
if let Ok(s) = v.coerce_str() {
let split_result: Vec<_> = separator
.split(&s)
.filter(|x| !(collapse_empty && x.is_empty()))
.collect();
let split_result: Vec<_> = match max_split {
Some(max_split) => separator
.splitn(&s, max_split)
.filter(|x| !(collapse_empty && x.is_empty()))
.collect(),
None => separator
.split(&s)
.filter(|x| !(collapse_empty && x.is_empty()))
.collect(),
};
let positional: Vec<_> = rest.iter().map(|f| f.item.clone()).collect();

// If they didn't provide column names, make up our own
Expand Down
13 changes: 13 additions & 0 deletions crates/nu-command/tests/commands/split_column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 33,19 @@ fn to_column() {

assert!(actual.out.contains("shipper"));

let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open sample.txt
| lines
| str trim
| split column -n 3 ","
| get column3
"#
));

assert!(actual.out.contains("tariff_item,name,origin"));

let actual = nu!(
cwd: dirs.test(), pipeline(
r"
Expand Down

0 comments on commit 5101b5e

Please sign in to comment.