perf: skip ratatui span when match at end of string (#91)

perf: skip ratatui span when match end of string

- for the entry name, we don't create an empty ratatui span when the
  match is right at the end of the entry name
- same for the preview, we don't create a ratatui span anymore when the
  match is at the end of the preview
This commit is contained in:
Bertrand Chardon 2024-12-02 16:18:52 +01:00 committed by GitHub
parent 20cf83b720
commit b7ddb00c4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -96,22 +96,29 @@ where
.iter()
.map(|(s, e)| (*s as usize, *e as usize))
{
// from the end of the last match to the start of the current one
spans.push(Span::styled(
slice_at_char_boundaries(&entry_name, last_match_end, start)
.to_string(),
Style::default().fg(results_list_colors.result_name_fg),
));
// the current match
spans.push(Span::styled(
slice_at_char_boundaries(&entry_name, start, end).to_string(),
Style::default().fg(Color::Red),
));
last_match_end = end;
}
// we need to push a span for the remainder of the entry name
// but only if there's something left
let next_boundary = next_char_boundary(&entry_name, last_match_end);
if next_boundary < entry_name.len() {
let remainder = entry_name[next_boundary..].to_string();
spans.push(Span::styled(
entry_name[next_char_boundary(&entry_name, last_match_end)..]
.to_string(),
remainder,
Style::default().fg(results_list_colors.result_name_fg),
));
}
// optional line number
if let Some(line_number) = entry.line_number {
spans.push(Span::styled(
@ -144,12 +151,14 @@ where
));
last_match_end = end;
}
let next_boundary = next_char_boundary(&preview, last_match_end);
if next_boundary < preview.len() {
spans.push(Span::styled(
preview[next_char_boundary(&preview, last_match_end)..]
.to_string(),
preview[next_boundary..].to_string(),
Style::default().fg(results_list_colors.result_preview_fg),
));
}
}
Line::from(spans)
}))
.direction(list_direction)