Skip to content

Commit 1d0d237

Browse files
ryoppippicristoslc
andcommitted
fix(terminal): preserve ANSI state across wrapped cells
Derive automatic-wrap continuation from the source fragment before truncation adds a display reset, preserving active SGR state in later model fragments. Treat authored newlines as logical boundaries while retaining state across automatic wrapping, and cover the complete 56-column five-column render. Co-authored-by: Cristos L-C <[email protected]>
1 parent b7ffa32 commit 1d0d237

2 files changed

Lines changed: 96 additions & 27 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: crates/ccusage-terminal/src/table.rs
3+
expression: "rendered.join(\"\\n\")"
4+
---
5+
┌──────────┬──────────┬──────────┬──────────┬──────────┐
6+
│ Date │ Models │ Input │ Output │ Cost │
7+
│ │ │ │ │ (USD) │
8+
├──────────┼──────────┼──────────┼──────────┼──────────┤
9+
│ 2026 │ - provi… │ 1,234 │ 5,678 │ $1.23 │
10+
│ 05-18 │ another… │ │ │ │
11+
└──────────┴──────────┴──────────┴──────────┴──────────┘

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

Lines changed: 85 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -393,19 +393,12 @@ fn wrap_cell_lines(cell: &str, width: usize) -> Vec<String> {
393393
return vec![String::new()];
394394
}
395395
let mut lines = Vec::new();
396-
let mut continuation = String::new();
397396
for line in cell.lines() {
398-
let line = if continuation.is_empty() {
399-
line.to_string()
400-
} else {
401-
format!("{continuation}{line}")
402-
};
403-
if visible_width(&line) <= width {
404-
lines.push(line.clone());
397+
if visible_width(line) <= width {
398+
lines.push(line.to_string());
405399
} else {
406-
lines.extend(wrap_cell_line(&line, width));
400+
lines.extend(wrap_cell_line(line, width));
407401
}
408-
continuation = ansi_continuation(&line);
409402
}
410403
lines
411404
}
@@ -430,6 +423,7 @@ fn wrap_cell_line(line: &str, width: usize) -> Vec<String> {
430423

431424
let mut lines = Vec::new();
432425
let mut current = String::new();
426+
let mut current_source = String::new();
433427
for word in words {
434428
let candidate_width = if current.is_empty() {
435429
visible_width(&word)
@@ -439,32 +433,36 @@ fn wrap_cell_line(line: &str, width: usize) -> Vec<String> {
439433
if candidate_width <= width {
440434
if !current.is_empty() {
441435
current.push(' ');
436+
current_source.push(' ');
442437
}
443438
current.push_str(&word);
439+
current_source.push_str(&word);
444440
} else {
445441
if !current.is_empty() {
446-
lines.push(current);
442+
lines.push((current, current_source));
447443
}
448-
current = if visible_width(&word) > width {
449-
truncate_to_width(&word, width)
444+
if visible_width(&word) > width {
445+
current = truncate_to_width(&word, width);
446+
current_source = word;
450447
} else {
451-
word
452-
};
448+
current_source = word.clone();
449+
current = word;
450+
}
453451
}
454452
}
455453
if !current.is_empty() {
456-
lines.push(current);
454+
lines.push((current, current_source));
457455
}
458456
let mut continuation = String::new();
459457
lines
460458
.into_iter()
461-
.map(|line| {
459+
.map(|(line, source)| {
462460
let line = if continuation.is_empty() {
463461
line
464462
} else {
465463
format!("{continuation}{line}")
466464
};
467-
continuation = ansi_continuation(&line);
465+
continuation = ansi_continuation(&format!("{continuation}{source}"));
468466
line
469467
})
470468
.collect()
@@ -1175,7 +1173,7 @@ mod tests {
11751173
}
11761174

11771175
#[test]
1178-
fn preserves_ansi_continuation_across_multiline_cells_without_leaking() {
1176+
fn resets_ansi_continuation_at_explicit_newlines() {
11791177
let mut table = SimpleTable::new(
11801178
vec!["Date", "Models", "Input"],
11811179
vec![Align::Left, Align::Left, Align::Right],
@@ -1197,14 +1195,17 @@ mod tests {
11971195
.filter(|line| line.contains("- first") || line.contains("- second"))
11981196
.collect::<Vec<_>>();
11991197
assert_eq!(model_lines.len(), 2, "{rendered:?}");
1200-
for line in model_lines {
1201-
let model_cell = line
1202-
.split('│')
1203-
.nth(2)
1204-
.expect("rendered row should include the Models cell");
1205-
assert!(model_cell.contains("\x1b[32m"), "{line:?}");
1206-
assert!(model_cell.trim_end().ends_with("\x1b[0m"), "{line:?}");
1207-
}
1198+
let first_cell = model_lines[0]
1199+
.split('│')
1200+
.nth(2)
1201+
.expect("rendered row should include the Models cell");
1202+
let second_cell = model_lines[1]
1203+
.split('│')
1204+
.nth(2)
1205+
.expect("rendered row should include the Models cell");
1206+
assert!(first_cell.contains("\x1b[32m"), "{rendered:?}");
1207+
assert!(first_cell.trim_end().ends_with("\x1b[0m"), "{rendered:?}");
1208+
assert!(!second_cell.contains("\x1b[32m"), "{rendered:?}");
12081209
}
12091210

12101211
#[test]
@@ -1276,4 +1277,61 @@ mod tests {
12761277
assert!(!input_cell.contains("\x1b[32m"), "{line:?}");
12771278
}
12781279
}
1280+
1281+
#[test]
1282+
fn preserves_ansi_continuation_after_truncation_in_a_narrow_table() {
1283+
let mut table = SimpleTable::new(
1284+
vec!["Date", "Models", "Input", "Output", "Cost (USD)"],
1285+
vec![
1286+
Align::Left,
1287+
Align::Left,
1288+
Align::Right,
1289+
Align::Right,
1290+
Align::Right,
1291+
],
1292+
TerminalStyle {
1293+
no_color: true,
1294+
..TerminalStyle::default()
1295+
},
1296+
)
1297+
.with_terminal_width(56)
1298+
.with_date_compaction(true);
1299+
table.push(vec![
1300+
"2026-05-18".to_string(),
1301+
"\x1b[32m- provider/very-long-model-name another-model".to_string(),
1302+
"1,234".to_string(),
1303+
"5,678".to_string(),
1304+
"$1.23".to_string(),
1305+
]);
1306+
1307+
let rendered = table.render_lines();
1308+
insta::assert_snapshot!(rendered.join("\n"));
1309+
assert!(
1310+
rendered.iter().all(|line| visible_width(line) == 56),
1311+
"{rendered:?}"
1312+
);
1313+
let model_lines = rendered
1314+
.iter()
1315+
.filter(|line| line.contains("\x1b[32m") || line.contains("another"))
1316+
.collect::<Vec<_>>();
1317+
assert_eq!(model_lines.len(), 2, "{rendered:?}");
1318+
for line in &model_lines {
1319+
let model_cell = line
1320+
.split('│')
1321+
.nth(2)
1322+
.expect("rendered row should include the Models cell");
1323+
assert!(model_cell.contains("\x1b[32m"), "{line:?}");
1324+
assert!(model_cell.trim_end().ends_with("\x1b[0m"), "{line:?}");
1325+
}
1326+
let later_model_cell = model_lines[1]
1327+
.split('│')
1328+
.nth(2)
1329+
.expect("rendered row should include the Models cell");
1330+
assert!(later_model_cell.contains("\x1b[32manother"), "{rendered:?}");
1331+
let later_input_cell = model_lines[1]
1332+
.split('│')
1333+
.nth(3)
1334+
.expect("rendered row should include the Input cell");
1335+
assert!(!later_input_cell.contains("\x1b[32m"), "{rendered:?}");
1336+
}
12791337
}

0 commit comments

Comments
 (0)