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