mirror of
https://github.com/openai/codex.git
synced 2026-11-26 23:40:43 +00:00
## Why Line-buffered stdout can flush on every newline during a long history replay, turning a batch of history rows into many small terminal writes. ## What changed Use a bounded buffer to group history writes in both standard and full-screen modes. Drain the buffer to the terminal backend without explicitly flushing stdout. ## Testing Add a regression test that inserts 500 rows in each mode and checks that fewer than 50 backend writes contain newlines, the final row appears directly above the viewport, and the cursor returns to its original position. GitOrigin-RevId: c33a13fc71307a3f71be5e266e04c0160c34a8b3
141 lines
3.9 KiB
Rust
141 lines
3.9 KiB
Rust
use std::fmt::{self};
|
|
use std::io::Write;
|
|
use std::io::{self};
|
|
|
|
use ratatui::prelude::CrosstermBackend;
|
|
|
|
use ratatui::backend::Backend;
|
|
use ratatui::backend::ClearType;
|
|
use ratatui::backend::WindowSize;
|
|
use ratatui::buffer::Cell;
|
|
use ratatui::layout::Position;
|
|
use ratatui::layout::Size;
|
|
|
|
/// This wraps a CrosstermBackend and a vt100::Parser to mock
|
|
/// a "real" terminal.
|
|
///
|
|
/// Importantly, this wrapper avoids calling any crossterm methods
|
|
/// which write to stdout regardless of the writer. This includes:
|
|
/// - getting the terminal size
|
|
/// - getting the cursor position
|
|
pub struct VT100Backend {
|
|
crossterm_backend: CrosstermBackend<vt100::Parser>,
|
|
pub(crate) newline_writes: usize,
|
|
}
|
|
|
|
impl VT100Backend {
|
|
/// Creates a new `TestBackend` with the specified width and height.
|
|
pub fn new(width: u16, height: u16) -> Self {
|
|
Self::with_scrollback(width, height, /*scrollback_len*/ 0)
|
|
}
|
|
|
|
pub fn with_scrollback(width: u16, height: u16, scrollback_len: usize) -> Self {
|
|
crossterm::style::force_color_output(true);
|
|
Self {
|
|
newline_writes: 0,
|
|
crossterm_backend: CrosstermBackend::new(vt100::Parser::new(
|
|
height,
|
|
width,
|
|
scrollback_len,
|
|
)),
|
|
}
|
|
}
|
|
|
|
pub fn vt100(&self) -> &vt100::Parser {
|
|
self.crossterm_backend.writer()
|
|
}
|
|
}
|
|
|
|
impl Write for VT100Backend {
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
if buf.contains(&b'\n') {
|
|
self.newline_writes += 1;
|
|
}
|
|
self.crossterm_backend.writer_mut().write(buf)
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
self.crossterm_backend.writer_mut().flush()
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for VT100Backend {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "{}", self.crossterm_backend.writer().screen().contents())
|
|
}
|
|
}
|
|
|
|
impl Backend for VT100Backend {
|
|
type Error = io::Error;
|
|
|
|
fn draw<'a, I>(&mut self, content: I) -> io::Result<()>
|
|
where
|
|
I: Iterator<Item = (u16, u16, &'a Cell)>,
|
|
{
|
|
self.crossterm_backend.draw(content)?;
|
|
Ok(())
|
|
}
|
|
|
|
fn hide_cursor(&mut self) -> io::Result<()> {
|
|
self.crossterm_backend.hide_cursor()?;
|
|
Ok(())
|
|
}
|
|
|
|
fn show_cursor(&mut self) -> io::Result<()> {
|
|
self.crossterm_backend.show_cursor()?;
|
|
Ok(())
|
|
}
|
|
|
|
fn get_cursor_position(&mut self) -> io::Result<Position> {
|
|
Ok(self.vt100().screen().cursor_position().into())
|
|
}
|
|
|
|
fn set_cursor_position<P: Into<Position>>(&mut self, position: P) -> io::Result<()> {
|
|
self.crossterm_backend.set_cursor_position(position)
|
|
}
|
|
|
|
fn clear(&mut self) -> io::Result<()> {
|
|
self.crossterm_backend.clear()
|
|
}
|
|
|
|
fn clear_region(&mut self, clear_type: ClearType) -> io::Result<()> {
|
|
self.crossterm_backend.clear_region(clear_type)
|
|
}
|
|
|
|
fn append_lines(&mut self, line_count: u16) -> io::Result<()> {
|
|
self.crossterm_backend.append_lines(line_count)
|
|
}
|
|
|
|
fn size(&self) -> io::Result<Size> {
|
|
let (rows, cols) = self.vt100().screen().size();
|
|
Ok(Size::new(cols, rows))
|
|
}
|
|
|
|
fn window_size(&mut self) -> io::Result<WindowSize> {
|
|
Ok(WindowSize {
|
|
columns_rows: self.vt100().screen().size().into(),
|
|
// Arbitrary size, we don't rely on this in testing.
|
|
pixels: Size {
|
|
width: 640,
|
|
height: 480,
|
|
},
|
|
})
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
self.crossterm_backend.writer_mut().flush()
|
|
}
|
|
|
|
fn scroll_region_up(&mut self, region: std::ops::Range<u16>, scroll_by: u16) -> io::Result<()> {
|
|
self.crossterm_backend.scroll_region_up(region, scroll_by)
|
|
}
|
|
|
|
fn scroll_region_down(
|
|
&mut self,
|
|
region: std::ops::Range<u16>,
|
|
scroll_by: u16,
|
|
) -> io::Result<()> {
|
|
self.crossterm_backend.scroll_region_down(region, scroll_by)
|
|
}
|
|
}
|