diff options
author | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2020-11-24 09:31:38 +0200 |
---|---|---|
committer | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2020-11-24 10:36:21 +0200 |
commit | d4f508642ae21f4b18b40d7a3a2b83a27655e979 (patch) | |
tree | 5b13d26c6fc85199eb5666e03e2a66f1b42fe427 /melib | |
parent | f69f623818f21bbba97be70dad9dcf309296c5ef (diff) |
widgets: allow text overflow in text fields
Show text content of a text field that exceeds the visible width
properly.
Diffstat (limited to 'melib')
-rw-r--r-- | melib/src/text_processing/mod.rs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/melib/src/text_processing/mod.rs b/melib/src/text_processing/mod.rs index ac2e26f6..2db2d859 100644 --- a/melib/src/text_processing/mod.rs +++ b/melib/src/text_processing/mod.rs @@ -33,6 +33,8 @@ pub use wcwidth::*; pub trait Truncate { fn truncate_at_boundary(&mut self, new_len: usize); fn trim_at_boundary(&self, new_len: usize) -> &str; + fn trim_left_at_boundary(&self, new_len: usize) -> &str; + fn truncate_left_at_boundary(&mut self, new_len: usize); } impl Truncate for &str { @@ -67,6 +69,39 @@ impl Truncate for &str { self } } + + fn trim_left_at_boundary(&self, skip_len: usize) -> &str { + if skip_len >= self.len() { + return ""; + } + + extern crate unicode_segmentation; + use unicode_segmentation::UnicodeSegmentation; + if let Some((first, _)) = UnicodeSegmentation::grapheme_indices(*self, true) + .skip(skip_len) + .next() + { + &self[first..] + } else { + self + } + } + + fn truncate_left_at_boundary(&mut self, skip_len: usize) { + if skip_len >= self.len() { + *self = ""; + return; + } + + extern crate unicode_segmentation; + use unicode_segmentation::UnicodeSegmentation; + if let Some((first, _)) = UnicodeSegmentation::grapheme_indices(*self, true) + .skip(skip_len) + .next() + { + *self = &self[first..]; + } + } } impl Truncate for String { @@ -101,6 +136,39 @@ impl Truncate for String { self.as_str() } } + + fn trim_left_at_boundary(&self, skip_len: usize) -> &str { + if skip_len >= self.len() { + return ""; + } + + extern crate unicode_segmentation; + use unicode_segmentation::UnicodeSegmentation; + if let Some((first, _)) = UnicodeSegmentation::grapheme_indices(self.as_str(), true) + .skip(skip_len) + .next() + { + &self[first..] + } else { + self.as_str() + } + } + + fn truncate_left_at_boundary(&mut self, skip_len: usize) { + if skip_len >= self.len() { + self.clear(); + return; + } + + extern crate unicode_segmentation; + use unicode_segmentation::UnicodeSegmentation; + if let Some((first, _)) = UnicodeSegmentation::grapheme_indices(self.as_str(), true) + .skip(skip_len) + .next() + { + *self = self[first..].to_string(); + } + } } pub trait GlobMatch { |