From 8c7e2dbdf9a85cb5d08b3de76a71174159d693b3 Mon Sep 17 00:00:00 2001 From: pwygab <88221256+merelymyself@users.noreply.github.com> Date: Sun, 29 Jan 2023 21:34:34 +0800 Subject: [PATCH] make `parse -r` columns return 0-indexed uncapitalised (#7897) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description Fixes #7886. ``` /home/gabriel/CodingProjects/nushell〉'A|B|C' | parse -r '(\w)\|(\w)\|(\w)' 01/29/2023 01:08:29 PM ╭───┬──────────┬──────────┬──────────╮ │ # │ capture0 │ capture1 │ capture2 │ ├───┼──────────┼──────────┼──────────┤ │ 0 │ A │ B │ C │ ╰───┴──────────┴──────────┴──────────╯ ``` # User-Facing Changes Columns automatically named by `parse -r` are now 0-indexec and uncapitalised. # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # 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. --- crates/nu-command/src/strings/parse.rs | 10 +++++----- crates/nu-command/tests/commands/parse.rs | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/nu-command/src/strings/parse.rs b/crates/nu-command/src/strings/parse.rs index c538a35408..07aae3efd8 100644 --- a/crates/nu-command/src/strings/parse.rs +++ b/crates/nu-command/src/strings/parse.rs @@ -74,12 +74,12 @@ impl Command for Parse { result: Some(Value::List { vals: vec![ Value::Record { - cols: vec!["Capture1".to_string(), "Capture2".to_string()], + cols: vec!["capture0".to_string(), "capture1".to_string()], vals: vec![Value::test_string(""), Value::test_string("foo")], span: Span::test_data(), }, Value::Record { - cols: vec!["Capture1".to_string(), "Capture2".to_string()], + cols: vec!["capture0".to_string(), "capture1".to_string()], vals: vec![Value::test_string("bar"), Value::test_string("")], span: Span::test_data(), }, @@ -93,7 +93,7 @@ impl Command for Parse { "\" @another(foo bar) \" | parse -r '\\s*(?<=[() ])(@\\w+)(\\([^)]*\\))?\\s*'", result: Some(Value::List { vals: vec![Value::Record { - cols: vec!["Capture1".to_string(), "Capture2".to_string()], + cols: vec!["capture0".to_string(), "capture1".to_string()], vals: vec![ Value::test_string("@another"), Value::test_string("(foo bar)"), @@ -108,7 +108,7 @@ impl Command for Parse { example: "\"abcd\" | parse -r '^a(bc(?=d)|b)cd$'", result: Some(Value::List { vals: vec![Value::Record { - cols: vec!["Capture1".to_string()], + cols: vec!["capture0".to_string()], vals: vec![Value::test_string("b")], span: Span::test_data(), }], @@ -276,7 +276,7 @@ fn column_names(regex: &Regex) -> Vec { .skip(1) .map(|(i, name)| { name.map(String::from) - .unwrap_or_else(|| format!("Capture{}", i)) + .unwrap_or_else(|| format!("capture{}", i - 1)) }) .collect() } diff --git a/crates/nu-command/tests/commands/parse.rs b/crates/nu-command/tests/commands/parse.rs index 1597f029b3..6998443a27 100644 --- a/crates/nu-command/tests/commands/parse.rs +++ b/crates/nu-command/tests/commands/parse.rs @@ -144,7 +144,7 @@ mod regex { open nushell_git_log_oneline.txt | parse --regex "(\\w+) (.+) \\(#(\\d+)\\)" | get 1 - | get Capture1 + | get capture0 "# )); @@ -163,7 +163,7 @@ mod regex { open nushell_git_log_oneline.txt | parse --regex "(?P\\w+) (.+) \\(#(?P\\d+)\\)" | get 1 - | get Capture2 + | get capture1 "# ));