diff options
author | nacho <nacho.nunez@aoifes.com> | 2018-04-18 11:31:24 +0200 |
---|---|---|
committer | nacho <nacho.nunez@aoifes.com> | 2018-04-18 11:31:24 +0200 |
commit | 1f7436781ef518a5567de690026fde0d44989cef (patch) | |
tree | eed302dff8c9529f0b51f8b6896d8fbc10b15b03 /src/lib.rs | |
parent | 4128d49e16cb179cae05de2c176b1fa9117b1fb5 (diff) |
fix truncate unicode grapheme clusters
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 23 |
1 files changed, 19 insertions, 4 deletions
@@ -35,6 +35,10 @@ //! extern crate unicode_width; +use unicode_width::UnicodeWidthStr; + +extern crate unicode_segmentation; +use unicode_segmentation::UnicodeSegmentation; extern crate getopts; use getopts::Options; @@ -324,10 +328,21 @@ impl<'a> Entry<'a> { let tree_width = (open_parents.len() + 1) * 3; // 3 chars per tree branch if tree_name_width >= tree_width { let name_width = tree_name_width - tree_width; - let length = unicode_width::UnicodeWidthStr::width(entry.name.as_str()); - - let mut name = entry.name.clone(); - name.truncate( name_width ); + let length = UnicodeWidthStr::width(entry.name.as_str()); + + // truncate Unicode string to name_width + let graphemes = UnicodeSegmentation::graphemes( entry.name.as_str(), true ); + let mut i = 0; + let mut vec = Vec::new(); + for cluster in graphemes { + let w = UnicodeWidthStr::width( cluster ); + if i + w <= name_width { + i += w; + vec.push( cluster ); + } + } + let mut name = String::new(); + vec.iter().for_each( |cluster| name.push_str( cluster ) ); // surround name by ANSII color escape sequences if let Some( ref col_str ) = entry.color { |