diff options
Diffstat (limited to 'utils/src')
| -rw-r--r-- | utils/src/display.rs | 1 | ||||
| -rw-r--r-- | utils/src/display/str_width.rs | 18 | ||||
| -rw-r--r-- | utils/src/display/table.rs | 21 |
3 files changed, 21 insertions, 19 deletions
diff --git a/utils/src/display.rs b/utils/src/display.rs index 5b452ce..c2ffeea 100644 --- a/utils/src/display.rs +++ b/utils/src/display.rs @@ -1,3 +1,4 @@ pub mod markdown; pub mod pager; +pub mod str_width; pub mod table; diff --git a/utils/src/display/str_width.rs b/utils/src/display/str_width.rs new file mode 100644 index 0000000..a725bdb --- /dev/null +++ b/utils/src/display/str_width.rs @@ -0,0 +1,18 @@ +pub fn display_width(s: &str) -> usize { + // Filter out ANSI escape sequences before calculating width + let filtered_bytes = strip_ansi_escapes::strip(s); + let filtered_str = match std::str::from_utf8(&filtered_bytes) { + Ok(s) => s, + Err(_) => s, // Fallback to original string if UTF-8 conversion fails + }; + + let mut width = 0; + for c in filtered_str.chars() { + if c.is_ascii() { + width += 1; + } else { + width += 2; + } + } + width +} diff --git a/utils/src/display/table.rs b/utils/src/display/table.rs index ae745d8..e6f1aca 100644 --- a/utils/src/display/table.rs +++ b/utils/src/display/table.rs @@ -1,3 +1,5 @@ +use crate::display::str_width::display_width; + pub struct Table { items: Vec<String>, line: Vec<Vec<String>>, @@ -122,22 +124,3 @@ impl std::fmt::Display for Table { Ok(()) } } - -pub fn display_width(s: &str) -> usize { - // Filter out ANSI escape sequences before calculating width - let filtered_bytes = strip_ansi_escapes::strip(s); - let filtered_str = match std::str::from_utf8(&filtered_bytes) { - Ok(s) => s, - Err(_) => s, // Fallback to original string if UTF-8 conversion fails - }; - - let mut width = 0; - for c in filtered_str.chars() { - if c.is_ascii() { - width += 1; - } else { - width += 2; - } - } - width -} |
