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
This commit is contained in:
Tobias 2022-09-23 14:11:33 +02:00 committed by GitHub
parent d9d14b38de
commit 2e23d4d734
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 mut last_match_end = 0;
let style = Style::new().fg(White).on(Red); let style = Style::new().fg(White).on(Red);
let mut highlighted = String::new(); let mut highlighted = String::new();
for cap in regex.captures_iter(haystack) { for cap in regex.captures_iter(stripped_haystack.as_str()) {
match cap { match cap {
Ok(capture) => { Ok(capture) => {
let start = match capture.get(0) { let start = match capture.get(0) {
@ -379,10 +384,10 @@ pub fn highlight_search_string(
}; };
highlighted.push_str( highlighted.push_str(
&string_style &string_style
.paint(&haystack[last_match_end..start]) .paint(&stripped_haystack[last_match_end..start])
.to_string(), .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; last_match_end = end;
} }
Err(e) => { 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) Ok(highlighted)
} }