Skip to content

fix: restore kitty keyboard stack on exit - #1750

Open
deepakganesh78 wants to merge 3 commits into
charmbracelet:mainfrom
deepakganesh78:fix/issue1749-kitty-keyboard-stack
Open

fix: restore kitty keyboard stack on exit#1750
deepakganesh78 wants to merge 3 commits into
charmbracelet:mainfrom
deepakganesh78:fix/issue1749-kitty-keyboard-stack

Conversation

@deepakganesh78

Copy link
Copy Markdown

Fixes #1749

Reproduction

Issue #1749 reports that Bubble Tea v2 enables Kitty keyboard enhancements but exits with CSI = 0 ; 1 u, leaving terminals such as WezTerm in the enhanced keyboard state. The added TestCursedRenderer_restoresKittyKeyboardStack reproduces this programmatically by flushing and closing a renderer and asserting that Bubble Tea pushes a Kitty keyboard stack entry and pops it on close instead of resetting it in place.

Root cause

The renderer used ansi.KittyKeyboard(flags, 1) to set the active keyboard flags and ansi.KittyKeyboard(0, 1) to clear them. That overwrites the terminal's current keyboard protocol flags instead of using the Kitty keyboard stack, so Bubble Tea cannot restore the prior terminal state on exit or when switching between main and alternate screen registries.

Fix

Use ansi.PushKittyKeyboard(flags) when Bubble Tea takes ownership of a screen's keyboard enhancements, keep in-place updates for enhancement changes while the same screen is active, and use ansi.PopKittyKeyboard(0) when switching screens or closing. The first render directly into the alternate screen avoids popping the main-screen stack when Bubble Tea never pushed it. Renderer golden files were updated for the new escape sequences.

Compatibility notes

This preserves Bubble Tea's existing default keyboard enhancement flags while making cleanup stack-balanced for terminals that implement the Kitty keyboard protocol. Non-supporting terminals should continue to ignore these sequences. modifyOtherKeys cleanup is unchanged.

Validation

  • go test . -run TestCursedRenderer_restoresKittyKeyboardStack -count=1 passes with the fix.
  • The regression test fails when cursed_renderer.go is reverted to the previous behavior.
  • gofmt -l . produced no output.
  • go build ./... passed.
  • go vet ./... passed.
  • go test ./... passed.

Use the Kitty keyboard protocol stack when enabling keyboard enhancements so Bubble Tea can pop its entry on screen switches and shutdown instead of overwriting terminal state. Update renderer goldens and add a regression test for push/pop behavior.

Fixes charmbracelet#1749

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@meowgorithm

Copy link
Copy Markdown
Member

Super good catch.

@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 56.98%. Comparing base (fc707bb) to head (a708da3).
⚠️ Report is 9 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1750      +/-   ##
==========================================
+ Coverage   56.34%   56.98%   +0.63%     
==========================================
  Files          25       25              
  Lines        1315     1325      +10     
==========================================
+ Hits          741      755      +14     
+ Misses        483      481       -2     
+ Partials       91       89       -2     

☔ View full report in Codecov by Harness.
📢 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.

@andrinoff andrinoff left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there @deepakganesh78!

Thank you for your contribution! You've beat me to it 😆

Please, note the review comments below, and try to fix it.

p.s. great work!

Comment thread cursed_renderer.go
Comment thread cursed_renderer.go Outdated
Comment thread cursed_renderer.go
Comment thread cursed_renderer.go Outdated
Comment thread cursed_renderer_test.go Outdated
Comment thread cursed_renderer_test.go Outdated
@andrinoff andrinoff self-assigned this Aug 6, 2026
Signed-off-by: drew <me@andrinoff.com>
@andrinoff
andrinoff force-pushed the fix/issue1749-kitty-keyboard-stack branch from 572f646 to f79bbd5 Compare August 12, 2026 13:21
Signed-off-by: drew <me@andrinoff.com>
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.

v2: kitty keyboard protocol is left active after exit

3 participants