From 890000bd0e53259f325602dc91ca3a32467a429f Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Sun, 2 Aug 2020 00:48:44 +0300 Subject: [PATCH] status page: trim extension name at 30 chars NNTP has some long protocol extension names --- melib/src/text_processing/mod.rs | 58 ++++++++++++++++++++++++++++++-- src/components/mail/status.rs | 8 +++-- 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/melib/src/text_processing/mod.rs b/melib/src/text_processing/mod.rs index 0c2e3de2..ac2e26f6 100644 --- a/melib/src/text_processing/mod.rs +++ b/melib/src/text_processing/mod.rs @@ -31,11 +31,46 @@ pub use line_break::*; pub use wcwidth::*; pub trait Truncate { - fn truncate_at_boundary(self, new_len: usize); + fn truncate_at_boundary(&mut self, new_len: usize); + fn trim_at_boundary(&self, new_len: usize) -> &str; } -impl Truncate for &mut String { - fn truncate_at_boundary(self, new_len: usize) { +impl Truncate for &str { + fn truncate_at_boundary(&mut self, new_len: usize) { + if new_len >= self.len() { + return; + } + + extern crate unicode_segmentation; + use unicode_segmentation::UnicodeSegmentation; + if let Some((last, _)) = UnicodeSegmentation::grapheme_indices(*self, true) + .take(new_len) + .last() + { + *self = &self[..last]; + } + } + + fn trim_at_boundary(&self, new_len: usize) -> &str { + if new_len >= self.len() { + return self; + } + + extern crate unicode_segmentation; + use unicode_segmentation::UnicodeSegmentation; + if let Some((last, _)) = UnicodeSegmentation::grapheme_indices(*self, true) + .take(new_len) + .last() + { + &self[..last] + } else { + self + } + } +} + +impl Truncate for String { + fn truncate_at_boundary(&mut self, new_len: usize) { if new_len >= self.len() { return; } @@ -49,6 +84,23 @@ impl Truncate for &mut String { String::truncate(self, last); } } + + fn trim_at_boundary(&self, new_len: usize) -> &str { + if new_len >= self.len() { + return self; + } + + extern crate unicode_segmentation; + use unicode_segmentation::UnicodeSegmentation; + if let Some((last, _)) = UnicodeSegmentation::grapheme_indices(self.as_str(), true) + .take(new_len) + .last() + { + &self[..last] + } else { + self.as_str() + } + } } pub trait GlobMatch { diff --git a/src/components/mail/status.rs b/src/components/mail/status.rs index dfe3840d..d29e4368 100644 --- a/src/components/mail/status.rs +++ b/src/components/mail/status.rs @@ -548,7 +548,11 @@ impl Component for AccountStatus { ); let max_name_width = std::cmp::max( "Server Extensions:".len(), - extensions.iter().map(|(n, _)| n.len()).max().unwrap_or(0), + extensions + .iter() + .map(|(n, _)| std::cmp::min(30, n.len())) + .max() + .unwrap_or(0), ); write_string_to_grid( "meli support:", @@ -563,7 +567,7 @@ impl Component for AccountStatus { for (i, (name, status)) in extensions.into_iter().enumerate() { let (width, height) = self.content.size(); write_string_to_grid( - &name, + name.trim_at_boundary(30), &mut self.content, self.theme_default.fg, self.theme_default.bg,