Skip to content

Commit e73c9b9

Browse files
committed
fix(terminal): relax text minima before numeric widths
When measured numeric content plus the Date and Models fallback minima needs one more column than the terminal provides, the fallback scaler can truncate numeric cells even though the table can fit. Allow wrap-capable text columns to give back space down to the established text floor before falling back, so numeric cells remain intact at the boundary.
1 parent e7eb1c9 commit e73c9b9

1 file changed

Lines changed: 93 additions & 1 deletion

File tree

  • rust/crates/ccusage-terminal/src

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

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
};
88

99
const MAX_MODELS_CONTENT_WIDTH: usize = 25;
10+
const MIN_WRAP_COLUMN_WIDTH: usize = 8;
1011

1112
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1213
pub enum Align {
@@ -252,7 +253,7 @@ fn fit_widths_to_terminal(
252253
})
253254
.collect::<Vec<_>>();
254255

255-
let content_minimums = fallback_minimums
256+
let mut content_minimums = fallback_minimums
256257
.iter()
257258
.enumerate()
258259
.map(|(index, minimum)| {
@@ -269,6 +270,20 @@ fn fit_widths_to_terminal(
269270
}
270271
})
271272
.collect::<Vec<_>>();
273+
while cli_table_required_width(&content_minimums) > terminal_width {
274+
let Some(index) = content_minimums
275+
.iter()
276+
.enumerate()
277+
.filter(|(index, width)| {
278+
aligns.get(*index) != Some(&Align::Right) && **width > MIN_WRAP_COLUMN_WIDTH
279+
})
280+
.max_by_key(|(_, width)| **width)
281+
.map(|(index, _)| index)
282+
else {
283+
break;
284+
};
285+
content_minimums[index] -= 1;
286+
}
272287
let natural_widths = widths.clone();
273288
let mut expansion_limits = natural_widths.clone();
274289
if compact_dates && let Some(first) = expansion_limits.first_mut() {
@@ -820,6 +835,83 @@ mod tests {
820835
assert!(total_line.contains("$123456.78"), "{total_line}");
821836
}
822837

838+
#[test]
839+
fn preserves_numeric_cells_at_the_codex_120_column_boundary() {
840+
let mut table = SimpleTable::new(
841+
vec![
842+
"Date",
843+
"Models",
844+
"Input",
845+
"Output",
846+
"Reasoning",
847+
"Cache Create",
848+
"Cache Read",
849+
"Total Tokens",
850+
"Cost (USD)",
851+
],
852+
vec![
853+
Align::Left,
854+
Align::Left,
855+
Align::Right,
856+
Align::Right,
857+
Align::Right,
858+
Align::Right,
859+
Align::Right,
860+
Align::Right,
861+
Align::Right,
862+
],
863+
TerminalStyle {
864+
no_color: true,
865+
..TerminalStyle::default()
866+
},
867+
)
868+
.with_terminal_width(120)
869+
.with_date_compaction(true);
870+
table.push(vec![
871+
"2026-05-18".to_string(),
872+
"- provider/this-is-a-deliberately-wide-model-name".to_string(),
873+
"123,456,789".to_string(),
874+
"1,234,567".to_string(),
875+
"9,876,543".to_string(),
876+
"1,234,567".to_string(),
877+
"12,345,678,901".to_string(),
878+
"12,345,678,901".to_string(),
879+
"$12345.67".to_string(),
880+
]);
881+
table.separator();
882+
table.push(vec![
883+
"Total".to_string(),
884+
String::new(),
885+
"123,456,789".to_string(),
886+
"1,234,567".to_string(),
887+
"9,876,543".to_string(),
888+
"1,234,567".to_string(),
889+
"12,345,678,901".to_string(),
890+
"12,345,678,901".to_string(),
891+
"$12345.67".to_string(),
892+
]);
893+
894+
let widths = table.column_widths();
895+
assert_eq!(cli_table_required_width(&widths), 120);
896+
assert!(widths[0] < 10 || widths[1] < 12, "{widths:?}");
897+
assert!(widths[2] >= 13, "{widths:?}");
898+
assert!(widths[6] >= 16, "{widths:?}");
899+
assert!(widths[7] >= 16, "{widths:?}");
900+
901+
let rendered = table.render_lines().join("\n");
902+
let total_line = rendered
903+
.lines()
904+
.find(|line| line.starts_with("│ Total"))
905+
.expect("rendered table should include a Total row");
906+
assert!(!total_line.contains('…'), "{total_line}");
907+
for value in ["123,456,789", "12,345,678,901"] {
908+
assert!(
909+
total_line.contains(value),
910+
"missing {value} in {total_line}"
911+
);
912+
}
913+
}
914+
823915
#[test]
824916
fn resets_colored_models_before_each_physical_cell_border() {
825917
let mut table = SimpleTable::new(

0 commit comments

Comments
 (0)