From b4450f9bc3a19216b19dd19fee5afdd082701487 Mon Sep 17 00:00:00 2001 From: UG <55311933+UUGTech@users.noreply.github.com> Date: Fri, 7 Jul 2023 04:15:08 +0900 Subject: [PATCH] fixed wrong fuzzy-find highlight in long str (#1731) * fixed wrong highlight in long str * support multibyte characters --- CHANGELOG.md | 1 + src/components/fuzzy_find_popup.rs | 58 +++++++++++++++++------------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b17b40c1..cc488145 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixes * fix commit dialog char count for multibyte characters ([#1726](https://github.com/extrawurst/gitui/issues/1726)) +* fix wrong hit highlighting in fuzzy find popup [[@UUGTech](https://github.com/UUGTech)] ([#1731](https://github.com/extrawurst/gitui/pull/1731)) ## [0.23.0] - 2022-06-19 diff --git a/src/components/fuzzy_find_popup.rs b/src/components/fuzzy_find_popup.rs index 89b1eb55..1235b7c3 100644 --- a/src/components/fuzzy_find_popup.rs +++ b/src/components/fuzzy_find_popup.rs @@ -21,6 +21,7 @@ use ratatui::{ Frame, }; use std::borrow::Cow; +use unicode_segmentation::UnicodeSegmentation; pub struct FuzzyFindPopup { queue: Queue, @@ -237,31 +238,38 @@ impl DrawableComponent for FuzzyFindPopup { let height = usize::from(chunks[1].height); let width = usize::from(chunks[1].width); - let items = self.filtered.iter().take(height).map( - |(idx, indicies)| { - let selected = self - .selected_index - .map_or(false, |index| index == *idx); - let full_text = trim_length_left( - &self.contents[*idx], - width, - ); - Line::from( - full_text - .char_indices() - .map(|(c_idx, c)| { - Span::styled( - Cow::from(c.to_string()), - self.theme.text( - selected, - indicies.contains(&c_idx), - ), - ) - }) - .collect::>(), - ) - }, - ); + let items = + self.filtered.iter().take(height).map( + |(idx, indicies)| { + let selected = self + .selected_index + .map_or(false, |index| index == *idx); + let full_text = trim_length_left( + &self.contents[*idx], + width, + ); + let trim_length = self.contents[*idx] + .graphemes(true) + .count() - full_text + .graphemes(true) + .count(); + Line::from( + full_text + .graphemes(true) + .enumerate() + .map(|(c_idx, c)| { + Span::styled( + Cow::from(c.to_string()), + self.theme.text( + selected, + indicies.contains(&(c_idx + trim_length)), + ), + ) + }) + .collect::>(), + ) + }, + ); ui::draw_list_block( f,