Skip to content

Commit d4533a0

Browse files
authored
TUI: change the diff preview to have color fg not bg (#2270)
<img width="328" height="95" alt="image" src="https://github.com/user-attachments/assets/70e1e6c2-a88f-4058-8763-85c3a02eedb4" />
1 parent 99a242e commit d4533a0

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

codex-rs/tui/src/diff_render.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,14 @@ fn push_wrapped_diff_line(
270270

271271
// Reserve a fixed number of spaces after the line number so that content starts
272272
// at a consistent column. The sign ("+"/"-") is rendered as part of the content
273-
// with the same background as the edit, not as a separate dimmed column.
273+
// and colored with the same foreground as the edited text, not as a separate
274+
// dimmed column.
274275
let gap_after_ln = SPACES_AFTER_LINE_NUMBER.saturating_sub(ln_str.len());
275276
let first_prefix_cols = indent.len() + ln_str.len() + gap_after_ln;
276277
let cont_prefix_cols = indent.len() + ln_str.len() + gap_after_ln;
277278

278279
let mut first = true;
279-
let (sign_opt, bg_style) = match kind {
280+
let (sign_opt, line_style) = match kind {
280281
DiffLineType::Insert => (Some('+'), Some(style_add())),
281282
DiffLineType::Delete => (Some('-'), Some(style_del())),
282283
DiffLineType::Context => (None, None),
@@ -307,30 +308,38 @@ fn push_wrapped_diff_line(
307308
spans.push(RtSpan::raw(" ".repeat(gap_after_ln)));
308309

309310
// Prefix the content with the sign if it is an insertion or deletion, and color
310-
// the sign with the same background as the edited text.
311+
// the sign and content with the same foreground as the edited text.
311312
let display_chunk = match sign_opt {
312313
Some(sign_char) => format!("{sign_char}{chunk}"),
313314
None => chunk.to_string(),
314315
};
315316

316-
let content_span = match bg_style {
317+
let content_span = match line_style {
317318
Some(style) => RtSpan::styled(display_chunk, style),
318319
None => RtSpan::raw(display_chunk),
319320
};
320321
spans.push(content_span);
321-
lines.push(RtLine::from(spans));
322+
let mut line = RtLine::from(spans);
323+
if let Some(style) = line_style {
324+
line.style = line.style.patch(style);
325+
}
326+
lines.push(line);
322327
first = false;
323328
} else {
324329
let hang_prefix = format!(
325330
"{indent}{}{}",
326331
" ".repeat(ln_str.len()),
327332
" ".repeat(gap_after_ln)
328333
);
329-
let content_span = match bg_style {
334+
let content_span = match line_style {
330335
Some(style) => RtSpan::styled(chunk.to_string(), style),
331336
None => RtSpan::raw(chunk.to_string()),
332337
};
333-
lines.push(RtLine::from(vec![RtSpan::raw(hang_prefix), content_span]));
338+
let mut line = RtLine::from(vec![RtSpan::raw(hang_prefix), content_span]);
339+
if let Some(style) = line_style {
340+
line.style = line.style.patch(style);
341+
}
342+
lines.push(line);
334343
}
335344
}
336345
lines
@@ -341,11 +350,11 @@ fn style_dim() -> Style {
341350
}
342351

343352
fn style_add() -> Style {
344-
Style::default().bg(Color::Green)
353+
Style::default().fg(Color::Green)
345354
}
346355

347356
fn style_del() -> Style {
348-
Style::default().bg(Color::Red)
357+
Style::default().fg(Color::Red)
349358
}
350359

351360
#[allow(clippy::expect_used)]

0 commit comments

Comments
 (0)