Skip to content

Commit 70be774

Browse files
committed
fix: make enter_alt_screen() respect alt_screen_enabled flag
Addresses review comments: - Resume picker now respects --no-alt-screen flag - Legacy TUI now properly wires up the flag The fix adds an alt_screen_enabled flag to the Tui struct that makes enter_alt_screen()/leave_alt_screen() no-ops when false. This ensures all call sites (resume picker, overlays, etc.) respect the setting.
1 parent 62d5258 commit 70be774

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

codex-rs/tui/src/lib.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use codex_core::config::resolve_oss_provider;
2222
use codex_core::find_conversation_path_by_id_str;
2323
use codex_core::get_platform_sandbox;
2424
use codex_core::protocol::AskForApproval;
25+
use codex_core::terminal::Multiplexer;
26+
use codex_protocol::config_types::AltScreenMode;
2527
use codex_protocol::config_types::SandboxMode;
2628
use codex_utils_absolute_path::AbsolutePathBuf;
2729
use std::fs::OpenOptions;
@@ -497,7 +499,27 @@ async fn run_ratatui_app(
497499
resume_picker::ResumeSelection::StartFresh
498500
};
499501

500-
let Cli { prompt, images, .. } = cli;
502+
let Cli {
503+
prompt,
504+
images,
505+
no_alt_screen,
506+
..
507+
} = cli;
508+
509+
// Determine whether to use alternate screen based on CLI flag and config.
510+
let use_alt_screen = if no_alt_screen {
511+
false
512+
} else {
513+
match config.tui_alternate_screen {
514+
AltScreenMode::Always => true,
515+
AltScreenMode::Never => false,
516+
AltScreenMode::Auto => {
517+
let terminal_info = codex_core::terminal::terminal_info();
518+
!matches!(terminal_info.multiplexer, Some(Multiplexer::Zellij { .. }))
519+
}
520+
}
521+
};
522+
tui.set_alt_screen_enabled(use_alt_screen);
501523

502524
let app_result = App::run(
503525
&mut tui,

codex-rs/tui/src/tui.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ pub struct Tui {
247247
terminal_focused: Arc<AtomicBool>,
248248
enhanced_keys_supported: bool,
249249
notification_backend: Option<DesktopNotificationBackend>,
250+
// When false, enter_alt_screen() becomes a no-op (for Zellij scrollback support)
251+
alt_screen_enabled: bool,
250252
}
251253

252254
impl Tui {
@@ -274,9 +276,15 @@ impl Tui {
274276
terminal_focused: Arc::new(AtomicBool::new(true)),
275277
enhanced_keys_supported,
276278
notification_backend: Some(detect_backend()),
279+
alt_screen_enabled: true,
277280
}
278281
}
279282

283+
/// Set whether alternate screen is enabled. When false, enter_alt_screen() becomes a no-op.
284+
pub fn set_alt_screen_enabled(&mut self, enabled: bool) {
285+
self.alt_screen_enabled = enabled;
286+
}
287+
280288
pub fn frame_requester(&self) -> FrameRequester {
281289
self.frame_requester.clone()
282290
}
@@ -407,6 +415,9 @@ impl Tui {
407415
/// Enter alternate screen and expand the viewport to full terminal size, saving the current
408416
/// inline viewport for restoration when leaving.
409417
pub fn enter_alt_screen(&mut self) -> Result<()> {
418+
if !self.alt_screen_enabled {
419+
return Ok(());
420+
}
410421
let _ = execute!(self.terminal.backend_mut(), EnterAlternateScreen);
411422
// Enable "alternate scroll" so terminals may translate wheel to arrows
412423
let _ = execute!(self.terminal.backend_mut(), EnableAlternateScroll);
@@ -426,6 +437,9 @@ impl Tui {
426437

427438
/// Leave alternate screen and restore the previously saved inline viewport, if any.
428439
pub fn leave_alt_screen(&mut self) -> Result<()> {
440+
if !self.alt_screen_enabled {
441+
return Ok(());
442+
}
429443
// Disable alternate scroll when leaving alt-screen
430444
let _ = execute!(self.terminal.backend_mut(), DisableAlternateScroll);
431445
let _ = execute!(self.terminal.backend_mut(), LeaveAlternateScreen);

codex-rs/tui2/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -541,9 +541,9 @@ async fn run_ratatui_app(
541541
}
542542
};
543543

544-
if use_alt_screen {
545-
let _ = tui.enter_alt_screen();
546-
}
544+
// Set flag on Tui so all enter_alt_screen() calls respect the setting
545+
tui.set_alt_screen_enabled(use_alt_screen);
546+
let _ = tui.enter_alt_screen();
547547

548548
let app_result = App::run(
549549
&mut tui,
@@ -558,9 +558,7 @@ async fn run_ratatui_app(
558558
)
559559
.await;
560560

561-
if use_alt_screen {
562-
let _ = tui.leave_alt_screen();
563-
}
561+
let _ = tui.leave_alt_screen();
564562
restore();
565563
if let Ok(exit_info) = &app_result {
566564
let mut stdout = std::io::stdout();

codex-rs/tui2/src/tui.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ pub struct Tui {
143143
terminal_focused: Arc<AtomicBool>,
144144
enhanced_keys_supported: bool,
145145
notification_backend: Option<DesktopNotificationBackend>,
146+
// When false, enter_alt_screen() becomes a no-op (for Zellij scrollback support)
147+
alt_screen_enabled: bool,
146148
}
147149

148150
impl Tui {
@@ -170,9 +172,15 @@ impl Tui {
170172
terminal_focused: Arc::new(AtomicBool::new(true)),
171173
enhanced_keys_supported,
172174
notification_backend: Some(detect_backend()),
175+
alt_screen_enabled: true,
173176
}
174177
}
175178

179+
/// Set whether alternate screen is enabled. When false, enter_alt_screen() becomes a no-op.
180+
pub fn set_alt_screen_enabled(&mut self, enabled: bool) {
181+
self.alt_screen_enabled = enabled;
182+
}
183+
176184
pub fn frame_requester(&self) -> FrameRequester {
177185
self.frame_requester.clone()
178186
}
@@ -309,6 +317,9 @@ impl Tui {
309317
/// Enter alternate screen and expand the viewport to full terminal size, saving the current
310318
/// inline viewport for restoration when leaving.
311319
pub fn enter_alt_screen(&mut self) -> Result<()> {
320+
if !self.alt_screen_enabled {
321+
return Ok(());
322+
}
312323
if !self.alt_screen_nesting.enter() {
313324
self.alt_screen_active.store(true, Ordering::Relaxed);
314325
return Ok(());
@@ -330,6 +341,9 @@ impl Tui {
330341

331342
/// Leave alternate screen and restore the previously saved inline viewport, if any.
332343
pub fn leave_alt_screen(&mut self) -> Result<()> {
344+
if !self.alt_screen_enabled {
345+
return Ok(());
346+
}
333347
if !self.alt_screen_nesting.leave() {
334348
self.alt_screen_active
335349
.store(self.alt_screen_nesting.is_active(), Ordering::Relaxed);

0 commit comments

Comments
 (0)