mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Avoid querying terminal size on every TUI redraw (#36482)
## What changed - Carry dimensions on resize events and reuse the cached screen size for ordinary draws. - Refresh terminal geometry after resize settling, process resume, and external program execution. - Pass the resolved size through transcript reflow, viewport updates, and standalone TUI screens so each frame uses consistent dimensions. ## Testing - Cover cached draws, resize-driven rendering, settled-size rechecks, resume refreshes, and alternate-screen size updates. GitOrigin-RevId: 9d83dbab50d7a038bd59b4c66c13abcac69ddc79
This commit is contained in:
1 parent
ee0247f95a
commit
7dc1856685
21 files changed
+398
-97
No files matched your search
@@ -437,13 +437,14 @@ impl App {
|
||||
AppEvent::CodexOp(op) => {
|
||||
let is_user_turn = matches!(&op, AppCommand::UserTurn { .. });
|
||||
if is_user_turn {
|
||||
self.handle_draw_pre_render(tui)?;
|
||||
let screen_size = tui.terminal.last_known_screen_size;
|
||||
self.handle_draw_pre_render(tui, screen_size)?;
|
||||
if self.transcript_reflow.has_pending_reflow() {
|
||||
self.transcript_reflow.schedule_immediate();
|
||||
self.maybe_run_resize_reflow(tui)?;
|
||||
self.maybe_run_resize_reflow(tui, screen_size)?;
|
||||
}
|
||||
self.chat_widget.pre_draw_tick();
|
||||
self.render_chat_widget_frame(tui)?;
|
||||
self.render_chat_widget_frame(tui, screen_size)?;
|
||||
}
|
||||
self.chat_widget.prepare_local_op_submission(&op);
|
||||
if let Err(err) = self.submit_active_thread_op(app_server, op).await {
|
||||
|
||||
@@ -79,7 +79,8 @@ impl App {
|
||||
} else {
|
||||
self.chat_widget.set_raw_output_mode(enabled);
|
||||
}
|
||||
if let Err(err) = self.reflow_transcript_now(tui) {
|
||||
let terminal_width = tui.terminal.last_known_screen_size.into();
|
||||
if let Err(err) = self.reflow_transcript_now(tui, terminal_width) {
|
||||
tracing::warn!(error = %err, "failed to reflow transcript after raw output mode toggle");
|
||||
self.chat_widget
|
||||
.add_error_message(format!("Failed to redraw transcript: {err}"));
|
||||
|
||||
@@ -30,6 +30,16 @@ use crate::terminal_hyperlinks::HyperlinkLine;
|
||||
use crate::transcript_reflow::TRANSCRIPT_REFLOW_DEBOUNCE;
|
||||
use crate::tui;
|
||||
|
||||
/// Full terminal width before transcript-specific layout reservations.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub(super) struct TerminalWidth(u16);
|
||||
|
||||
impl From<ratatui::layout::Size> for TerminalWidth {
|
||||
fn from(size: ratatui::layout::Size) -> Self {
|
||||
Self(size.width)
|
||||
}
|
||||
}
|
||||
|
||||
struct ReflowCellDisplay {
|
||||
lines: Vec<HyperlinkLine>,
|
||||
is_stream_continuation: bool,
|
||||
@@ -253,7 +263,8 @@ impl App {
|
||||
pub(super) fn maybe_finish_stream_reflow(&mut self, tui: &mut tui::Tui) -> Result<()> {
|
||||
if self.transcript_reflow.take_stream_finish_reflow_needed() {
|
||||
self.schedule_immediate_resize_reflow(tui);
|
||||
self.maybe_run_resize_reflow(tui)?;
|
||||
let screen_size = tui.terminal.last_known_screen_size;
|
||||
self.maybe_run_resize_reflow(tui, screen_size)?;
|
||||
} else if self.transcript_reflow.pending_is_due(Instant::now()) {
|
||||
tui.frame_requester().schedule_frame();
|
||||
}
|
||||
@@ -285,7 +296,8 @@ impl App {
|
||||
}
|
||||
|
||||
self.schedule_immediate_resize_reflow(tui);
|
||||
self.maybe_run_resize_reflow(tui)?;
|
||||
let screen_size = tui.terminal.last_known_screen_size;
|
||||
self.maybe_run_resize_reflow(tui, screen_size)?;
|
||||
if !self.transcript_reflow.has_pending_reflow() {
|
||||
self.transcript_reflow.clear_stream_flags();
|
||||
}
|
||||
@@ -342,8 +354,11 @@ impl App {
|
||||
self.reset_history_emission_state();
|
||||
}
|
||||
|
||||
pub(super) fn handle_draw_pre_render(&mut self, tui: &mut tui::Tui) -> Result<()> {
|
||||
let size = tui.terminal.size()?;
|
||||
pub(super) fn handle_draw_pre_render(
|
||||
&mut self,
|
||||
tui: &mut tui::Tui,
|
||||
size: ratatui::layout::Size,
|
||||
) -> Result<()> {
|
||||
let should_rebuild_transcript = self.handle_draw_size_change(
|
||||
size,
|
||||
tui.terminal.last_known_screen_size,
|
||||
@@ -355,7 +370,7 @@ impl App {
|
||||
// rebuild from transcript cells.
|
||||
tui.clear_pending_history_lines();
|
||||
}
|
||||
self.maybe_run_resize_reflow(tui)?;
|
||||
self.maybe_run_resize_reflow(tui, size)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -365,7 +380,11 @@ impl App {
|
||||
/// surface. Callers must keep using `HistoryCell` source as the rebuild input; attempting to
|
||||
/// reuse terminal-wrapped output here would preserve exactly the stale wrapping this feature is
|
||||
/// meant to remove.
|
||||
pub(super) fn maybe_run_resize_reflow(&mut self, tui: &mut tui::Tui) -> Result<()> {
|
||||
pub(super) fn maybe_run_resize_reflow(
|
||||
&mut self,
|
||||
tui: &mut tui::Tui,
|
||||
screen_size: ratatui::layout::Size,
|
||||
) -> Result<()> {
|
||||
let Some(deadline) = self.transcript_reflow.pending_until() else {
|
||||
return Ok(());
|
||||
};
|
||||
@@ -390,8 +409,8 @@ impl App {
|
||||
let reflow_ran_during_stream =
|
||||
!self.transcript_cells.is_empty() && self.should_mark_reflow_as_stream_time();
|
||||
|
||||
let width = self.reflow_transcript_now(tui)?;
|
||||
self.transcript_reflow.mark_reflowed_width(width);
|
||||
let width = self.reflow_transcript_now(tui, screen_size.into())?;
|
||||
self.transcript_reflow.mark_reflowed_width(width.0);
|
||||
|
||||
if reflow_ran_during_stream {
|
||||
self.transcript_reflow.mark_ran_during_stream();
|
||||
@@ -399,15 +418,17 @@ impl App {
|
||||
// Some terminals settle their final reported width after the repaint that handled the
|
||||
// last resize event. Request one cheap follow-up draw so `handle_draw_pre_render` can
|
||||
// sample that width and schedule a final reflow if needed.
|
||||
tui.frame_requester()
|
||||
.schedule_frame_in(TRANSCRIPT_REFLOW_DEBOUNCE);
|
||||
tui.schedule_screen_size_recheck(TRANSCRIPT_REFLOW_DEBOUNCE);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn reflow_transcript_now(&mut self, tui: &mut tui::Tui) -> Result<u16> {
|
||||
let terminal_width = tui.terminal.size()?.width;
|
||||
let width = self.chat_widget.history_wrap_width(terminal_width);
|
||||
pub(super) fn reflow_transcript_now(
|
||||
&mut self,
|
||||
tui: &mut tui::Tui,
|
||||
terminal_width: TerminalWidth,
|
||||
) -> Result<TerminalWidth> {
|
||||
let width = self.chat_widget.history_wrap_width(terminal_width.0);
|
||||
if self.transcript_cells.is_empty() {
|
||||
// Drop any queued pre-resize/pre-consolidation inserts before rebuilding from cells.
|
||||
tui.clear_pending_history_lines();
|
||||
@@ -438,9 +459,12 @@ impl App {
|
||||
/// Unlike resize reflow, rollback must clear the terminal even when no cells remain. Otherwise
|
||||
/// the cancelled user prompt stays visible in scrollback despite being removed from the source
|
||||
/// transcript.
|
||||
pub(super) fn rebuild_transcript_after_backtrack(&mut self, tui: &mut tui::Tui) -> Result<()> {
|
||||
let terminal_width = tui.terminal.size()?.width;
|
||||
let width = self.chat_widget.history_wrap_width(terminal_width);
|
||||
pub(super) fn rebuild_transcript_after_backtrack(
|
||||
&mut self,
|
||||
tui: &mut tui::Tui,
|
||||
terminal_width: TerminalWidth,
|
||||
) -> Result<()> {
|
||||
let width = self.chat_widget.history_wrap_width(terminal_width.0);
|
||||
let reflowed_lines = if self.transcript_cells.is_empty() {
|
||||
self.reset_history_emission_state();
|
||||
Vec::new()
|
||||
|
||||
@@ -5357,7 +5357,8 @@ async fn required_stream_reflow_during_capped_initial_replay_survives_transcript
|
||||
assert!(app.initial_history_replay_buffer.is_none());
|
||||
assert!(app.transcript_reflow.has_pending_reflow());
|
||||
|
||||
app.maybe_run_resize_reflow(&mut tui)?;
|
||||
let screen_size = tui.terminal.last_known_screen_size;
|
||||
app.maybe_run_resize_reflow(&mut tui, screen_size)?;
|
||||
assert!(app.transcript_reflow.has_pending_reflow());
|
||||
|
||||
app.close_transcript_overlay(&mut tui);
|
||||
@@ -5422,6 +5423,27 @@ async fn height_shrink_schedules_resize_reflow() {
|
||||
assert!(app.transcript_reflow.has_pending_reflow());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn resizing_empty_transcript_schedules_settled_size_recheck() {
|
||||
let (mut app, _rx, _op_rx) = make_test_app_with_channels().await;
|
||||
let mut tui = crate::tui::test_support::make_test_tui().expect("test tui");
|
||||
let frame_requester = crate::tui::FrameRequester::test_dummy();
|
||||
let initial_size = ratatui::layout::Size::new(/*width*/ 80, /*height*/ 24);
|
||||
let resized_size = ratatui::layout::Size::new(/*width*/ 100, /*height*/ 24);
|
||||
|
||||
assert!(!app.handle_draw_size_change(initial_size, initial_size, &frame_requester));
|
||||
tui.screen_size_for_event(&TuiEvent::Resize(resized_size))
|
||||
.expect("resolve resize event");
|
||||
tui.terminal.resize(resized_size).expect("apply event size");
|
||||
assert!(app.handle_draw_size_change(resized_size, initial_size, &frame_requester));
|
||||
tokio::time::sleep(crate::transcript_reflow::TRANSCRIPT_REFLOW_DEBOUNCE).await;
|
||||
assert_eq!(
|
||||
tui.screen_size_for_event(&TuiEvent::Draw)
|
||||
.expect("resolve settled size"),
|
||||
initial_size
|
||||
);
|
||||
}
|
||||
|
||||
fn test_turn(turn_id: &str, status: TurnStatus, items: Vec<ThreadItem>) -> Turn {
|
||||
Turn {
|
||||
id: turn_id.to_string(),
|
||||
|
||||
Reference in new issue
Block a user