From 9418b5574597b741c5ed5ccfcb1f4ce4c4750196 Mon Sep 17 00:00:00 2001 From: jackyzy823 Date: Mon, 25 Aug 2025 06:11:47 +0000 Subject: [PATCH] Fix width calculation problem due to unicode-width update --- Cargo.toml | 2 +- src/lib.rs | 4 ++-- src/utils.rs | 8 ++++++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 65fd38eab..73b5771d4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,7 @@ path = "src/main.rs" name = "prettytable" [dependencies] -unicode-width = "0.1" +unicode-width = "0.2" term = "0.7" lazy_static = "1.4" is-terminal = "0.4" diff --git a/src/lib.rs b/src/lib.rs index e4fdb78d4..d2c200014 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1314,9 +1314,9 @@ mod tests { table.add_row(Row::new(vec![Cell::new("\u{1b}[\u{1b}\u{0}\u{0}")])); - let out = "+--+ + let out = "+-----+ | \u{1b}[\u{1b}\u{0}\u{0} | -+--+ ++-----+ "; assert_eq!(table.to_string().replace("\r\n", "\n"), out); diff --git a/src/utils.rs b/src/utils.rs index 1256ced7b..f2cac6859 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -109,8 +109,12 @@ pub fn display_width(text: &str) -> usize { _ => state, }; - // We don't count escape characters as hidden as - // UnicodeWidthStr::width already considers them. + // We should count escape characters as hidden because + // UnicodeWidthStr::width treats escape characters as 1 column wide. + // However UnicodeWidthChar::width treats escape characters as `None`. + if state == State::EscapeChar { + hidden += 1; + } if matches!(state, State::OpenBracket | State::AfterEscape) { // but if we see an escape char *inside* the ANSI escape, we should ignore it. if UnicodeWidthChar::width(c).unwrap_or(0) > 0 {