mirror of
https://github.com/nushell/nushell.git
synced 2025-04-16 09:18:21 +02:00
Add --raw to find command (#14970)
<!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> When using `find`, we insert ansi code. This is great for visual but it make comparison a tedious task. For exemple ```nu > ([{a: 1 b: 1}] | find 1 | get 0 | get a) == 1 # false ``` The documentation recommand using the `ansi strip` command but you then lose your typing converting it to a string. ```nu > [{a: 1 b: 1}] | find 1 | get 0 | get a | ansi strip | describe # string ``` And this makes me very sad 😢 . The idea here is to have a simple option to keep the usage of `find` without the ansi marking. ```nu > ([{a: 1 b: 1}] | find --raw 1 | get 0 | get a) == 1 # true ``` Tbh I think we could also do a fix on the parser to really escape the ansi makers but this sounded like way more work so I would like your opinion on this before working on it. Also note that this is my first time writting rust and trying to contribute to nushell so if you see any weird shenanigans be sure to tell me ! # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> A new flag for find # 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` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` 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 > ``` --> For testing I updated all the previous `find` test to also run them with this new flag just to be sure that we didn't lose any other functionalities # 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: Tangui <mael.nicolas@clever-cloud.com>
This commit is contained in:
parent
b55ed69c92
commit
1349187e17
@ -52,6 +52,11 @@ impl Command for Find {
|
||||
"column names to be searched (with rest parameter, not regex yet)",
|
||||
Some('c'),
|
||||
)
|
||||
.switch(
|
||||
"no-highlight",
|
||||
"no-highlight mode: find without marking with ascii code",
|
||||
Some('n'),
|
||||
)
|
||||
.switch("invert", "invert the match", Some('v'))
|
||||
.rest("rest", SyntaxShape::Any, "Terms to search.")
|
||||
.category(Category::Filters)
|
||||
@ -161,8 +166,15 @@ impl Command for Find {
|
||||
},
|
||||
Example {
|
||||
description: "Remove ANSI sequences from result",
|
||||
example: "[[foo bar]; [abc 123] [def 456]] | find 123 | get bar | ansi strip",
|
||||
result: None, // This is None because ansi strip is not available in tests
|
||||
example:"[[foo bar]; [abc 123] [def 456]] | find --no-highlight 123",
|
||||
result: Some(Value::list(
|
||||
vec![Value::test_record(record! {
|
||||
"foo" => Value::test_string("abc"),
|
||||
"bar" => Value::test_int(123)
|
||||
}
|
||||
)],
|
||||
Span::test_data(),
|
||||
))
|
||||
},
|
||||
Example {
|
||||
description: "Find and highlight text in specific columns",
|
||||
@ -350,6 +362,7 @@ fn find_with_rest_and_highlight(
|
||||
let span = call.head;
|
||||
let config = stack.get_config(engine_state);
|
||||
let filter_config = config.clone();
|
||||
let no_highlight = call.has_flag(engine_state, stack, "no-highlight")?;
|
||||
let invert = call.has_flag(engine_state, stack, "invert")?;
|
||||
let terms = call.rest::<Value>(engine_state, stack, 0)?;
|
||||
let lower_terms = terms
|
||||
@ -377,6 +390,9 @@ fn find_with_rest_and_highlight(
|
||||
.map(
|
||||
move |mut x| {
|
||||
let span = x.span();
|
||||
if no_highlight {
|
||||
return x;
|
||||
};
|
||||
match &mut x {
|
||||
Value::Record { val, .. } => highlight_terms_in_record_with_search_columns(
|
||||
&cols_to_search_in_map,
|
||||
@ -417,6 +433,9 @@ fn find_with_rest_and_highlight(
|
||||
let stream = stream.modify(|iter| {
|
||||
iter.map(move |mut x| {
|
||||
let span = x.span();
|
||||
if no_highlight {
|
||||
return x;
|
||||
};
|
||||
match &mut x {
|
||||
Value::Record { val, .. } => highlight_terms_in_record_with_search_columns(
|
||||
&cols_to_search_in_map,
|
||||
@ -458,15 +477,19 @@ fn find_with_rest_and_highlight(
|
||||
let lower_val = line.to_lowercase();
|
||||
for term in &terms {
|
||||
if lower_val.contains(term) {
|
||||
output.push(Value::string(
|
||||
highlight_search_string(
|
||||
&line,
|
||||
term,
|
||||
&string_style,
|
||||
&highlight_style,
|
||||
)?,
|
||||
span,
|
||||
))
|
||||
if no_highlight {
|
||||
output.push(Value::string(&line, span))
|
||||
} else {
|
||||
output.push(Value::string(
|
||||
highlight_search_string(
|
||||
&line,
|
||||
term,
|
||||
&string_style,
|
||||
&highlight_style,
|
||||
)?,
|
||||
span,
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,78 +3,103 @@ use nu_test_support::nu;
|
||||
#[test]
|
||||
fn find_with_list_search_with_string() {
|
||||
let actual = nu!("[moe larry curly] | find moe | get 0");
|
||||
let actual_no_highlight = nu!("[moe larry curly] | find --no-highlight moe | get 0");
|
||||
|
||||
assert_eq!(
|
||||
actual.out,
|
||||
"\u{1b}[37m\u{1b}[0m\u{1b}[41;37mmoe\u{1b}[0m\u{1b}[37m\u{1b}[0m"
|
||||
);
|
||||
assert_eq!(actual_no_highlight.out, "moe");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_with_list_search_with_char() {
|
||||
let actual = nu!("[moe larry curly] | find l | to json -r");
|
||||
let actual_no_highlight = nu!("[moe larry curly] | find --no-highlight l | to json -r");
|
||||
|
||||
assert_eq!(actual.out, "[\"\\u001b[37m\\u001b[0m\\u001b[41;37ml\\u001b[0m\\u001b[37marry\\u001b[0m\",\"\\u001b[37mcur\\u001b[0m\\u001b[41;37ml\\u001b[0m\\u001b[37my\\u001b[0m\"]");
|
||||
assert_eq!(actual_no_highlight.out, "[\"larry\",\"curly\"]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_with_bytestream_search_with_char() {
|
||||
let actual =
|
||||
nu!("\"ABC\" | save foo.txt; let res = open foo.txt | find abc; rm foo.txt; $res | get 0");
|
||||
let actual_no_highlight =
|
||||
nu!("\"ABC\" | save foo.txt; let res = open foo.txt | find --no-highlight abc; rm foo.txt; $res | get 0");
|
||||
|
||||
assert_eq!(
|
||||
actual.out,
|
||||
"\u{1b}[37m\u{1b}[0m\u{1b}[41;37mABC\u{1b}[0m\u{1b}[37m\u{1b}[0m"
|
||||
)
|
||||
);
|
||||
assert_eq!(actual_no_highlight.out, "ABC");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_with_list_search_with_number() {
|
||||
let actual = nu!("[1 2 3 4 5] | find 3 | get 0");
|
||||
let actual_no_highlight = nu!("[1 2 3 4 5] | find --no-highlight 3 | get 0");
|
||||
|
||||
assert_eq!(actual.out, "3");
|
||||
assert_eq!(actual_no_highlight.out, "3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_with_string_search_with_string() {
|
||||
let actual = nu!("echo Cargo.toml | find toml");
|
||||
let actual_no_highlight = nu!("echo Cargo.toml | find --no-highlight toml");
|
||||
|
||||
assert_eq!(
|
||||
actual.out,
|
||||
"\u{1b}[37mCargo.\u{1b}[0m\u{1b}[41;37mtoml\u{1b}[0m\u{1b}[37m\u{1b}[0m"
|
||||
);
|
||||
assert_eq!(actual_no_highlight.out, "Cargo.toml");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_with_string_search_with_string_not_found() {
|
||||
let actual = nu!("[moe larry curly] | find shemp | is-empty");
|
||||
let actual_no_highlight = nu!("[moe larry curly] | find --no-highlight shemp | is-empty");
|
||||
|
||||
assert_eq!(actual.out, "true");
|
||||
assert_eq!(actual_no_highlight.out, "true");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_with_filepath_search_with_string() {
|
||||
let actual =
|
||||
nu!(r#"["amigos.txt","arepas.clu","los.txt","tres.txt"] | find arep | to json -r"#);
|
||||
let actual_no_highlight = nu!(
|
||||
r#"["amigos.txt","arepas.clu","los.txt","tres.txt"] | find --no-highlight arep | to json -r"#
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
actual.out,
|
||||
"[\"\\u001b[37m\\u001b[0m\\u001b[41;37marep\\u001b[0m\\u001b[37mas.clu\\u001b[0m\"]"
|
||||
);
|
||||
assert_eq!(actual_no_highlight.out, "[\"arepas.clu\"]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_with_filepath_search_with_multiple_patterns() {
|
||||
let actual =
|
||||
nu!(r#"["amigos.txt","arepas.clu","los.txt","tres.txt"] | find arep ami | to json -r"#);
|
||||
let actual_no_highlight = nu!(
|
||||
r#"["amigos.txt","arepas.clu","los.txt","tres.txt"] | find --no-highlight arep ami | to json -r"#
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "[\"\\u001b[37m\\u001b[0m\\u001b[41;37mami\\u001b[0m\\u001b[37mgos.txt\\u001b[0m\",\"\\u001b[37m\\u001b[0m\\u001b[41;37marep\\u001b[0m\\u001b[37mas.clu\\u001b[0m\"]");
|
||||
assert_eq!(actual_no_highlight.out, "[\"amigos.txt\",\"arepas.clu\"]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_takes_into_account_linebreaks_in_string() {
|
||||
let actual = nu!(r#""atest\nanothertest\nnohit\n" | find a | length"#);
|
||||
let actual_no_highlight =
|
||||
nu!(r#""atest\nanothertest\nnohit\n" | find --no-highlight a | length"#);
|
||||
|
||||
assert_eq!(actual.out, "2");
|
||||
assert_eq!(actual_no_highlight.out, "2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -82,8 +107,12 @@ fn find_with_regex_in_table_keeps_row_if_one_column_matches() {
|
||||
let actual = nu!(
|
||||
"[[name nickname]; [Maurice moe] [Laurence larry]] | find --regex ce | get name | to json -r"
|
||||
);
|
||||
let actual_no_highlight = nu!(
|
||||
"[[name nickname]; [Maurice moe] [Laurence larry]] | find --no-highlight --regex ce | get name | to json -r"
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, r#"["Maurice","Laurence"]"#);
|
||||
assert_eq!(actual_no_highlight.out, r#"["Maurice","Laurence"]"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -91,8 +120,12 @@ fn inverted_find_with_regex_in_table_keeps_row_if_none_of_the_columns_matches()
|
||||
let actual = nu!(
|
||||
"[[name nickname]; [Maurice moe] [Laurence larry]] | find --regex moe --invert | get name | to json -r"
|
||||
);
|
||||
let actual_no_highlight = nu!(
|
||||
"[[name nickname]; [Maurice moe] [Laurence larry]] | find --no-highlight --regex moe --invert | get name | to json -r"
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, r#"["Laurence"]"#);
|
||||
assert_eq!(actual_no_highlight.out, r#"["Laurence"]"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -100,9 +133,14 @@ fn find_in_table_only_keep_rows_with_matches_on_selected_columns() {
|
||||
let actual = nu!(
|
||||
"[[name nickname]; [Maurice moe] [Laurence larry]] | find r --columns [nickname] | get name | to json -r"
|
||||
);
|
||||
let actual_no_highlight = nu!(
|
||||
"[[name nickname]; [Maurice moe] [Laurence larry]] | find r --no-highlight --columns [nickname] | get name | to json -r"
|
||||
);
|
||||
|
||||
assert!(actual.out.contains("Laurence"));
|
||||
assert!(!actual.out.contains("Maurice"));
|
||||
assert!(actual_no_highlight.out.contains("Laurence"));
|
||||
assert!(!actual_no_highlight.out.contains("Maurice"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -110,23 +148,34 @@ fn inverted_find_in_table_keeps_row_if_none_of_the_selected_columns_matches() {
|
||||
let actual = nu!(
|
||||
"[[name nickname]; [Maurice moe] [Laurence larry]] | find r --columns [nickname] --invert | get name | to json -r"
|
||||
);
|
||||
let actual_no_highlight = nu!(
|
||||
"[[name nickname]; [Maurice moe] [Laurence larry]] | find r --no-highlight --columns [nickname] --invert | get name | to json -r"
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, r#"["Maurice"]"#);
|
||||
assert_eq!(actual_no_highlight.out, r#"["Maurice"]"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_in_table_keeps_row_with_single_matched_and_keeps_other_columns() {
|
||||
let actual = nu!("[[name nickname Age]; [Maurice moe 23] [Laurence larry 67] [William will 18]] | find Maurice");
|
||||
let actual_no_highlight = nu!("[[name nickname Age]; [Maurice moe 23] [Laurence larry 67] [William will 18]] | find --no-highlight Maurice");
|
||||
|
||||
println!("{:?}", actual.out);
|
||||
assert!(actual.out.contains("moe"));
|
||||
assert!(actual.out.contains("Maurice"));
|
||||
assert!(actual.out.contains("23"));
|
||||
|
||||
println!("{:?}", actual_no_highlight.out);
|
||||
assert!(actual_no_highlight.out.contains("moe"));
|
||||
assert!(actual_no_highlight.out.contains("Maurice"));
|
||||
assert!(actual_no_highlight.out.contains("23"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_in_table_keeps_row_with_multiple_matched_and_keeps_other_columns() {
|
||||
let actual = nu!("[[name nickname Age]; [Maurice moe 23] [Laurence larry 67] [William will 18] [William bill 60]] | find moe William");
|
||||
let actual_no_highlight = nu!("[[name nickname Age]; [Maurice moe 23] [Laurence larry 67] [William will 18] [William bill 60]] | find --no-highlight moe William");
|
||||
|
||||
println!("{:?}", actual.out);
|
||||
assert!(actual.out.contains("moe"));
|
||||
@ -137,64 +186,92 @@ fn find_in_table_keeps_row_with_multiple_matched_and_keeps_other_columns() {
|
||||
assert!(actual.out.contains("18"));
|
||||
assert!(actual.out.contains("bill"));
|
||||
assert!(actual.out.contains("60"));
|
||||
|
||||
println!("{:?}", actual_no_highlight.out);
|
||||
assert!(actual_no_highlight.out.contains("moe"));
|
||||
assert!(actual_no_highlight.out.contains("Maurice"));
|
||||
assert!(actual_no_highlight.out.contains("23"));
|
||||
assert!(actual_no_highlight.out.contains("William"));
|
||||
assert!(actual_no_highlight.out.contains("will"));
|
||||
assert!(actual_no_highlight.out.contains("18"));
|
||||
assert!(actual_no_highlight.out.contains("bill"));
|
||||
assert!(actual_no_highlight.out.contains("60"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_with_string_search_with_special_char_1() {
|
||||
let actual = nu!("[[d]; [a?b] [a*b] [a{1}b] ] | find '?' | to json -r");
|
||||
let actual_no_highlight =
|
||||
nu!("[[d]; [a?b] [a*b] [a{1}b] ] | find --no-highlight '?' | to json -r");
|
||||
|
||||
assert_eq!(
|
||||
actual.out,
|
||||
"[{\"d\":\"\\u001b[37ma\\u001b[0m\\u001b[41;37m?\\u001b[0m\\u001b[37mb\\u001b[0m\"}]"
|
||||
);
|
||||
assert_eq!(actual_no_highlight.out, "[{\"d\":\"a?b\"}]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_with_string_search_with_special_char_2() {
|
||||
let actual = nu!("[[d]; [a?b] [a*b] [a{1}b]] | find '*' | to json -r");
|
||||
let actual_no_highlight =
|
||||
nu!("[[d]; [a?b] [a*b] [a{1}b]] | find --no-highlight '*' | to json -r");
|
||||
|
||||
assert_eq!(
|
||||
actual.out,
|
||||
"[{\"d\":\"\\u001b[37ma\\u001b[0m\\u001b[41;37m*\\u001b[0m\\u001b[37mb\\u001b[0m\"}]"
|
||||
);
|
||||
assert_eq!(actual_no_highlight.out, "[{\"d\":\"a*b\"}]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_with_string_search_with_special_char_3() {
|
||||
let actual = nu!("[[d]; [a?b] [a*b] [a{1}b] ] | find '{1}' | to json -r");
|
||||
let actual_no_highlight =
|
||||
nu!("[[d]; [a?b] [a*b] [a{1}b] ] | find --no-highlight '{1}' | to json -r");
|
||||
|
||||
assert_eq!(
|
||||
actual.out,
|
||||
"[{\"d\":\"\\u001b[37ma\\u001b[0m\\u001b[41;37m{1}\\u001b[0m\\u001b[37mb\\u001b[0m\"}]"
|
||||
);
|
||||
assert_eq!(actual_no_highlight.out, "[{\"d\":\"a{1}b\"}]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_with_string_search_with_special_char_4() {
|
||||
let actual = nu!("[{d: a?b} {d: a*b} {d: a{1}b} {d: a[]b}] | find '[' | to json -r");
|
||||
let actual_no_highlight =
|
||||
nu!("[{d: a?b} {d: a*b} {d: a{1}b} {d: a[]b}] | find --no-highlight '[' | to json -r");
|
||||
|
||||
assert_eq!(
|
||||
actual.out,
|
||||
"[{\"d\":\"\\u001b[37ma\\u001b[0m\\u001b[41;37m[\\u001b[0m\\u001b[37m]b\\u001b[0m\"}]"
|
||||
);
|
||||
assert_eq!(actual_no_highlight.out, "[{\"d\":\"a[]b\"}]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_with_string_search_with_special_char_5() {
|
||||
let actual = nu!("[{d: a?b} {d: a*b} {d: a{1}b} {d: a[]b}] | find ']' | to json -r");
|
||||
let actual_no_highlight =
|
||||
nu!("[{d: a?b} {d: a*b} {d: a{1}b} {d: a[]b}] | find --no-highlight ']' | to json -r");
|
||||
|
||||
assert_eq!(
|
||||
actual.out,
|
||||
"[{\"d\":\"\\u001b[37ma[\\u001b[0m\\u001b[41;37m]\\u001b[0m\\u001b[37mb\\u001b[0m\"}]"
|
||||
);
|
||||
assert_eq!(actual_no_highlight.out, "[{\"d\":\"a[]b\"}]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_with_string_search_with_special_char_6() {
|
||||
let actual = nu!("[{d: a?b} {d: a*b} {d: a{1}b} {d: a[]b}] | find '[]' | to json -r");
|
||||
let actual_no_highlight =
|
||||
nu!("[{d: a?b} {d: a*b} {d: a{1}b} {d: a[]b}] | find --no-highlight '[]' | to json -r");
|
||||
|
||||
assert_eq!(
|
||||
actual.out,
|
||||
"[{\"d\":\"\\u001b[37ma\\u001b[0m\\u001b[41;37m[]\\u001b[0m\\u001b[37mb\\u001b[0m\"}]"
|
||||
);
|
||||
assert_eq!(actual_no_highlight.out, "[{\"d\":\"a[]b\"}]");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user