diff options
author | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2019-03-25 13:22:45 +0200 |
---|---|---|
committer | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2019-06-10 19:40:40 +0300 |
commit | 4c26077f30571a69d93b05447e6f765e15bf46e2 (patch) | |
tree | a9a2ece239188d5e606da78d29da463924886d2e /ui/src/terminal/grapheme_clusters.rs | |
parent | 9522508a924955768b13cdd996cd52a869a45105 (diff) |
ui: word break with grapheme length, not bytes
#69 East characters are not working.
Diffstat (limited to 'ui/src/terminal/grapheme_clusters.rs')
-rw-r--r-- | ui/src/terminal/grapheme_clusters.rs | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/ui/src/terminal/grapheme_clusters.rs b/ui/src/terminal/grapheme_clusters.rs index 4e661c9b..2135ce8f 100644 --- a/ui/src/terminal/grapheme_clusters.rs +++ b/ui/src/terminal/grapheme_clusters.rs @@ -8,34 +8,42 @@ */ -use super::wcwidth::{wcwidth, CodePointsIter}; use super::*; -pub fn split_graphemes(s: &str) -> Vec<&str> { - UnicodeSegmentation::graphemes(s, true).collect::<Vec<&str>>() -} -pub fn next_grapheme(s: &str) -> Option<(usize, &str)> { - UnicodeSegmentation::grapheme_indices(s, true).next() -} +pub trait Graphemes: UnicodeSegmentation + CodePointsIter { + fn split_graphemes<'a>(&'a self) -> Vec<&'a str> { + UnicodeSegmentation::graphemes(self, true).collect::<Vec<&str>>() + } -pub fn last_grapheme(s: &str) -> Option<(usize, &str)> { - UnicodeSegmentation::grapheme_indices(s, true).next_back() -} + fn graphemes_indices<'a>(&'a self) -> Vec<(usize, &'a str)> { + UnicodeSegmentation::grapheme_indices(self, true).collect::<Vec<(usize, &str)>>() + } -pub fn grapheme_width(grapheme: &str) -> i32 { - let mut count = 0; - for c in grapheme.code_points() { - count += if let Some(c) = wcwidth(c) { - c as i32 - } else { - -1 - }; + fn next_grapheme<'a>(&'a self) -> Option<(usize, &'a str)> { + UnicodeSegmentation::grapheme_indices(self, true).next() } - count + fn last_grapheme<'a>(&'a self) -> Option<(usize, &'a str)> { + UnicodeSegmentation::grapheme_indices(self, true).next_back() + } + + fn grapheme_width(&self) -> i32 { + let mut count = 0; + for c in self.code_points() { + count += if let Some(c) = wcwidth(c) { + c as i32 + } else { + -1 + }; + } + + count + } } +impl Graphemes for str {} + //#[derive(PartialEq)] //enum Property { // CR, |