mirror of
https://github.com/nachoparker/dutree
synced 2024-05-27 02:16:08 +02:00
fix truncate unicode grapheme clusters
This commit is contained in:
parent
4128d49e16
commit
1f7436781e
|
@ -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)",
|
||||
]
|
||||
|
||||
|
@ -92,6 +93,11 @@ name = "ucd-util"
|
|||
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"
|
||||
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
|
21
src/lib.rs
21
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 length = UnicodeWidthStr::width(entry.name.as_str());
|
||||
|
||||
let mut name = entry.name.clone();
|
||||
name.truncate( name_width );
|
||||
// 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 {
|
||||
|
|
Loading…
Reference in New Issue