Skip to content

Commit d152534

Browse files
fix clear-to-end being emitted at the end of a row (#4447)
This was causing glitchy behavior when a line in the input was the exact width of the terminal.
1 parent c4120a2 commit d152534

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

codex-rs/tui/src/custom_terminal.rs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,13 @@ fn diff_buffers<'a>(a: &'a Buffer, b: &'a Buffer) -> Vec<DrawCommand<'a>> {
412412
.unwrap_or(0);
413413
last_nonblank_column[y as usize] = x as u16;
414414
let (x_abs, y_abs) = a.pos_of(row_start + x + 1);
415-
updates.push(DrawCommand::ClearToEnd {
416-
x: x_abs,
417-
y: y_abs,
418-
bg,
419-
});
415+
if x < (a.area.width as usize).saturating_sub(1) {
416+
updates.push(DrawCommand::ClearToEnd {
417+
x: x_abs,
418+
y: y_abs,
419+
bg,
420+
});
421+
}
420422
}
421423

422424
// Cells invalidated by drawing/replacing preceding multi-width characters:
@@ -570,3 +572,38 @@ impl ModifierDiff {
570572
Ok(())
571573
}
572574
}
575+
576+
#[cfg(test)]
577+
mod tests {
578+
use super::*;
579+
use pretty_assertions::assert_eq;
580+
use ratatui::layout::Rect;
581+
582+
#[test]
583+
fn diff_buffers_does_not_emit_clear_to_end_for_full_width_row() {
584+
let area = Rect::new(0, 0, 3, 2);
585+
let previous = Buffer::empty(area);
586+
let mut next = Buffer::empty(area);
587+
588+
next.cell_mut((2, 0))
589+
.expect("cell should exist")
590+
.set_symbol("X");
591+
592+
let commands = diff_buffers(&previous, &next);
593+
594+
let clear_count = commands
595+
.iter()
596+
.filter(|command| matches!(command, DrawCommand::ClearToEnd { y, .. } if *y == 0))
597+
.count();
598+
assert_eq!(
599+
0, clear_count,
600+
"expected diff_buffers not to emit ClearToEnd; commands: {commands:?}",
601+
);
602+
assert!(
603+
commands
604+
.iter()
605+
.any(|command| matches!(command, DrawCommand::Put { x: 2, y: 0, .. })),
606+
"expected diff_buffers to update the final cell; commands: {commands:?}",
607+
);
608+
}
609+
}

0 commit comments

Comments
 (0)