fix: drain OSC response when cursor position reply arrives first - #210
Open
IdLnFb wants to merge 1 commit into
Open
fix: drain OSC response when cursor position reply arrives first#210IdLnFb wants to merge 1 commit into
IdLnFb wants to merge 1 commit into
Conversation
In some terminals (iTerm2, Terminal.app on macOS) the cursor position
response to CSI 6n arrives before the OSC 11 background-color response.
termStatusReport detected a non-OSC reply and returned early, but
re-enabled TTY echo (via the deferred raw-mode restore) before the OSC
response had arrived. The OSC response then arrived with echo ON and was:
1. echoed directly to the terminal display as garbled output, and
2. left in the shell's input buffer, appearing as a phantom shell command
after the process exited.
Fix: when the first response is not an OSC reply, poll for up to 100 ms
for a pending OSC response and drain it (including the trailing backslash
of the ESC-backslash string terminator) before returning ErrStatusReport.
The 100 ms window is generous enough for the response to arrive over a
local Unix PTY without adding noticeable latency on terminals that truly
don't support OSC 11.
Reproducer: any program that calls HasDarkBackground() during package
init (e.g. charmbracelet/bubbletea v1 does this in tea_init.go) while
running in iTerm2 or Terminal.app.
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In some terminals (iTerm2, Terminal.app on macOS) the cursor position response to
CSI 6narrives before the OSC 11 background-color response.termStatusReportdetected a non-OSC reply and returned early, restoring TTY echo (via the deferred raw-mode teardown) before the OSC response had arrived. That response then arrived with echo ON and was:^[]11;rgb:0000/1313/1919^[\)Any interactive prompt rendered after the process exits picks up those buffered bytes — producing corrupted default values or unexpected input.
Fix
When the first response is not an OSC reply, poll for up to 100 ms for a pending OSC response and drain it (including the trailing
\of theESC\string terminator) before returningErrStatusReport.The 100 ms window is generous for a local Unix PTY without adding noticeable latency on terminals that don't support OSC 11 at all — they send nothing, so the
selectreturns immediately.Reproducer
Any program that calls
HasDarkBackground()during packageinittriggers this reliably in iTerm2 and Terminal.app.charmbracelet/bubbleteav1 does exactly this intea_init.go.