From 2f57befcd2af631301b633af561c7404414f6644 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Sun, 28 Mar 2021 15:03:18 +0200 Subject: [PATCH] actually, use both --- Cargo.toml | 1 + src/lib.rs | 4 +++- src/test.rs | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8d1c199..7e9247f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,4 +17,5 @@ keywords = ["ascii", "table"] colorful = "0.2" [dependencies] +unicode-segmentation = "1.7.1" unicode-width = "0.1.8" diff --git a/src/lib.rs b/src/lib.rs index f4c3f04..14f18ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,6 +58,7 @@ mod test; use std::collections::BTreeMap; use std::fmt::Display; +use unicode_segmentation::UnicodeSegmentation; use unicode_width::UnicodeWidthStr; const SE: &str = "┌"; @@ -398,7 +399,8 @@ impl SmartString { fn char_len(&self) -> usize { self.fragments.iter() .filter(|(visible, _)| *visible) - .map(|(_, string)| UnicodeWidthStr::width(string.as_str())) + .map(|(_, string)| UnicodeSegmentation::graphemes(string.as_str(), true) + .map(|x| if UnicodeWidthStr::width(x) > 1 { 2 } else { 1 }).sum::()) .sum() } diff --git a/src/test.rs b/src/test.rs index 550b9bb..d1860fc 100644 --- a/src/test.rs +++ b/src/test.rs @@ -335,12 +335,12 @@ fn show_one_character_for_header2() { #[test] fn cube_with_partial_content() { let config = cube_config(); - let input: Vec<&[&str]> = vec![&["1", "2", "😞"], &["4", "5"], &["7"]]; + let input: Vec<&[&str]> = vec![&["1", "2", "😞"], &["4", "5", "👩‍🔬"], &["7"]]; let expected = "┌───┬───┬────┐\n\ │ a │ b │ c │\n\ ├───┼───┼────┤\n\ │ 1 │ 2 │ 😞 │\n\ - │ 4 │ 5 │ │\n\ + │ 4 │ 5 │ 👩‍🔬 │\n\ │ 7 │ │ │\n\ └───┴───┴────┘\n";