From 2e23d4d7341ccda2edcd738ae9b055ddf3c3ce3c Mon Sep 17 00:00:00 2001 From: Tobias <76444201+Fl1tzi@users.noreply.github.com> Date: Fri, 23 Sep 2022 14:11:33 +0200 Subject: [PATCH] fix issue 6602 (broken highlighting in find) (#6604) * fix issue 6602 (broken highlighting in find) Find uses highlight_search_string to see where the string was found. The problem was that the style would just "append" to the existing haystack (including the ANSI escape codes of LS_COLORS). This first strips the ANSI escape codes out of the haystack before formatting the output string. * update formatting --- crates/nu-command/src/core_commands/help.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/crates/nu-command/src/core_commands/help.rs b/crates/nu-command/src/core_commands/help.rs index e8d2a2eb4..962884e1f 100644 --- a/crates/nu-command/src/core_commands/help.rs +++ b/crates/nu-command/src/core_commands/help.rs @@ -362,11 +362,16 @@ pub fn highlight_search_string( )); } }; + // strip haystack to remove existing ansi style + let stripped_haystack: String = match strip_ansi_escapes::strip(haystack) { + Ok(i) => String::from_utf8(i).unwrap_or_else(|_| String::from(haystack)), + Err(_) => String::from(haystack), + }; let mut last_match_end = 0; let style = Style::new().fg(White).on(Red); let mut highlighted = String::new(); - for cap in regex.captures_iter(haystack) { + for cap in regex.captures_iter(stripped_haystack.as_str()) { match cap { Ok(capture) => { let start = match capture.get(0) { @@ -379,10 +384,10 @@ pub fn highlight_search_string( }; highlighted.push_str( &string_style - .paint(&haystack[last_match_end..start]) + .paint(&stripped_haystack[last_match_end..start]) .to_string(), ); - highlighted.push_str(&style.paint(&haystack[start..end]).to_string()); + highlighted.push_str(&style.paint(&stripped_haystack[start..end]).to_string()); last_match_end = end; } Err(e) => { @@ -397,6 +402,10 @@ pub fn highlight_search_string( } } - highlighted.push_str(&string_style.paint(&haystack[last_match_end..]).to_string()); + highlighted.push_str( + &string_style + .paint(&stripped_haystack[last_match_end..]) + .to_string(), + ); Ok(highlighted) }