Highlights find upgrade (#11509)

this PR should close #9105

# Description
I have implemented highlights for find which work for all strings. The
implementation also works for lists, but with exceptions (for example,
it does not work for list of lists). The implementation is also not
implemented for --regex.

---------

Co-authored-by: Georgiana <geo@LAPTOP-EQP6H37N>
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
Georgiana Grigoreanu
2024-01-30 16:06:20 +02:00
committed by GitHub
parent c08f46f836
commit 6530403ff8
2 changed files with 50 additions and 8 deletions

View File

@ -75,9 +75,9 @@ impl Command for Find {
result: None,
},
Example {
description: "Search for a term in a string",
description: "Search and highlight text for a term in a string",
example: r#"'Cargo.toml' | find toml"#,
result: Some(Value::test_string("Cargo.toml".to_owned())),
result: Some(Value::test_string("\u{1b}[37mCargo.\u{1b}[0m\u{1b}[41;37mtoml\u{1b}[0m\u{1b}[37m\u{1b}[0m".to_owned())),
},
Example {
description: "Search a number or a file size in a list of numbers",
@ -91,7 +91,7 @@ impl Command for Find {
description: "Search a char in a list of string",
example: r#"[moe larry curly] | find l"#,
result: Some(Value::list(
vec![Value::test_string("larry"), Value::test_string("curly")],
vec![Value::test_string("\u{1b}[37m\u{1b}[0m\u{1b}[41;37ml\u{1b}[0m\u{1b}[37marry\u{1b}[0m"), Value::test_string("\u{1b}[37mcur\u{1b}[0m\u{1b}[41;37ml\u{1b}[0m\u{1b}[37my\u{1b}[0m")],
Span::test_data(),
)),
},
@ -276,6 +276,31 @@ where
})
}
fn highlight_terms_in_string(
val: &Value,
span: Span,
config: &Config,
terms: &[Value],
string_style: Style,
highlight_style: Style,
) -> Value {
let val_str = val.into_string("", config);
if let Some(term) = terms
.iter()
.find(|term| contains_ignore_case(&val_str, &term.into_string("", config)))
{
let term_str = term.into_string("", config);
let highlighted_str =
highlight_search_string(&val_str, &term_str, &string_style, &highlight_style)
.unwrap_or_else(|_| string_style.paint(&term_str).to_string());
return Value::string(highlighted_str, span);
}
val.clone()
}
#[allow(clippy::too_many_arguments)]
fn highlight_terms_in_record_with_search_columns(
search_cols: &[String],
@ -369,6 +394,14 @@ fn find_with_rest_and_highlight(
string_style,
highlight_style,
),
Value::String { .. } => highlight_terms_in_string(
&x,
span,
&config,
&terms,
string_style,
highlight_style,
),
_ => x,
}
},