Skip to content

TUI: rejoin escape sequences split across reads (fixes #130) - #133

Merged
ivankovic merged 1 commit into
mainfrom
android-support
Dec 3, 2026
Merged

ivankovic merged 1 commit into
mainfrom
android-support

Conversation

@ivankovic

Copy link
Copy Markdown
Owner

Fixes #130.

Over adb shell's pty an arrow key's ESC [ A can arrive as a read() of just the 0x1B byte. crossterm 0.29 commits a lone trailing ESC of a short read as KeyCode::Esc immediately (crossterm-rs/crossterm#993), and Esc quits the viewer — so navigating with the cursor keys randomly exits on Android (also reproducible over ssh). The rest of the sequence then leaks as stray [/A key events.

A new input layer (src/tui/input.rs) sits between crossterm's EventStream and the app: a bare Esc press is held for 100 ms (vim's ttimeoutlen default); if a [/O tail follows within the window, its bytes are collected and re-mapped to the event the sequence was — arrows, Home/End, PgUp/PgDn, Insert/Delete, BackTab, F-keys, SGR/normal/rxvt mouse reports, SS3, CSI modifier params (ESC [ 1 ; 5 A = Ctrl+Up). If nothing or another key follows in time, the held Esc is emitted as-is, so a real Escape still quits, just up to 100 ms later.

Tests:

  • 11 unit tests of the rejoin state machine (no pty needed).
  • tests/tui_split_escape.rs: Linux pty e2e via script — a split Down arrow (ESC, 50 ms, [B) to the real binary keeps the viewer alive and redraws; a lone ESC still quits cleanly. Verified to fail with the hold disabled (pre-fix binary quits on the split arrow).

No CI changes needed: the e2e runs in the existing ubuntu-latest nextest job.

Over `adb shell`'s pty an arrow key's `ESC [ A` can arrive as a read of
just the 0x1B byte; crossterm 0.29 commits a lone trailing ESC of a short
read as KeyCode::Esc immediately (crossterm#993), and Esc quits the viewer,
so navigating with the cursor keys randomly exits on Android (also seen
over ssh). The tail then leaks as stray `[`/`A` keys.

A new input layer (src/tui/input.rs) sits between crossterm's EventStream
and the app: a bare Esc press is held for 100ms (vim's ttimeoutlen
default); if a `[`/`O` tail follows within the window its bytes are
collected and re-mapped to the event the sequence was - arrows, Home/End,
PageUp/PageDown, Insert/Delete, BackTab, F-keys, SGR/normal/rxvt mouse
reports, SS3, with CSI modifier params (`ESC [ 1 ; 5 A` = Ctrl+Up). If
nothing or another key follows in time, the held Esc is emitted as-is, so
a real Escape still works. Unparseable sequences are dropped, exactly as
crossterm's parser drops them when they arrive in one read.

UI::next_event wires it in with a sleep branch in the existing select loop.
Tests: 11 unit tests of the rejoin state machine, and a Linux pty e2e
(tests/tui_split_escape.rs) that sends a split Down arrow (ESC, 50ms,
"[B") to the real binary under `script` and asserts the viewer stays up
and redraws, then that a lone Esc still quits cleanly; verified to fail
with the hold disabled.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
@ivankovic
ivankovic merged commit d758ffe into main Dec 3, 2026
21 checks passed
@ivankovic
ivankovic deleted the android-support branch December 3, 2026 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: OmniDiff exits unexpectedly when pressing arrow keys in an Android ADB shell

1 participant