use "search_result" style to colorize matching strings (fixes #9275) (#9326)

This change introduces new `search_result` style supported in the color
config. The change also removes obsolete check for `config.ls_colors`
for computing the style. `config.ls_colors` has been removed last year,
so this removes the reference to the obsolete flag, along with a cleanup
that removes all the code that used to rely on ls_colors for
highlighting search results.


# 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.
-->

# 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 -- crates/nu-std/tests/run.nu` 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.
-->
This commit is contained in:
solodov 2023-06-01 16:51:18 -04:00 committed by GitHub
parent 3aab69110e
commit 55689ddb50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 69 additions and 97 deletions

View File

@ -2,10 +2,7 @@ use crate::help_aliases::help_aliases;
use crate::help_commands::help_commands; use crate::help_commands::help_commands;
use crate::help_modules::help_modules; use crate::help_modules::help_modules;
use fancy_regex::Regex; use fancy_regex::Regex;
use nu_ansi_term::{ use nu_ansi_term::Style;
Color::{Red, White},
Style,
};
use nu_engine::CallExt; use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
@ -141,6 +138,7 @@ pub fn highlight_search_in_table(
search_string: &str, search_string: &str,
searched_cols: &[&str], searched_cols: &[&str],
string_style: &Style, string_style: &Style,
highlight_style: &Style,
) -> Result<Vec<Value>, ShellError> { ) -> Result<Vec<Value>, ShellError> {
let orig_search_string = search_string; let orig_search_string = search_string;
let search_string = search_string.to_lowercase(); let search_string = search_string.to_lowercase();
@ -164,7 +162,12 @@ pub fn highlight_search_in_table(
if let Value::String { val: s, span } = val { if let Value::String { val: s, span } = val {
if s.to_lowercase().contains(&search_string) { if s.to_lowercase().contains(&search_string) {
*val = Value::String { *val = Value::String {
val: highlight_search_string(s, orig_search_string, string_style)?, val: highlight_search_string(
s,
orig_search_string,
string_style,
highlight_style,
)?,
span: *span, span: *span,
}; };
Ok(true) Ok(true)
@ -200,6 +203,7 @@ pub fn highlight_search_string(
haystack: &str, haystack: &str,
needle: &str, needle: &str,
string_style: &Style, string_style: &Style,
highlight_style: &Style,
) -> Result<String, ShellError> { ) -> Result<String, ShellError> {
let regex_string = format!("(?i){needle}"); let regex_string = format!("(?i){needle}");
let regex = match Regex::new(&regex_string) { let regex = match Regex::new(&regex_string) {
@ -217,7 +221,6 @@ pub fn highlight_search_string(
// strip haystack to remove existing ansi style // strip haystack to remove existing ansi style
let stripped_haystack = nu_utils::strip_ansi_likely(haystack); let stripped_haystack = nu_utils::strip_ansi_likely(haystack);
let mut last_match_end = 0; let mut last_match_end = 0;
let style = Style::new().fg(White).on(Red);
let mut highlighted = String::new(); let mut highlighted = String::new();
for cap in regex.captures_iter(stripped_haystack.as_ref()) { for cap in regex.captures_iter(stripped_haystack.as_ref()) {
@ -236,7 +239,11 @@ pub fn highlight_search_string(
.paint(&stripped_haystack[last_match_end..start]) .paint(&stripped_haystack[last_match_end..start])
.to_string(), .to_string(),
); );
highlighted.push_str(&style.paint(&stripped_haystack[start..end]).to_string()); highlighted.push_str(
&highlight_style
.paint(&stripped_haystack[start..end])
.to_string(),
);
last_match_end = end; last_match_end = end;
} }
Err(e) => { Err(e) => {

View File

@ -84,11 +84,18 @@ pub fn help_aliases(
// Also note that this sample string is passed into user-written code (the closure that may or may not be // Also note that this sample string is passed into user-written code (the closure that may or may not be
// defined for "string"). // defined for "string").
let string_style = style_computer.compute("string", &Value::string("search result", head)); let string_style = style_computer.compute("string", &Value::string("search result", head));
let highlight_style =
style_computer.compute("search_result", &Value::string("search result", head));
if let Some(f) = find { if let Some(f) = find {
let all_cmds_vec = build_help_aliases(engine_state, stack, head); let all_cmds_vec = build_help_aliases(engine_state, stack, head);
let found_cmds_vec = let found_cmds_vec = highlight_search_in_table(
highlight_search_in_table(all_cmds_vec, &f.item, &["name", "usage"], &string_style)?; all_cmds_vec,
&f.item,
&["name", "usage"],
&string_style,
&highlight_style,
)?;
return Ok(found_cmds_vec return Ok(found_cmds_vec
.into_iter() .into_iter()

View File

@ -65,6 +65,8 @@ pub fn help_commands(
// Also note that this sample string is passed into user-written code (the closure that may or may not be // Also note that this sample string is passed into user-written code (the closure that may or may not be
// defined for "string"). // defined for "string").
let string_style = style_computer.compute("string", &Value::string("search result", head)); let string_style = style_computer.compute("string", &Value::string("search result", head));
let highlight_style =
style_computer.compute("search_result", &Value::string("search result", head));
if let Some(f) = find { if let Some(f) = find {
let all_cmds_vec = build_help_commands(engine_state, head); let all_cmds_vec = build_help_commands(engine_state, head);
@ -73,6 +75,7 @@ pub fn help_commands(
&f.item, &f.item,
&["name", "usage", "search_terms"], &["name", "usage", "search_terms"],
&string_style, &string_style,
&highlight_style,
)?; )?;
return Ok(found_cmds_vec return Ok(found_cmds_vec

View File

@ -84,11 +84,18 @@ pub fn help_externs(
// Also note that this sample string is passed into user-written code (the closure that may or may not be // Also note that this sample string is passed into user-written code (the closure that may or may not be
// defined for "string"). // defined for "string").
let string_style = style_computer.compute("string", &Value::string("search result", head)); let string_style = style_computer.compute("string", &Value::string("search result", head));
let highlight_style =
style_computer.compute("search_result", &Value::string("search result", head));
if let Some(f) = find { if let Some(f) = find {
let all_cmds_vec = build_help_externs(engine_state, stack, head); let all_cmds_vec = build_help_externs(engine_state, stack, head);
let found_cmds_vec = let found_cmds_vec = highlight_search_in_table(
highlight_search_in_table(all_cmds_vec, &f.item, &["name", "usage"], &string_style)?; all_cmds_vec,
&f.item,
&["name", "usage"],
&string_style,
&highlight_style,
)?;
return Ok(found_cmds_vec return Ok(found_cmds_vec
.into_iter() .into_iter()

View File

@ -90,11 +90,18 @@ pub fn help_modules(
// Also note that this sample string is passed into user-written code (the closure that may or may not be // Also note that this sample string is passed into user-written code (the closure that may or may not be
// defined for "string"). // defined for "string").
let string_style = style_computer.compute("string", &Value::string("search result", head)); let string_style = style_computer.compute("string", &Value::string("search result", head));
let highlight_style =
style_computer.compute("search_result", &Value::string("search result", head));
if let Some(f) = find { if let Some(f) = find {
let all_cmds_vec = build_help_modules(engine_state, stack, head); let all_cmds_vec = build_help_modules(engine_state, stack, head);
let found_cmds_vec = let found_cmds_vec = highlight_search_in_table(
highlight_search_in_table(all_cmds_vec, &f.item, &["name", "usage"], &string_style)?; all_cmds_vec,
&f.item,
&["name", "usage"],
&string_style,
&highlight_style,
)?;
return Ok(found_cmds_vec return Ok(found_cmds_vec
.into_iter() .into_iter()

View File

@ -163,6 +163,7 @@ impl<'a> StyleComputer<'a> {
("list".to_string(), ComputableStyle::Static(Color::White.normal())), ("list".to_string(), ComputableStyle::Static(Color::White.normal())),
("block".to_string(), ComputableStyle::Static(Color::White.normal())), ("block".to_string(), ComputableStyle::Static(Color::White.normal())),
("hints".to_string(), ComputableStyle::Static(Color::DarkGray.normal())), ("hints".to_string(), ComputableStyle::Static(Color::DarkGray.normal())),
("search_result".to_string(), ComputableStyle::Static(Color::White.normal().on(Color::Red))),
]); ]);
for (key, value) in &config.color_config { for (key, value) in &config.color_config {

View File

@ -1,17 +1,15 @@
use nu_cmd_lang::help::highlight_search_string; use nu_cmd_lang::help::highlight_search_string;
use fancy_regex::Regex; use fancy_regex::Regex;
use lscolors::{Color as LsColors_Color, LsColors, Style as LsColors_Style}; use nu_ansi_term::Style;
use nu_ansi_term::{Color, Style};
use nu_color_config::StyleComputer; use nu_color_config::StyleComputer;
use nu_engine::{env_to_string, CallExt}; use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Config, Example, IntoInterruptiblePipelineData, IntoPipelineData, ListStream, Category, Config, Example, IntoInterruptiblePipelineData, IntoPipelineData, ListStream,
PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
use nu_utils::get_ls_colors;
#[derive(Clone)] #[derive(Clone)]
pub struct Find; pub struct Find;
@ -300,7 +298,7 @@ fn highlight_terms_in_record_with_search_columns(
config: &Config, config: &Config,
terms: &[Value], terms: &[Value],
string_style: Style, string_style: Style,
ls_colors: &LsColors, highlight_style: Style,
) -> Value { ) -> Value {
let cols_to_search = if search_cols.is_empty() { let cols_to_search = if search_cols.is_empty() {
cols.to_vec() cols.to_vec()
@ -316,18 +314,11 @@ fn highlight_terms_in_record_with_search_columns(
let term_str = term.into_string("", config); let term_str = term.into_string("", config);
let output_value = let output_value =
if contains_ignore_case(&val_str, &term_str) && cols_to_search.contains(cur_col) { if contains_ignore_case(&val_str, &term_str) && cols_to_search.contains(cur_col) {
let (value_to_highlight, highlight_string_style) = if config.use_ls_colors {
// Get the original LS_COLORS color
get_colored_value_and_string_style(ls_colors, &val_str, &string_style)
} else {
// No LS_COLORS support, so just use the original value
(val_str.clone(), string_style)
};
let highlighted_str = match highlight_search_string( let highlighted_str = match highlight_search_string(
&value_to_highlight, &val_str,
&term_str, &term_str,
&highlight_string_style, &string_style,
&highlight_style,
) { ) {
Ok(highlighted_str) => highlighted_str, Ok(highlighted_str) => highlighted_str,
Err(_) => string_style.paint(term_str).to_string(), Err(_) => string_style.paint(term_str).to_string(),
@ -350,24 +341,6 @@ fn highlight_terms_in_record_with_search_columns(
} }
} }
fn get_colored_value_and_string_style(
ls_colors: &LsColors,
val_str: &str,
string_style: &Style,
) -> (String, Style) {
let style = ls_colors.style_for_path(val_str);
let ansi_style = style
.map(LsColors_Style::to_nu_ansi_term_style)
.unwrap_or_default();
let ls_colored_val = ansi_style.paint(val_str).to_string();
let ansi_term_style = style
.map(to_nu_ansi_term_style)
.unwrap_or_else(|| *string_style);
(ls_colored_val, ansi_term_style)
}
fn contains_ignore_case(string: &str, substring: &str) -> bool { fn contains_ignore_case(string: &str, substring: &str) -> bool {
string.to_lowercase().contains(&substring.to_lowercase()) string.to_lowercase().contains(&substring.to_lowercase())
} }
@ -401,12 +374,8 @@ fn find_with_rest_and_highlight(
// Also note that this sample string is passed into user-written code (the closure that may or may not be // Also note that this sample string is passed into user-written code (the closure that may or may not be
// defined for "string"). // defined for "string").
let string_style = style_computer.compute("string", &Value::string("search result", span)); let string_style = style_computer.compute("string", &Value::string("search result", span));
let highlight_style =
let ls_colors_env_str = match stack.get_env_var(&engine_state, "LS_COLORS") { style_computer.compute("search_result", &Value::string("search result", span));
Some(v) => Some(env_to_string("LS_COLORS", &v, &engine_state, stack)?),
None => None,
};
let ls_colors = get_ls_colors(ls_colors_env_str);
let cols_to_search_in_map = match call.get_flag(&engine_state, stack, "columns")? { let cols_to_search_in_map = match call.get_flag(&engine_state, stack, "columns")? {
Some(cols) => cols, Some(cols) => cols,
@ -429,7 +398,7 @@ fn find_with_rest_and_highlight(
&config, &config,
&terms, &terms,
string_style, string_style,
&ls_colors, highlight_style,
) )
} }
_ => x, _ => x,
@ -461,7 +430,7 @@ fn find_with_rest_and_highlight(
&config, &config,
&terms, &terms,
string_style, string_style,
&ls_colors, highlight_style,
) )
} }
_ => x, _ => x,
@ -504,6 +473,7 @@ fn find_with_rest_and_highlight(
line, line,
&term_str, &term_str,
&string_style, &string_style,
&highlight_style,
)?, )?,
span, span,
}) })
@ -622,47 +592,6 @@ fn record_matches_term(
}) })
} }
fn to_nu_ansi_term_style(style: &LsColors_Style) -> Style {
fn to_nu_ansi_term_color(color: &LsColors_Color) -> Color {
match *color {
LsColors_Color::Fixed(n) => Color::Fixed(n),
LsColors_Color::RGB(r, g, b) => Color::Rgb(r, g, b),
LsColors_Color::Black => Color::Black,
LsColors_Color::Red => Color::Red,
LsColors_Color::Green => Color::Green,
LsColors_Color::Yellow => Color::Yellow,
LsColors_Color::Blue => Color::Blue,
LsColors_Color::Magenta => Color::Magenta,
LsColors_Color::Cyan => Color::Cyan,
LsColors_Color::White => Color::White,
// Below items are a rough translations to 256 colors as
// nu-ansi-term do not have bright variants
LsColors_Color::BrightBlack => Color::Fixed(8),
LsColors_Color::BrightRed => Color::Fixed(9),
LsColors_Color::BrightGreen => Color::Fixed(10),
LsColors_Color::BrightYellow => Color::Fixed(11),
LsColors_Color::BrightBlue => Color::Fixed(12),
LsColors_Color::BrightMagenta => Color::Fixed(13),
LsColors_Color::BrightCyan => Color::Fixed(14),
LsColors_Color::BrightWhite => Color::Fixed(15),
}
}
Style {
foreground: style.foreground.as_ref().map(to_nu_ansi_term_color),
background: style.background.as_ref().map(to_nu_ansi_term_color),
is_bold: style.font_style.bold,
is_dimmed: style.font_style.dimmed,
is_italic: style.font_style.italic,
is_underline: style.font_style.underline,
is_blink: style.font_style.slow_blink || style.font_style.rapid_blink,
is_reverse: style.font_style.reverse,
is_hidden: style.font_style.hidden,
is_strikethrough: style.font_style.strikethrough,
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

File diff suppressed because one or more lines are too long

View File

@ -52,6 +52,7 @@ let dark_theme = {
list: white list: white
block: white block: white
hints: dark_gray hints: dark_gray
search_result: {bg: red fg: white}
shape_and: purple_bold shape_and: purple_bold
shape_binary: purple_bold shape_binary: purple_bold
@ -136,6 +137,7 @@ let light_theme = {
list: white list: white
block: white block: white
hints: dark_gray hints: dark_gray
search_result: {fg: white bg: red}
shape_and: purple_bold shape_and: purple_bold
shape_binary: purple_bold shape_binary: purple_bold