summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAbin Simon <abinsimon10@gmail.com>2019-03-12 11:50:57 +0530
committerPierre Peltier <dev@halium.fr>2019-03-12 12:01:19 +0100
commit3477b6e723d48ec81f4cd5f04e1103c14ed5dcad (patch)
treed83f496fdab24d0b4a59ac0dc0e9818973530a4c
parentc6dcf2ed0f1b2d36912b476da4c1c1d774999de6 (diff)
refactor how width is computed for colored strings
-rw-r--r--src/display.rs16
1 files changed, 5 insertions, 11 deletions
diff --git a/src/display.rs b/src/display.rs
index fab14e0..b6603aa 100644
--- a/src/display.rs
+++ b/src/display.rs
@@ -278,18 +278,12 @@ fn get_long_output(
fn get_visible_width(input: &str) -> usize {
let mut nb_invisible_char = 0;
- // Search for every instance of "\e[" indicating the start of a color code
- // then count the number of charactere between the string and the first 'm'
- // charactere indicating the end of the code color.
- let matches: Vec<_> = input.match_indices("\u{1b}[").collect();
- if matches.len() > 0 {
- for c in input.chars().skip(matches[0].0) {
- nb_invisible_char += 1;
- if c == 'm' {
- break;
- }
+ // If the input has color, do not compute the length contributed by the color to the actual length
+ if input.starts_with("\u{1b}[") {
+ let m_pos = input.find('m');
+ if let Some(len) = m_pos {
+ nb_invisible_char = len + 3 // 1 (index -> length) + 2 ( compensate for color reset chars )
}
- nb_invisible_char += 2;
}
UnicodeWidthStr::width(input) - nb_invisible_char