Skip to content

fix(tty): flush queued tty input on start#1048

Merged
gdamore merged 1 commit into
mainfrom
awake-enquiry
Apr 3, 2026
Merged

fix(tty): flush queued tty input on start#1048
gdamore merged 1 commit into
mainfrom
awake-enquiry

Conversation

@gdamore

@gdamore gdamore commented Apr 3, 2026

Copy link
Copy Markdown
Owner

Summary

  • flush queued tty input when Unix tty backends enter raw mode
  • apply the flush to both /dev/tty and stdin/stdout tty implementations
  • avoid stale startup mouse reports such as Ghostty paste-triggered input

Testing

  • go test ./...

Fixes #1045

Summary by CodeRabbit

Bug Fixes

  • Improved terminal input handling by flushing any pending input immediately after entering raw mode, preventing stale input from being processed in subsequent reads.

@coderabbitai

coderabbitai Bot commented Apr 3, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0677be6f-77ca-41ed-aca5-78948f3051a1

📥 Commits

Reviewing files that changed from the base of the PR and between 1ce3aa5 and 1576c34.

📒 Files selected for processing (4)
  • tty/nonblock_bsd.go
  • tty/nonblock_unix.go
  • tty/stdin_unix.go
  • tty/tty_unix.go

📝 Walkthrough

Walkthrough

This pull request adds terminal input flushing capabilities to Unix-based systems (BSD/macOS and Linux). Platform-specific tcFlushInput() helper functions are introduced to discard queued terminal input, and these are integrated into TTY startup routines to clear pending input after switching to raw mode, with appropriate error handling and state restoration on failure.

Changes

Cohort / File(s) Summary
Input flush helpers
tty/nonblock_bsd.go, tty/nonblock_unix.go
Added platform-specific tcFlushInput() functions: BSD/macOS version uses TIOCFLUSH ioctl with TCIFLUSH flag; Unix version uses unix.IoctlSetInt with TCFLSH to discard queued terminal input.
TTY startup integration
tty/stdin_unix.go, tty/tty_unix.go
Integrated tcFlushInput() calls into Start() methods immediately after switching terminal to raw mode via term.MakeRaw; added error handling to restore terminal state and return errors if flush fails.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • PR #923 — Restructures devTty.Start error paths and resource cleanup in tty_unix.go, sharing overlapping startup/cleanup logic with this PR's additions.

Poem

🐰 Hop hop, flush away,
Stale input can't delay,
Raw mode, then we clear the queue,
Rabbit whiskers twitch on cue!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title 'fix(tty): flush queued tty input on start' directly and accurately summarizes the main change: flushing TTY input when starting raw mode to fix stale input state.
Linked Issues check ✅ Passed The PR addresses issue #1045 by flushing queued TTY input on start in both Unix implementations (stdin and /dev/tty), which directly resolves the Ghostty paste-triggered stale mouse input issue.
Out of Scope Changes check ✅ Passed All changes are in-scope: new tcFlushInput helpers for BSD and Unix, plus integration into Start() methods for both stdin and /dev/tty backends, all targeting the stale input problem.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch awake-enquiry

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented Apr 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 36.36364% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.22%. Comparing base (1ce3aa5) to head (1576c34).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
tty/tty_unix.go 0.00% 3 Missing and 1 partial ⚠️
tty/stdin_unix.go 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1048      +/-   ##
==========================================
- Coverage   86.32%   86.22%   -0.11%     
==========================================
  Files          46       46              
  Lines        5046     5059      +13     
==========================================
+ Hits         4356     4362       +6     
- Misses        534      540       +6     
- Partials      156      157       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@gdamore

gdamore commented Apr 3, 2026

Copy link
Copy Markdown
Owner Author

Ignore the codecov problems ... its pretty much impossible to test the flush of ttys in CI/CD.

@gdamore gdamore merged commit bea6721 into main Apr 3, 2026
13 of 15 checks passed
@gdamore gdamore deleted the awake-enquiry branch April 3, 2026 00:24
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.

Ghostty left mouse button gets stuck after paste in tcell demo until right-click

1 participant