From be0241bab13a93afde783687b9e66fd05a7bfc14 Mon Sep 17 00:00:00 2001 From: Petros Amoiridis Date: Tue, 7 Feb 2023 19:25:07 +0200 Subject: [PATCH 1/3] Add test for string with unicode characters --- crates/util/src/lib.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/util/src/lib.rs b/crates/util/src/lib.rs index 8cdbfc6438023f38b76b168a10ab95652590b689..0e270db906c18d3cef6eb4aaf18435f79a1921bb 100644 --- a/crates/util/src/lib.rs +++ b/crates/util/src/lib.rs @@ -276,4 +276,15 @@ mod tests { assert_eq!(foo, None); } + + #[test] + fn test_trancate_and_trailoff() { + const MAX_CHARS: usize = 24; + assert_eq!( + truncate_and_trailoff("ajouter un compte d'èèèès", MAX_CHARS), + "ajouter un compte d'è…" + ); + assert_eq!(truncate_and_trailoff("ajouter", MAX_CHARS), "ajouter"); + assert_eq!(truncate_and_trailoff("", MAX_CHARS), ""); + } } From 9bff82f16179b2efc4ada581f30e91374886fb46 Mon Sep 17 00:00:00 2001 From: Petros Amoiridis Date: Tue, 7 Feb 2023 19:25:57 +0200 Subject: [PATCH 2/3] Use truncate_and_trailoff function A function that already works with unicode characters. --- crates/search/src/project_search.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs index d3d5c437c5e6f6675d30ebf501322afd96d5d5d2..a4a7a520ae9c3307b68b613a805febbe0c0f081a 100644 --- a/crates/search/src/project_search.rs +++ b/crates/search/src/project_search.rs @@ -259,11 +259,7 @@ impl Item for ProjectSearchView { .boxed(), ) .with_children(self.model.read(cx).active_query.as_ref().map(|query| { - let query_text = if query.as_str().len() > MAX_TAB_TITLE_LEN { - query.as_str()[..MAX_TAB_TITLE_LEN].to_string() + "…" - } else { - query.as_str().to_string() - }; + let query_text = util::truncate_and_trailoff(query.as_str(), MAX_TAB_TITLE_LEN); Label::new(query_text, tab_theme.label.clone()) .aligned() From e15ffc85602ea51a3169cf5861bcf7d52db75079 Mon Sep 17 00:00:00 2001 From: Petros Amoiridis Date: Tue, 7 Feb 2023 20:06:20 +0200 Subject: [PATCH 3/3] Make truncate_and_trailoff a bit more clear Co-Authored-By: Max Brunsfeld --- crates/util/src/lib.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/crates/util/src/lib.rs b/crates/util/src/lib.rs index 0e270db906c18d3cef6eb4aaf18435f79a1921bb..ea8fdee2a8c5ded508581f7e7591e73f01ad4718 100644 --- a/crates/util/src/lib.rs +++ b/crates/util/src/lib.rs @@ -46,10 +46,10 @@ pub fn truncate(s: &str, max_chars: usize) -> &str { pub fn truncate_and_trailoff(s: &str, max_chars: usize) -> String { debug_assert!(max_chars >= 5); - if s.len() > max_chars { - format!("{}…", truncate(s, max_chars.saturating_sub(3))) - } else { - s.to_string() + let truncation_ix = s.char_indices().map(|(i, _)| i).nth(max_chars); + match truncation_ix { + Some(length) => s[..length].to_string() + "…", + None => s.to_string(), } } @@ -279,12 +279,9 @@ mod tests { #[test] fn test_trancate_and_trailoff() { - const MAX_CHARS: usize = 24; - assert_eq!( - truncate_and_trailoff("ajouter un compte d'èèèès", MAX_CHARS), - "ajouter un compte d'è…" - ); - assert_eq!(truncate_and_trailoff("ajouter", MAX_CHARS), "ajouter"); - assert_eq!(truncate_and_trailoff("", MAX_CHARS), ""); + assert_eq!(truncate_and_trailoff("", 5), ""); + assert_eq!(truncate_and_trailoff("èèèèèè", 7), "èèèèèè"); + assert_eq!(truncate_and_trailoff("èèèèèè", 6), "èèèèèè"); + assert_eq!(truncate_and_trailoff("èèèèèè", 5), "èèèèè…"); } }