(value: &str)
| 1 | use unicode_width::UnicodeWidthChar; |
| 2 | |
| 3 | pub(crate) fn visible_width(value: &str) -> usize { |
| 4 | let bytes = value.as_bytes(); |
| 5 | let mut width = 0; |
| 6 | let mut index = 0; |
| 7 | while index < bytes.len() { |
| 8 | if bytes[index] == 0x1b { |
| 9 | index += 1; |
| 10 | if index < bytes.len() && bytes[index] == b'[' { |
| 11 | index += 1; |
| 12 | while index < bytes.len() && !(bytes[index] as char).is_ascii_alphabetic() { |
| 13 | index += 1; |
| 14 | } |
| 15 | index += usize::from(index < bytes.len()); |
| 16 | } |
| 17 | continue; |
| 18 | } |
| 19 | let Some(ch) = value[index..].chars().next() else { |
| 20 | break; |
| 21 | }; |
| 22 | width += char_display_width(ch); |
| 23 | index += ch.len_utf8(); |
| 24 | } |
| 25 | width |
| 26 | } |
| 27 | |
| 28 | pub(crate) fn contains_ansi(value: &str) -> bool { |
| 29 | value.as_bytes().contains(&0x1b) |
no test coverage detected