From e01974b7abb276ed3f489052285d45402abbffe4 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Fri, 15 Jul 2022 08:06:29 -0500 Subject: [PATCH] Ensure users colors are maintained when highlighting find matches (#6054) --- crates/nu-command/src/core_commands/help.rs | 46 +++++++++++++++------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/crates/nu-command/src/core_commands/help.rs b/crates/nu-command/src/core_commands/help.rs index 3e5bc16273..e7df90a6c2 100644 --- a/crates/nu-command/src/core_commands/help.rs +++ b/crates/nu-command/src/core_commands/help.rs @@ -1,7 +1,8 @@ use nu_ansi_term::{ - Color::{Red, White}, + Color::{Default, Red, White}, Style, }; +use nu_color_config::get_color_config; use nu_engine::{get_full_help, CallExt}; use nu_protocol::{ ast::Call, @@ -84,6 +85,13 @@ fn help( let find: Option> = call.get_flag(engine_state, stack, "find")?; let rest: Vec> = call.rest(engine_state, stack, 0)?; let commands = engine_state.get_decl_ids_sorted(false); + let config = engine_state.get_config(); + let color_hm = get_color_config(config); + let default_style = Style::new().fg(Default).on(Default); + let string_style = match color_hm.get("string") { + Some(style) => style, + None => &default_style, + }; if let Some(f) = find { let org_search_string = f.item.clone(); @@ -108,12 +116,12 @@ fn help( }; let key_match = key.to_lowercase().contains(&search_string); - let use_match = usage.to_lowercase().contains(&search_string); - if key_match || use_match || matches_term { + let usage_match = usage.to_lowercase().contains(&search_string); + if key_match || usage_match || matches_term { cols.push("name".into()); vals.push(Value::String { val: if key_match { - highlight_search_string(&key, &org_search_string)? + highlight_search_string(&key, &org_search_string, string_style)? } else { key }, @@ -146,8 +154,8 @@ fn help( cols.push("usage".into()); vals.push(Value::String { - val: if use_match { - highlight_search_string(&usage, &org_search_string)? + val: if usage_match { + highlight_search_string(&usage, &org_search_string, string_style)? } else { usage }, @@ -164,12 +172,18 @@ fn help( .iter() .map(|term| { if term.to_lowercase().contains(&search_string) { - match highlight_search_string(term, &org_search_string) { + match highlight_search_string( + term, + &org_search_string, + string_style, + ) { Ok(s) => s, - Err(_) => term.to_string(), + Err(_) => { + string_style.paint(term.to_string()).to_string() + } } } else { - term.clone() + string_style.paint(term.to_string()).to_string() } }) .collect::>() @@ -330,7 +344,11 @@ You can also learn more at https://www.nushell.sh/book/"#; } // Highlight the search string using ANSI escape sequences and regular expressions. -fn highlight_search_string(haystack: &str, needle: &str) -> Result { +fn highlight_search_string( + haystack: &str, + needle: &str, + string_style: &Style, +) -> Result { let regex_string = format!("(?i){}", needle); let regex = match regex::Regex::new(®ex_string) { Ok(regex) => regex, @@ -357,11 +375,15 @@ fn highlight_search_string(haystack: &str, needle: &str) -> Result cap.end(), None => 0, }; - highlighted.push_str(&haystack[last_match_end..start]); + highlighted.push_str( + &string_style + .paint(&haystack[last_match_end..start]) + .to_string(), + ); highlighted.push_str(&style.paint(&haystack[start..end]).to_string()); last_match_end = end; } - highlighted.push_str(&haystack[last_match_end..]); + highlighted.push_str(&string_style.paint(&haystack[last_match_end..]).to_string()); Ok(highlighted) }