From cc8b6b52239203309691abf3f8aaf9cabeda7389 Mon Sep 17 00:00:00 2001 From: Gerrit Viljoen Date: Sun, 17 May 2020 12:08:36 +0200 Subject: [PATCH] save game --- src/lib.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index a122d67..cfc5097 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -364,7 +364,34 @@ impl SmartString { // } // } // count - Self { fragments: vec![(true, string.to_string())] } + let string = string.to_string(); + let mut fragments = Vec::new(); + let mut visible = true; + let mut buf = String::new(); + let mut iter = string.chars().peekable(); + + while let Some(ch) = iter.next() { + if visible { + if ch == '\u{1b}' && Some(&'[') == iter.peek() { + fragments.push((visible, buf)); + visible = !visible; + buf = String::new(); + } + buf.push(ch); + } else { + if ch != '\u{1b}' && ch != '[' && ch != ';' && ch != 'm' && !('0'..'9').contains(&ch) { + fragments.push((visible, buf)); + visible = !visible; + buf = String::new(); + } + buf.push(ch); + } + } + if !buf.is_empty() { + fragments.push((visible, buf)); + } + + Self { fragments } } fn char_len(&self) -> usize {