Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions codex-rs/tui/src/diff_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ use std::path::PathBuf;
use codex_utils_absolute_path::AbsolutePathBuf;
use unicode_width::UnicodeWidthChar;

/// Replacement for a tab character in rendered diff content.
const TAB_REPLACEMENT: &str = " ";
/// Display width of a tab character in columns.
const TAB_WIDTH: usize = 4;
const TAB_WIDTH: usize = TAB_REPLACEMENT.len();

// -- Diff background palette --------------------------------------------------
//
Expand Down Expand Up @@ -989,7 +991,10 @@ fn wrap_styled_spans(spans: &[RtSpan<'static>], max_cols: usize) -> Vec<Vec<RtSp
break;
};
let ch_len = ch.len_utf8();
current_line.push(RtSpan::styled(remaining[..ch_len].to_string(), style));
current_line.push(RtSpan::styled(
remaining[..ch_len].replace('\t', TAB_REPLACEMENT),
style,
));
// Use fallback width 1 (not 0) so this branch always advances
// even if `ch` has unknown/zero display width.
col = ch.width().unwrap_or(if ch == '\t' { TAB_WIDTH } else { 1 });
Expand All @@ -998,7 +1003,7 @@ fn wrap_styled_spans(spans: &[RtSpan<'static>], max_cols: usize) -> Vec<Vec<RtSp
}

let (chunk, rest) = remaining.split_at(byte_end);
current_line.push(RtSpan::styled(chunk.to_string(), style));
current_line.push(RtSpan::styled(chunk.replace('\t', TAB_REPLACEMENT), style));
col += chars_col;
remaining = rest;

Expand Down Expand Up @@ -1370,6 +1375,15 @@ mod tests {
.render_ref(f.area(), f.buffer_mut())
})
.expect("draw");
assert!(
terminal
.backend()
.buffer()
.content()
.iter()
.all(|cell| !cell.symbol().contains('\t')),
"diff buffer should not contain literal tabs"
);
assert_snapshot!(name, terminal.backend());
}

Expand Down Expand Up @@ -2351,12 +2365,15 @@ mod tests {
fn wrap_styled_spans_tabs_have_visible_width() {
// A tab should count as TAB_WIDTH columns, not zero.
// With max_cols=8, a tab (4 cols) + "abcde" (5 cols) = 9 cols → must wrap.
let spans = vec![RtSpan::raw("\tabcde")];
let style = Style::default().fg(Color::Green);
let spans = vec![RtSpan::styled("\tabcde", style)];
let result = wrap_styled_spans(&spans, /*max_cols*/ 8);
assert!(
result.len() >= 2,
"tab + 5 chars should exceed 8 cols and wrap, got {} line(s): {result:?}",
result.len()
assert_eq!(
result,
vec![
vec![RtSpan::styled(" abcd", style)],
vec![RtSpan::styled("e", style)],
]
);
}

Expand All @@ -2373,7 +2390,7 @@ mod tests {
.collect::<String>()
})
.collect();
assert_eq!(line_text, vec!["abcd", "\t", "界"]);
assert_eq!(line_text, vec!["abcd", " ", "界"]);

let line_width = |line: &[RtSpan<'static>]| -> usize {
line.iter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ expression: terminal.backend()
---
"• Edited 6 files (+9 -9) "
" └ assets/banner.txt (+3 -0) "
" 1 +HEADER VALUE "
" 2 +rocket 🚀 " Hidden by multi-width symbols: [(15, " ")]
" 3 +city 東京 " Hidden by multi-width symbols: [(13, " "), (15, " ")]
" 1 +HEADER VALUE "
" 2 +rocket 🚀 " Hidden by multi-width symbols: [(18, " ")]
" 3 +city 東京 " Hidden by multi-width symbols: [(16, " "), (18, " ")]
" "
" └ examples/new_sample.rs (+3 -0) "
" 1 +pub fn greet(name: &str) { "
Expand All @@ -20,8 +20,8 @@ expression: terminal.backend()
" "
" └ scripts/calc.txt → scripts/calc.py (+1 -1) "
" 1 def add(a, b): "
" 2 - return a + b "
" 2 + return a + b + 42 "
" 2 - return a + b "
" 2 + return a + b + 42 "
" 3 "
" 4 print(add(1, 2)) "
" "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ expression: terminal.backend()
---
"• Edited 6 files (+9 -9) "
" └ assets/banner.txt (+3 -0) "
" 1 +HEADER VALUE "
" 2 +rocket 🚀 " Hidden by multi-width symbols: [(15, " ")]
" 3 +city 東京 " Hidden by multi-width symbols: [(13, " "), (15, " ")]
" 1 +HEADER VALUE "
" 2 +rocket 🚀 " Hidden by multi-width symbols: [(18, " ")]
" 3 +city 東京 " Hidden by multi-width symbols: [(16, " "), (18, " ")]
" "
" └ examples/new_sample.rs (+3 -0) "
" 1 +pub fn greet(name: &str) { "
Expand All @@ -20,8 +20,8 @@ expression: terminal.backend()
" "
" └ scripts/calc.txt → scripts/calc.py (+1 -1) "
" 1 def add(a, b): "
" 2 - return a + b "
" 2 + return a + b + 42 "
" 2 - return a + b "
" 2 + return a + b + 42 "
" 3 "
" 4 print(add(1, 2)) "
" "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ expression: terminal.backend()
---
"• Edited 6 files (+9 -9) "
" └ assets/banner.txt (+3 -0) "
" 1 +HEADER VALUE "
" 2 +rocket 🚀 " Hidden by multi-width symbols: [(15, " ")]
" 3 +city 東京 " Hidden by multi-width symbols: [(13, " "), (15, " ")]
" 1 +HEADER VALUE "
" 2 +rocket 🚀 " Hidden by multi-width symbols: [(18, " ")]
" 3 +city 東京 " Hidden by multi-width symbols: [(16, " "), (18, " ")]
" "
" └ examples/new_sample.rs (+3 -0) "
" 1 +pub fn greet(name: &str) { "
Expand All @@ -20,8 +20,8 @@ expression: terminal.backend()
" "
" └ scripts/calc.txt → scripts/calc.py (+1 -1) "
" 1 def add(a, b): "
" 2 - return a + b "
" 2 + return a + b + 42 "
" 2 - return a + b "
" 2 + return a + b + 42 "
" 3 "
" 4 print(add(1, 2)) "
" "
Expand Down
Loading