summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornacho <nacho.nunez@aoifes.com>2018-04-18 11:31:24 +0200
committernacho <nacho.nunez@aoifes.com>2018-04-18 11:31:24 +0200
commit1f7436781ef518a5567de690026fde0d44989cef (patch)
treeeed302dff8c9529f0b51f8b6896d8fbc10b15b03
parent4128d49e16cb179cae05de2c176b1fa9117b1fb5 (diff)
fix truncate unicode grapheme clusters
-rw-r--r--Cargo.lock7
-rw-r--r--Cargo.toml3
-rw-r--r--src/lib.rs23
3 files changed, 28 insertions, 5 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 2fed4cc..b9a4a6c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -13,6 +13,7 @@ dependencies = [
"getopts 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
"terminal_size 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -93,6 +94,11 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "unicode-segmentation"
+version = "1.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
name = "unicode-width"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -137,6 +143,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum terminal_size 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "ef4f7fdb2a063032d361d9a72539380900bc3e0cd9ffc9ca8b677f8c855bae0f"
"checksum thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "279ef31c19ededf577bfd12dfae728040a21f635b06a24cd670ff510edd38963"
"checksum ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd2be2d6639d0f8fe6cdda291ad456e23629558d466e2789d2c3e9892bda285d"
+"checksum unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a8083c594e02b8ae1654ae26f0ade5158b119bd88ad0e8227a5d8fcd72407946"
"checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f"
"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"
"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"
diff --git a/Cargo.toml b/Cargo.toml
index 6dc6d4e..e7aecea 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "dutree"
-version = "0.2.4"
+version = "0.2.5"
authors = ["nacho <nacho@ownyourbits.com>"]
description = "Command line tool to analyze disk usage"
repository = "https://github.com/nachoparker/dutree"
@@ -14,3 +14,4 @@ getopts = "0.2"
terminal_size = "0.1.7"
regex = "0.2"
unicode-width = "0.1.1"
+unicode-segmentation = "1.2.0"
diff --git a/src/lib.rs b/src/lib.rs
index 4915502..b1fcae7 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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 {