nushell/crates/nu-command/src/strings/str_/replace.rs
Darren Schroeder 4804e6a151
add more input_output_types found from breaking scripts (#9683)
# Description

This PR fixes some problems I found in scripts by adding some additional
input_output_types.

Here's a list of nushell scripts that it fixed. Look for `# broke here:`
below.

This PR fixes 3, 4, 6, 7 by adding additional input_output_types. 1 was
fixed by changing the script. 2. just doesn't work anymore because mkdir
return type has changed. 5, is a problem with the script, the datatype
for `...rest` needed to be removed.

```nushell
# 1.
def terminal-size [] {
    let sz = (input (ansi size) --bytes-until 'R')
    # $sz should look like this
    # Length: 9 (0x9) bytes | printable whitespace ascii_other non_ascii
    # 00000000:   1b 5b 33 38  3b 31 35 30  52                         •[38;150R
    let sz_len = ($sz | bytes length)

    # let's skip the esc[ and R
    let r = ($sz | bytes at 2..($sz_len - 2) | into string)

    # $r should look like 38;150
    # broke here: because $r needed to be a string for split row
    let size = ($r | split row ';')

    # output in record syntax
    {
        rows: ($size | get 0)
        columns: ($size | get 1)
    }
}

# 2.
# make and cd to a folder
def-env mkcd [name: path] {
    # broke here: but apparently doesn't work anymore
    # It looks like  mkdir returns nothing where it used to return a value
    cd (mkdir $name -v | first) 
}

# 3.
# changed 'into datetime'
def get-monday [] {
  (seq date -r --days 7 |
  # broke here: because into datetime didn't support list input
   into datetime | 
   where { |e| 
   ($e | date format %u) == "1" }).0 | 
   date format "%Y-%m-%d"
}

# 4.
# Delete all branches that are not in the excepts list
# Usage: del-branches [main]
def del-branches [
    excepts:list  # don't delete branch in the list
    --dry-run(-d) # do a dry-run
 ] {
    let branches = (git branch | lines | str trim)
    # broke here: because str replace didn't support list<string>
    let remote_branches = (git branch -r | lines | str replace '^.+?/' '' | uniq)
    if $dry_run {
        print "Starting Dry-Run"
    } else {
        print "Deleting for real"
    }
    $branches | each {|it|
        if ($it not-in $excepts) and ($it not-in $remote_branches) and (not ($it | str starts-with "*")) {
            # git branch -D $it
            if $dry_run {
                print $"git branch -D ($it)"
            } else {
                print $"Deleting ($it) for real"
                #git branch -D $it
            }
        }
    }
}

# 5.
# zoxide script
def-env __zoxide_z [...rest] {
  # `z -` does not work yet, see https://github.com/nushell/nushell/issues/4769
  # broke here: 'append doesn't support string input'
  let arg0 = ($rest | append '~').0
  # broke here: 'length doesn't support string input' so change `...rest:string` to `...rest`
  let path = if (($rest | length) <= 1) and ($arg0 == '-' or ($arg0 | path expand | path type) == dir) {
    $arg0
  } else {
    (zoxide query --exclude $env.PWD -- $rest | str trim -r -c "\n")
  }
  cd $path
}

# 6.
def a [] { 
    let x = (commandline)
    if ($x | is-empty) { return }
    # broke here: because commandline was previously only returning Type::Nothing
    if not ($x | str starts-with "aaa") { print "bbb" }
}

# 7.
# repeat a string x amount of times
def repeat [arg: string, dupe: int] {
  # broke here: 'command does not support range input'
  0..<$dupe | reduce -f '' {|i acc| $acc + $arg}
}
```

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# 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 -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# 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.
-->
2023-07-14 10:58:41 -05:00

301 lines
11 KiB
Rust

use fancy_regex::{NoExpand, Regex};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type,
Value,
};
struct Arguments {
all: bool,
find: Spanned<String>,
replace: Spanned<String>,
cell_paths: Option<Vec<CellPath>>,
literal_replace: bool,
no_regex: bool,
multiline: bool,
}
impl CmdArgument for Arguments {
fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
self.cell_paths.take()
}
}
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str replace"
}
fn signature(&self) -> Signature {
Signature::build("str replace")
.input_output_types(vec![
(Type::String, Type::String),
(Type::Table(vec![]), Type::Table(vec![])),
(
Type::List(Box::new(Type::String)),
Type::List(Box::new(Type::String)),
),
])
.vectorizes_over_list(true)
.required("find", SyntaxShape::String, "the pattern to find")
.required("replace", SyntaxShape::String, "the replacement string")
.rest(
"rest",
SyntaxShape::CellPath,
"For a data structure input, operate on strings at the given cell paths",
)
.switch("all", "replace all occurrences of the pattern", Some('a'))
.switch(
"no-expand",
"do not expand capture groups (like $name) in the replacement string",
Some('n'),
)
.switch(
"string",
"match the pattern as a substring of the input, instead of a regular expression",
Some('s'),
)
.switch(
"multiline",
"multi-line regex mode: ^ and $ match begin/end of line; equivalent to (?m)",
Some('m'),
)
.allow_variants_without_examples(true)
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Find and replace text."
}
fn search_terms(&self) -> Vec<&str> {
vec!["search", "shift", "switch", "regex"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let find: Spanned<String> = call.req(engine_state, stack, 0)?;
let replace: Spanned<String> = call.req(engine_state, stack, 1)?;
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 2)?;
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
let literal_replace = call.has_flag("no-expand");
let no_regex = call.has_flag("string");
let multiline = call.has_flag("multiline");
let args = Arguments {
all: call.has_flag("all"),
find,
replace,
cell_paths,
literal_replace,
no_regex,
multiline,
};
operate(action, args, input, call.head, engine_state.ctrlc.clone())
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Find and replace contents with capture group",
example: "'my_library.rb' | str replace '(.+).rb' '$1.nu'",
result: Some(Value::test_string("my_library.nu")),
},
Example {
description: "Find and replace all occurrences of find string",
example: "'abc abc abc' | str replace -a 'b' 'z'",
result: Some(Value::test_string("azc azc azc")),
},
Example {
description: "Find and replace all occurrences of find string in table",
example:
"[[ColA ColB ColC]; [abc abc ads]] | str replace -a 'b' 'z' ColA ColC",
result: Some(Value::List {
vals: vec![Value::Record {
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()],
vals: vec![
Value::test_string("azc"),
Value::test_string("abc"),
Value::test_string("ads"),
],
span: Span::test_data(),
}],
span: Span::test_data(),
}),
},
Example {
description: "Find and replace contents without using the replace parameter as a regular expression",
example: r"'dogs_$1_cats' | str replace '\$1' '$2' -n",
result: Some(Value::test_string("dogs_$2_cats")),
},
Example {
description: "Find and replace the first occurrence using string replacement *not* regular expressions",
example: r"'c:\some\cool\path' | str replace 'c:\some\cool' '~' -s",
result: Some(Value::test_string("~\\path")),
},
Example {
description: "Find and replace all occurrences using string replacement *not* regular expressions",
example: r#"'abc abc abc' | str replace -a 'b' 'z' -s"#,
result: Some(Value::test_string("azc azc azc")),
},
Example {
description: "Use captures to manipulate the input text",
example: r#""abc-def" | str replace "(.+)-(.+)" "${2}_${1}""#,
result: Some(Value::test_string("def_abc")),
},
Example {
description: "Find and replace with fancy-regex",
example: r"'a successful b' | str replace '\b([sS])uc(?:cs|s?)e(ed(?:ed|ing|s?)|ss(?:es|ful(?:ly)?|i(?:ons?|ve(?:ly)?)|ors?)?)\b' '${1}ucce$2'",
result: Some(Value::test_string("a successful b")),
},
Example {
description: "Find and replace with fancy-regex",
example: r#"'GHIKK-9+*' | str replace '[*[:xdigit:]+]' 'z'"#,
result: Some(Value::test_string("GHIKK-z+*")),
},
Example {
description: "Find and replace on individual lines (multiline)",
example: r#""non-matching line\n123. one line\n124. another line\n" | str replace -am '^[0-9]+\. ' ''"#,
result: Some(Value::test_string("non-matching line\none line\nanother line\n")),
},
]
}
}
struct FindReplace<'a>(&'a str, &'a str);
fn action(
input: &Value,
Arguments {
find,
replace,
all,
literal_replace,
no_regex,
multiline,
..
}: &Arguments,
head: Span,
) -> Value {
match input {
Value::String { val, .. } => {
let FindReplace(find_str, replace_str) = FindReplace(&find.item, &replace.item);
if *no_regex {
// just use regular string replacement vs regular expressions
if *all {
Value::String {
val: val.replace(find_str, replace_str),
span: head,
}
} else {
Value::String {
val: val.replacen(find_str, replace_str, 1),
span: head,
}
}
} else {
// use regular expressions to replace strings
let flags = match multiline {
true => "(?m)",
false => "",
};
let regex_string = flags.to_string() + find_str;
let regex = Regex::new(&regex_string);
match regex {
Ok(re) => {
if *all {
Value::String {
val: {
if *literal_replace {
re.replace_all(val, NoExpand(replace_str)).to_string()
} else {
re.replace_all(val, replace_str).to_string()
}
},
span: head,
}
} else {
Value::String {
val: {
if *literal_replace {
re.replace(val, NoExpand(replace_str)).to_string()
} else {
re.replace(val, replace_str).to_string()
}
},
span: head,
}
}
}
Err(e) => Value::Error {
error: Box::new(ShellError::IncorrectValue {
msg: format!("Regex error: {e}"),
span: find.span,
}),
},
}
}
}
Value::Error { .. } => input.clone(),
_ => Value::Error {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "string".into(),
wrong_type: input.get_type().to_string(),
dst_span: head,
src_span: input.expect_span(),
}),
},
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::{action, Arguments, SubCommand};
fn test_spanned_string(val: &str) -> Spanned<String> {
Spanned {
item: String::from(val),
span: Span::test_data(),
}
}
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
#[test]
fn can_have_capture_groups() {
let word = Value::test_string("Cargo.toml");
let options = Arguments {
find: test_spanned_string("Cargo.(.+)"),
replace: test_spanned_string("Carga.$1"),
cell_paths: None,
literal_replace: false,
all: false,
no_regex: false,
multiline: false,
};
let actual = action(&word, &options, Span::test_data());
assert_eq!(actual, Value::test_string("Carga.toml"));
}
}