Skip to content

Commit 26f3a6f

Browse files
committed
fix(terminal): use standard unicode display widths
Use unicode-width standard width calculation for terminal table output so ambiguous-width characters are measured like non-CJK terminal columns while CJK and combining marks still render with their expected widths. Simplify the compact-date minimum width condition to keep the behavior explicit: preserve a 12-column date only when compact dates are enabled and the table already fits, otherwise allow the 10-column compact form. Add direct width cases for ASCII, CJK, combining marks, controls, and ambiguous characters.
1 parent c66eb41 commit 26f3a6f

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

‎rust/crates/ccusage-terminal/src/table.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ impl SimpleTable {
139139
})
140140
.collect::<Vec<_>>();
141141
let total_required = cli_table_required_width(&widths);
142-
let first_column_min = if self.compact_dates && total_required > self.terminal_width {
143-
10
144-
} else if self.compact_dates {
142+
let first_column_min = if self.compact_dates && total_required <= self.terminal_width {
145143
12
146144
} else {
147145
10

‎rust/crates/ccusage-terminal/src/width.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub(crate) fn contains_ansi(value: &str) -> bool {
3030
}
3131

3232
pub(crate) fn char_display_width(ch: char) -> usize {
33-
UnicodeWidthChar::width_cjk(ch).unwrap_or(0)
33+
UnicodeWidthChar::width(ch).unwrap_or(0)
3434
}
3535

3636
pub(crate) fn visible_width_max_line(value: &str) -> usize {
@@ -50,4 +50,13 @@ mod tests {
5050
assert_eq!(visible_width("e\u{0301}"), 1);
5151
assert_eq!(visible_width("表"), 2);
5252
}
53+
54+
#[test]
55+
fn char_display_width_handles_standard_width_cases() {
56+
assert_eq!(char_display_width('a'), 1);
57+
assert_eq!(char_display_width('表'), 2);
58+
assert_eq!(char_display_width('\u{0301}'), 0);
59+
assert_eq!(char_display_width('\x07'), 0);
60+
assert_eq!(char_display_width('±'), 1);
61+
}
5362
}

0 commit comments

Comments
 (0)