mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
Restore the terminal's original alternate-scroll mode (#61697)
## Why Releasing TUI input modes previously disabled alternate scrolling unconditionally, overriding terminals where it was initially enabled. ## What changed - Query alternate-scroll mode (`1007`) during the Unix startup probe and filter its replies from replayed input. - Restore the recorded mode during editor handoff and exit, defaulting to disabled when the initial state is unknown. - Retry scroll-mode restoration after a failed cleanup write. ## Testing Extend startup probe tests to parse enabled and disabled reports. Add coverage for preserving both initial states across handoff and exit, and retrying failed restoration. GitOrigin-RevId: 786a44c04da6e2c9cb9af73c096d8df92ded88e9
This commit is contained in:
@@ -35,6 +35,15 @@ pub(crate) struct DefaultColors {
|
||||
pub(crate) bg: (u8, u8, u8),
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn alternate_scroll_report(input: &[u8]) -> Option<bool> {
|
||||
match input {
|
||||
b"\x1b[?1007;1$y" | b"\x1b[?1007;3$y" => Some(true),
|
||||
b"\x1b[?1007;0$y" | b"\x1b[?1007;2$y" | b"\x1b[?1007;4$y" => Some(false),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[cfg_attr(test, allow(dead_code))]
|
||||
mod imp {
|
||||
@@ -66,6 +75,7 @@ mod imp {
|
||||
/// Results from the TUI's one-shot startup terminal probe.
|
||||
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
||||
pub(crate) struct StartupProbe {
|
||||
pub(crate) alternate_scroll_enabled: Option<bool>,
|
||||
pub(crate) cursor_position: Option<Position>,
|
||||
pub(crate) default_colors: Option<DefaultColors>,
|
||||
pub(crate) keyboard_enhancement_supported: Option<bool>,
|
||||
@@ -307,6 +317,8 @@ mod imp {
|
||||
if keyboard_probe == StartupKeyboardEnhancementProbe::Query {
|
||||
query.extend_from_slice(b"\x1B[?u");
|
||||
}
|
||||
// Query before DA1 so its reply acts as a barrier even if mode 1007 is unsupported.
|
||||
query.extend_from_slice(b"\x1B[?1007$p");
|
||||
// DA1 finishes keyboard detection and establishes whether we can use it at exit.
|
||||
query.extend_from_slice(b"\x1B[c\x1B[6n\x1B]10;?\x1B\\\x1B]11;?\x1B\\");
|
||||
if query_identity {
|
||||
@@ -365,6 +377,7 @@ mod imp {
|
||||
) -> io::Result<StartupProbe> {
|
||||
let deadline = Instant::now() + timeout;
|
||||
let mut probe = StartupProbe {
|
||||
alternate_scroll_enabled: None,
|
||||
cursor_position: None,
|
||||
default_colors: None,
|
||||
keyboard_enhancement_supported: None,
|
||||
@@ -400,6 +413,10 @@ mod imp {
|
||||
buffer: &[u8],
|
||||
keyboard_probe: StartupKeyboardEnhancementProbe,
|
||||
) {
|
||||
if probe.alternate_scroll_enabled.is_none() {
|
||||
probe.alternate_scroll_enabled =
|
||||
buffer.windows(11).find_map(super::alternate_scroll_report);
|
||||
}
|
||||
if probe.cursor_position.is_none() {
|
||||
probe.cursor_position = parse_cursor_position(buffer);
|
||||
}
|
||||
@@ -676,6 +693,7 @@ mod imp {
|
||||
#[test]
|
||||
fn startup_probe_parses_batched_terminal_responses() {
|
||||
let mut probe = StartupProbe {
|
||||
alternate_scroll_enabled: None,
|
||||
cursor_position: None,
|
||||
default_colors: None,
|
||||
keyboard_enhancement_supported: None,
|
||||
@@ -685,13 +703,14 @@ mod imp {
|
||||
update_startup_probe(
|
||||
&mut probe,
|
||||
&mut saw_supported_keyboard,
|
||||
b"\x1B[20;10R\x1B]11;rgb:1111/1111/1111\x07\x1B[?64;1;2c\x1B]10;rgb:eeee/eeee/eeee\x1B\\\x1B[?7u",
|
||||
b"\x1B[?1007;1$y\x1B[20;10R\x1B]11;rgb:1111/1111/1111\x07\x1B[?64;1;2c\x1B]10;rgb:eeee/eeee/eeee\x1B\\\x1B[?7u",
|
||||
StartupKeyboardEnhancementProbe::Query,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
probe,
|
||||
StartupProbe {
|
||||
alternate_scroll_enabled: Some(true),
|
||||
cursor_position: Some(Position { x: 9, y: 19 }),
|
||||
default_colors: Some(DefaultColors {
|
||||
fg: (238, 238, 238),
|
||||
@@ -710,6 +729,7 @@ mod imp {
|
||||
#[test]
|
||||
fn startup_probe_waits_for_identity_even_when_keyboard_probe_is_skipped() {
|
||||
let mut probe = StartupProbe {
|
||||
alternate_scroll_enabled: None,
|
||||
cursor_position: None,
|
||||
default_colors: None,
|
||||
keyboard_enhancement_supported: None,
|
||||
@@ -762,7 +782,7 @@ mod imp {
|
||||
fn startup_probe_finds_responses_after_large_typeahead() {
|
||||
let mut input = vec![b'x'; 2_048];
|
||||
input.extend_from_slice(
|
||||
b"\x1B[20;10R\x1B]11;rgb:1111/1111/1111\x07\x1B[?64;1;2c\x1B]10;rgb:eeee/eeee/eeee\x1B\\\x1B[?7u",
|
||||
b"\x1B[?1007;2$y\x1B[20;10R\x1B]11;rgb:1111/1111/1111\x07\x1B[?64;1;2c\x1B]10;rgb:eeee/eeee/eeee\x1B\\\x1B[?7u",
|
||||
);
|
||||
let mut tty = tty_with_buffered_input(&input);
|
||||
let mut buffer = Vec::new();
|
||||
@@ -779,6 +799,7 @@ mod imp {
|
||||
assert_eq!(
|
||||
probe,
|
||||
StartupProbe {
|
||||
alternate_scroll_enabled: Some(false),
|
||||
cursor_position: Some(Position { x: 9, y: 19 }),
|
||||
default_colors: Some(DefaultColors {
|
||||
fg: (238, 238, 238),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Preserve startup terminal input while filtering completed terminal-color replies.
|
||||
//! Preserve startup terminal input while filtering completed color and scroll-mode replies.
|
||||
|
||||
const MAX_REPLAYED_OSC_BYTES: usize = 1024;
|
||||
|
||||
@@ -13,6 +13,13 @@ pub(super) fn startup_replay_input(input: &[u8]) -> Vec<u8> {
|
||||
let mut cursor = 0;
|
||||
let mut inside_paste = false;
|
||||
while cursor < input.len() {
|
||||
if input
|
||||
.get(cursor..cursor + 11)
|
||||
.is_some_and(|report| super::alternate_scroll_report(report).is_some())
|
||||
{
|
||||
cursor += 11;
|
||||
continue;
|
||||
}
|
||||
if inside_paste {
|
||||
if input[cursor..].starts_with(PASTE_END) {
|
||||
replay.extend_from_slice(PASTE_END);
|
||||
|
||||
@@ -544,6 +544,7 @@ pub(crate) fn init() -> Result<InitializedTerminal> {
|
||||
"terminal startup probes failed: {err}"
|
||||
);
|
||||
crate::terminal_probe::StartupProbe {
|
||||
alternate_scroll_enabled: None,
|
||||
cursor_position: None,
|
||||
default_colors: None,
|
||||
keyboard_enhancement_supported: None,
|
||||
@@ -553,6 +554,14 @@ pub(crate) fn init() -> Result<InitializedTerminal> {
|
||||
}
|
||||
};
|
||||
|
||||
#[cfg(unix)]
|
||||
ALTERNATE_SCREEN.initial_scroll_enabled.store(
|
||||
startup_probe
|
||||
.alternate_scroll_enabled
|
||||
.unwrap_or(/*default*/ false),
|
||||
Ordering::Relaxed,
|
||||
);
|
||||
|
||||
#[cfg(unix)]
|
||||
crate::terminal_palette::set_default_colors_from_startup_probe(startup_probe.default_colors);
|
||||
|
||||
|
||||
@@ -32,6 +32,8 @@ pub(super) static ALTERNATE_SCREEN: AlternateScreen = AlternateScreen {
|
||||
mouse_active: AtomicBool::new(/*v*/ false),
|
||||
mouse_capture_disabled: AtomicBool::new(/*v*/ false),
|
||||
input_configured: AtomicBool::new(/*v*/ false),
|
||||
initial_scroll_enabled: AtomicBool::new(/*v*/ false),
|
||||
scroll_dirty: AtomicBool::new(/*v*/ false),
|
||||
keyboard_active: AtomicBool::new(/*v*/ false),
|
||||
parent_reports_key_releases: AtomicBool::new(/*v*/ false),
|
||||
link_pointer: super::link_pointer::LinkPointer::new(),
|
||||
@@ -46,6 +48,8 @@ pub(super) struct AlternateScreen {
|
||||
mouse_capture_disabled: AtomicBool,
|
||||
// A cleanup/setup error must not make the next identical request look already applied.
|
||||
input_configured: AtomicBool,
|
||||
pub(super) initial_scroll_enabled: AtomicBool,
|
||||
scroll_dirty: AtomicBool,
|
||||
// An editor can take over the screen after Codex has popped its keyboard mode.
|
||||
keyboard_active: AtomicBool,
|
||||
// Restore the main screen's input classification when this screen's keyboard frame is popped.
|
||||
@@ -115,6 +119,7 @@ impl AlternateScreen {
|
||||
) -> Result<()> {
|
||||
self.input_configured
|
||||
.store(/*val*/ false, Ordering::Relaxed);
|
||||
self.scroll_dirty.store(/*val*/ true, Ordering::Relaxed);
|
||||
let result = if capture_mouse && !self.mouse_capture_disabled.load(Ordering::Relaxed) {
|
||||
// A partial write can already enable reporting; cleanup must still attempt to stop it.
|
||||
self.mouse_active.store(/*val*/ true, Ordering::Relaxed);
|
||||
@@ -191,10 +196,22 @@ impl AlternateScreen {
|
||||
} else {
|
||||
Ok(())
|
||||
};
|
||||
let scroll_result = execute!(writer, DisableAlternateScroll);
|
||||
let scroll_result = self.restore_scroll(writer);
|
||||
mouse_result.and(keyboard_result).and(scroll_result)
|
||||
}
|
||||
|
||||
fn restore_scroll(&self, writer: &mut impl Write) -> Result<()> {
|
||||
if self.scroll_dirty.load(Ordering::Relaxed) {
|
||||
if self.initial_scroll_enabled.load(Ordering::Relaxed) {
|
||||
execute!(writer, EnableAlternateScroll)?;
|
||||
} else {
|
||||
execute!(writer, DisableAlternateScroll)?;
|
||||
}
|
||||
self.scroll_dirty.store(/*val*/ false, Ordering::Relaxed);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn leave(&self, writer: &mut impl Write) -> Result<()> {
|
||||
let input_result = self.release_input(writer);
|
||||
// A failed earlier cleanup write must not prevent the actual screen transition.
|
||||
@@ -214,7 +231,9 @@ impl AlternateScreen {
|
||||
self.leave(writer)
|
||||
} else {
|
||||
// A failed mouse cleanup can outlive a successful return to the main screen.
|
||||
self.disable_mouse(writer)
|
||||
let mouse_result = self.disable_mouse(writer);
|
||||
let scroll_result = self.restore_scroll(writer);
|
||||
mouse_result.and(scroll_result)
|
||||
};
|
||||
if self.active.load(Ordering::Relaxed) {
|
||||
// Leave failed: another pop here would still target the alternate stack.
|
||||
|
||||
@@ -13,6 +13,7 @@ struct KeyboardScreens {
|
||||
main: Vec<u8>,
|
||||
alternate: Vec<u8>,
|
||||
alternate_active: bool,
|
||||
alternate_scroll: bool,
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -67,6 +68,8 @@ impl KeyboardScreens {
|
||||
match sequence {
|
||||
"[?1049h" => self.alternate_active = true,
|
||||
"[?1049l" => self.alternate_active = false,
|
||||
"[?1007h" => self.alternate_scroll = true,
|
||||
"[?1007l" => self.alternate_scroll = false,
|
||||
_ => {
|
||||
let stack = if self.alternate_active {
|
||||
&mut self.alternate
|
||||
@@ -95,6 +98,39 @@ impl KeyboardScreens {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alternate_scroll_is_restored_across_handoff_and_exit() {
|
||||
for initial_scroll in [false, true] {
|
||||
let screen = AlternateScreen::default();
|
||||
screen
|
||||
.initial_scroll_enabled
|
||||
.store(initial_scroll, Ordering::Relaxed);
|
||||
let mut output = Vec::new();
|
||||
let mut terminal = KeyboardScreens {
|
||||
alternate_scroll: initial_scroll,
|
||||
..Default::default()
|
||||
};
|
||||
enter_with_mouse_enabled(&screen, &mut output, /*capture_mouse*/ true).unwrap();
|
||||
terminal.process(&std::mem::take(&mut output));
|
||||
assert!(!terminal.alternate_scroll);
|
||||
|
||||
screen
|
||||
.configure_input(&mut output, /*capture_mouse*/ false)
|
||||
.unwrap();
|
||||
screen.release_input(&mut output).unwrap();
|
||||
terminal.process(&std::mem::take(&mut output));
|
||||
assert_eq!(terminal.alternate_scroll, initial_scroll);
|
||||
|
||||
screen.leave(&mut output).unwrap();
|
||||
enter_with_mouse_enabled(&screen, &mut output, /*capture_mouse*/ true).unwrap();
|
||||
screen
|
||||
.restore(&mut output, KeyboardRestore::PopStack)
|
||||
.unwrap();
|
||||
terminal.process(&output);
|
||||
assert_eq!(terminal.alternate_scroll, initial_scroll);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn editor_handoff_balances_keyboard_stacks_if_editor_leaves_the_screen() {
|
||||
if keyboard_modes::keyboard_enhancement_disabled() {
|
||||
@@ -262,6 +298,13 @@ fn cleanup_write_failure_does_not_pop_the_wrong_screen() {
|
||||
(stays_in_alternate, expected_main, &[][..]),
|
||||
),
|
||||
);
|
||||
if sequence == b"\x1b[?1007l" {
|
||||
let offset = writer.get_ref().output.len();
|
||||
screen
|
||||
.restore(&mut writer, KeyboardRestore::PopStack)
|
||||
.unwrap();
|
||||
assert!(writer.get_ref().output[offset..].starts_with(b"\x1b[?1007l"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user