MCPcopy Create free account
hub / github.com/ccusage/ccusage / visible_width

Function visible_width

rust/crates/ccusage-terminal/src/width.rs:3–26  ·  view source on GitHub ↗
(value: &str)

Source from the content-addressed store, hash-verified

1use unicode_width::UnicodeWidthChar;
2
3pub(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
28pub(crate) fn contains_ansi(value: &str) -> bool {
29 value.as_bytes().contains(&0x1b)

Callers 5

box_title_linesFunction · 0.85
wrap_cell_linesFunction · 0.85
wrap_cell_lineFunction · 0.85
truncate_visibleFunction · 0.85
pad_cellFunction · 0.85

Calls 3

char_display_widthFunction · 0.85
lenMethod · 0.80
nextMethod · 0.45

Tested by

no test coverage detected