From 73cc36ce71b49c1e4966094f92410b237d4109fb Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Sun, 28 Mar 2021 14:29:33 +0200 Subject: [PATCH] Measure text width using Unicode segmentation --- Cargo.toml | 5 ++++- src/lib.rs | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 50372c9..9256a47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ascii_table" -version = "3.0.2" +version = "4.0.0-alpha" authors = ["Gerrit Viljoen "] license = "MIT" edition = "2018" @@ -15,3 +15,6 @@ keywords = ["ascii", "table"] [dev-dependencies] colorful = "0.2" + +[dependencies] +unicode-segmentation = "1.7.1" diff --git a/src/lib.rs b/src/lib.rs index 10abcf3..93276d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,6 +58,8 @@ mod test; use std::collections::BTreeMap; use std::fmt::Display; +use unicode_segmentation::UnicodeSegmentation; + const SE: &str = "┌"; const NW: &str = "┘"; const SW: &str = "┐"; @@ -396,7 +398,7 @@ impl SmartString { fn char_len(&self) -> usize { self.fragments.iter() .filter(|(visible, _)| *visible) - .map(|(_, string)| string.chars().count()) + .map(|(_, string)| UnicodeSegmentation::graphemes(string.as_str(), true).count()) .sum() }