From 42746ffdc32db3f6918c1038f899f9d00a32997a Mon Sep 17 00:00:00 2001 From: Gerrit Viljoen Date: Sat, 16 May 2020 22:10:54 +0200 Subject: [PATCH] save game --- Cargo.toml | 3 --- src/lib.rs | 19 ++++++++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 96ab8a9..ed8ad55 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,5 @@ readme = "readme.md" categories = ["command-line-interface"] keywords = ["ascii", "table"] -[dependencies] -regex = "1" - [dev-dependencies] colorful = "0.2" diff --git a/src/lib.rs b/src/lib.rs index c5da011..28c3455 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -233,7 +233,24 @@ impl AsciiTable { } fn count_characters(&self, cell: &str) -> usize { - cell.chars().count() + let mut count = 0; + let mut block = false; + let mut iter = cell.chars().peekable(); + while let Some(ch) = iter.next() { + if block { + if ch != '\u{1b}' && ch != '[' && ch != ';' && ch != 'm' && !('0'..'9').contains(&ch) { + block = false; + count += 1; + } + } else { + if ch == '\u{1b}' && Some(&'[') == iter.peek() { + block = true; + } else { + count += 1; + } + } + } + count } fn truncate_widths(&self, mut widths: Vec) -> Vec {