Compare commits

...

2 commits

Author SHA1 Message Date
drew
bab9b8e855
fix: fixup
Signed-off-by: drew <me@andrinoff.com>
2026-08-12 17:02:00 +04:00
deepakganesh78
c2d0c81afa fix: restore kitty keyboard stack on exit
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 #1749

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-08-02 22:18:26 +05:30
15 changed files with 196 additions and 27 deletions

View file

@ -33,6 +33,7 @@ type cursedRenderer struct {
mapnl bool
syncdUpdates bool // whether to use synchronized output mode for updates
starting bool // indicates whether the renderer is starting after being stopped
kittyPushed bool // whether we have an entry on the terminal's Kitty keyboard stack
}
var _ renderer = &cursedRenderer{}
@ -118,6 +119,7 @@ func (s *cursedRenderer) start() {
if s.lastView.ReportFocus {
_, _ = s.scr.WriteString(ansi.SetModeFocusEvent)
}
// Mouse modes.
switch s.lastView.MouseMode {
case MouseModeNone:
case MouseModeCellMotion:
@ -131,12 +133,15 @@ func (s *cursedRenderer) start() {
if s.lastView.ProgressBar != nil {
setProgressBar(s, s.lastView.ProgressBar)
}
// Enable modifyOtherKeys and Kitty keyboard protocol.
// Both can coexist; terminals ignore what they don't support.
// Enable modifyOtherKeys. The Kitty keyboard protocol is restored
// by flush below.
_, _ = s.scr.WriteString(ansi.SetModifyOtherKeys2)
kittyFlags := keyboardEnhancementsFlags(s.lastView.KeyboardEnhancements)
_, _ = s.scr.WriteString(ansi.KittyKeyboard(kittyFlags, 1))
// The last view no longer describes what is on the screen: the
// sequences above re-applied everything except the content, which
// the next flush repaints. Clearing it makes the next flush treat
// the frame as the first render of the restored session.
s.lastView = nil
}
// close implements renderer.
@ -153,10 +158,11 @@ func (s *cursedRenderer) close() (err error) {
// two registries for the main and alt screens. We disable keyboard
// enhancements whenever we enter/exit alt screen mode in
// [cursedRenderer.flush].
// Here, we reset the keyboard protocol of the last screen used
// assuming the other screen is already reset when we switched screens.
// Here, we pop the keyboard protocol of the last screen used
// assuming the other screen is already popped when we switched screens.
_, _ = s.buf.WriteString(ansi.ResetModifyOtherKeys)
_, _ = s.buf.WriteString(ansi.KittyKeyboard(0, 1))
_, _ = s.buf.WriteString(ansi.PopKittyKeyboard(1))
s.kittyPushed = false
// Go to the bottom of the screen.
// We need to go to the bottom of the screen regardless of whether
@ -290,6 +296,7 @@ func (s *cursedRenderer) flush(closing bool) error {
}
// We're no longer starting.
starting := s.starting
s.starting = false
if frameArea != s.cellbuf.Bounds() {
@ -318,13 +325,14 @@ func (s *cursedRenderer) flush(closing bool) error {
// Alt screen mode.
shouldUpdateAltScreen := (s.lastView == nil && view.AltScreen) || (s.lastView != nil && s.lastView.AltScreen != view.AltScreen)
if shouldUpdateAltScreen {
if shouldUpdateAltScreen && !starting {
// We want to enter/exit altscreen mode but defer writing the actual
// sequences until we flush the rest of the updates. This is because we
// control the cursor visibility and we need to ensure that happens
// after entering/exiting alt screen mode. Some terminals have
// different cursor visibility states for main and alt screen modes and
// this ensures we handle that correctly.
// this ensures we handle that correctly. When the renderer was just
// restarted, start() already entered the alt screen if needed.
enableAltScreen(s, view.AltScreen, false)
}
@ -376,7 +384,7 @@ func (s *cursedRenderer) flush(closing bool) error {
}
// kitty keyboard protocol
if s.lastView == nil || view.KeyboardEnhancements != s.lastView.KeyboardEnhancements ||
if !s.kittyPushed || s.lastView == nil || view.KeyboardEnhancements != s.lastView.KeyboardEnhancements ||
view.AltScreen != s.lastView.AltScreen {
// NOTE: We need to reset the keyboard protocol when switching
// between main and alt screen. This is because the specs specify
@ -386,7 +394,20 @@ func (s *cursedRenderer) flush(closing bool) error {
_, _ = s.scr.WriteString(ansi.SetModifyOtherKeys2)
kittyFlags := keyboardEnhancementsFlags(view.KeyboardEnhancements)
_, _ = s.scr.WriteString(ansi.KittyKeyboard(kittyFlags, 1))
if s.lastView == nil || view.AltScreen != s.lastView.AltScreen {
// First render, renderer restart, or screen switch: the
// previous screen's entry (if any) is popped below, so push a
// fresh one for this screen.
_, _ = s.scr.WriteString(ansi.PushKittyKeyboard(kittyFlags))
s.kittyPushed = true
} else {
// Only the flags changed while the same screen stays active.
// Update the topmost stack entry in place instead of popping
// and re-pushing, so a keyboard change doesn't churn the
// stack. Note that this overwrites whatever entry is currently
// on top, which is normally ours.
_, _ = s.scr.WriteString(ansi.KittyKeyboard(kittyFlags, 1))
}
if !closing {
// Request keyboard enhancements when they change
_, _ = s.scr.WriteString(ansi.RequestKittyKeyboard)
@ -511,11 +532,17 @@ func (s *cursedRenderer) flush(closing bool) error {
var buf bytes.Buffer
if shouldUpdateAltScreen {
// We always disable keyboard enhancements when switching screens
// We always reset keyboard enhancements when switching screens
// because the terminal is expected to have two different keyboard
// registries for main and alt screens.
// registries for main and alt screens. modifyOtherKeys has no
// stack, so it is reset in place; the Kitty keyboard stack is
// popped, but only if we pushed an entry for the screen we're
// leaving (kittyPushed is false on the very first render).
_, _ = buf.WriteString(ansi.ResetModifyOtherKeys)
_, _ = buf.WriteString(ansi.KittyKeyboard(0, 1))
if s.kittyPushed {
_, _ = buf.WriteString(ansi.PopKittyKeyboard(1))
s.kittyPushed = false
}
if view.AltScreen {
// Entering alt screen mode.
buf.WriteString(ansi.SetModeAltScreenSaveCursor)

View file

@ -1,10 +1,14 @@
package tea
import (
"bytes"
"fmt"
"io"
"strings"
"testing"
"time"
"github.com/charmbracelet/x/ansi"
)
type mouseRaceModel struct {
@ -78,3 +82,118 @@ func TestCursedRenderer_mouseVsFlush(t *testing.T) {
t.Fatal("program did not exit after Quit")
}
}
func assertInOrder(t *testing.T, got string, wants ...string) {
t.Helper()
rest := got
for _, want := range wants {
idx := strings.Index(rest, want)
if idx < 0 {
t.Fatalf("expected %q to appear after the previous sequences in %q", want, got)
}
rest = rest[idx+len(want):]
}
}
func TestCursedRenderer_restoresKittyKeyboardStack(t *testing.T) {
t.Parallel()
var out bytes.Buffer
r := newCursedRenderer(&out, []string{"TERM=xterm-256color"}, 80, 24)
r.start()
view := NewView("hello")
view.KeyboardEnhancements.ReportEventTypes = true
pushMain := ansi.PushKittyKeyboard(keyboardEnhancementsFlags(view.KeyboardEnhancements))
pop := ansi.PopKittyKeyboard(1)
render := func(v View) {
t.Helper()
r.render(v)
if err := r.flush(false); err != nil {
t.Fatal(err)
}
}
render(view)
// Stop the renderer (as on suspend or ExecProcess) and start it again:
// close pops the stack entry, start pushes it back.
if err := r.close(); err != nil {
t.Fatal(err)
}
r.start()
// Enter and leave the alt screen. The terminal keeps a separate Kitty
// keyboard stack per screen, so each screen gets its own push and pop.
view.AltScreen = true
render(view)
view.AltScreen = false
render(view)
if err := r.close(); err != nil {
t.Fatal(err)
}
got := out.String()
// The flags are pushed once per screen activation: the first flush,
// the flush after the renderer was restarted, and when leaving the
// alt screen back to the main screen. Entering the alt screen pushes
// into the alt screen's own stack, which uses the default flags here.
// start() itself does not write to [out].
if n := strings.Count(got, pushMain); n != 3 {
t.Fatalf("expected kitty keyboard protocol to be pushed 3 times with %q (%d times), got %q", pushMain, n, got)
}
// One pop per stop/start cycle and per screen switch: closing pops the
// current screen's entry, and switching screens pops the entry of the
// screen being left.
if n := strings.Count(got, pop); n != 4 {
t.Fatalf("expected kitty keyboard protocol to be popped 4 times with %q (%d times), got %q", pop, n, got)
}
// Every pop must come after the push of the screen it belongs to, and
// the final pop must come last.
assertInOrder(t, got, pushMain, pop, pushMain, pop, pushMain, pop)
if strings.Contains(got, ansi.KittyKeyboard(0, 1)) {
t.Fatalf("expected kitty keyboard protocol not to be reset in-place with %q, got %q", ansi.KittyKeyboard(0, 1), got)
}
}
func TestCursedRenderer_updatesKittyKeyboardFlagsInPlace(t *testing.T) {
t.Parallel()
var out bytes.Buffer
r := newCursedRenderer(&out, []string{"TERM=xterm-256color"}, 80, 24)
render := func(v View) {
t.Helper()
r.render(v)
if err := r.flush(false); err != nil {
t.Fatal(err)
}
}
view := NewView("hello")
render(view)
// Changing the enhancement flags without switching screens updates the
// current stack entry in place instead of pushing a new one.
changed := view
changed.KeyboardEnhancements.ReportEventTypes = true
render(changed)
wantUpdate := ansi.KittyKeyboard(keyboardEnhancementsFlags(changed.KeyboardEnhancements), 1)
got := out.String()
if !strings.Contains(got, wantUpdate) {
t.Fatalf("expected kitty keyboard flags to be updated in place with %q, got %q", wantUpdate, got)
}
assertInOrder(t, got,
ansi.PushKittyKeyboard(keyboardEnhancementsFlags(view.KeyboardEnhancements)),
wantUpdate,
)
if strings.Contains(got, ansi.PopKittyKeyboard(1)) {
t.Fatalf("expected kitty keyboard protocol not to be popped with %q, got %q", ansi.PopKittyKeyboard(1), got)
}
if n := strings.Count(got, ansi.PushKittyKeyboard(0)); n > 1 {
t.Fatalf("expected kitty keyboard protocol to be pushed once, got %d pushes in %q", n, got)
}
}

View file

@ -1 +1 @@
[?25l[?2004h[>4;2m[=1;1u success[>4m[=0;1u [?25h[?2004l[?2026$p[?2027$p]10;?]11;?]12;?
[?25l[?2004h[>4;2m[>1u success[>4m[<1u [?25h[?2004l[?2026$p[?2027$p]10;?]11;?]12;?

View file

@ -1 +1 @@
[?25l [?2004h[>4;2m[=1;1usuccess[>4m[=0;1u [?25h[?2004l[?2026$p[?2027$p
[?25l [?2004h[>4;2m[>1usuccess[>4m[<1u [?25h[?2004l[?2026$p[?2027$p

View file

@ -1 +1 @@
[?25l[?2004h[>4;2m[=1;1u success[>4m[=0;1u [?25h[?2004l[?2026$p[?2027$p]52;c;?]52;c;c3VjY2Vzcw==
[?25l[?2004h[>4;2m[>1u success[>4m[<1u [?25h[?2004l[?2026$p[?2027$p]52;c;?]52;c;c3VjY2Vzcw==

View file

@ -1 +1 @@
[?25l[?2004h[>4;2m[=1;1u success[>4m[=0;1u [?25h[?2004l[?2026$p[?2027$p
[?25l[?2004h[>4;2m[>1u success[>4m[<1u [?25h[?2004l[?2026$p[?2027$p

View file

@ -1 +1,24 @@
[>4m[=0;1u[?1049h[?25l[?2004h[>4;2m[=1;1usuccess[>4m[=0;1u[?1049l[?25h[?2004l[?2026$p[?2027$p
[>4m[<1u[?1049h[?25l[?2004h[>4;2m[>1u success[>4m[<1u
[?1049l[?25h[?2004l[?2026$p[?2027$p

View file

@ -1 +1 @@
[?25l[?2004h[>4;2m[=1;1u]11;#ffffff success[>4m[=0;1u [?25h[?2004l]111[?2026$p[?2027$p
[?25l[?2004h[>4;2m[>1u]11;#ffffff success[>4m[<1u [?25h[?2004l]111[?2026$p[?2027$p

View file

@ -1 +1 @@
[?25l[?2004h[>4;2m[=1;1u success[>4m[=0;1u [?25h[?2004l[?2026$p[?2027$p
[?25l[?2004h[>4;2m[>1u success[>4m[<1u [?25h[?2004l[?2026$p[?2027$p

View file

@ -1 +1 @@
[?25l[?2004h[>4;2m[=1;1u success[>4m[=0;1u [?25h[?2004l[?2026$p[?2027$p
[?25l[?2004h[>4;2m[>1u success[>4m[<1u [?25h[?2004l[?2026$p[?2027$p

View file

@ -1 +1 @@
[?2004h[>4;2m[=1;1u[1 q success [?25h[>4m[=0;1u[?2004l[?2026$p[?2027$p
[?2004h[>4;2m[>1u[1 q success [?25h[>4m[<1u[?2004l[?2026$p[?2027$p

View file

@ -1 +1 @@
[?25l[?2004h[>4;2m[=3;1u success[>4m[=0;1u [?25h[?2004l[?2026$p[?2027$p
[?25l[?2004h[>4;2m[>3u success[>4m[<1u [?25h[?2004l[?2026$p[?2027$p

View file

@ -1 +1 @@
[?25l[?2004h[?1003h[?1006h[>4;2m[=1;1u success[>4m[=0;1u [?25h[?2004l[?1002l[?1003l[?1006l[?2026$p[?2027$p
[?25l[?2004h[?1003h[?1006h[>4;2m[>1u success[>4m[<1u [?25h[?2004l[?1002l[?1003l[?1006l[?2026$p[?2027$p

View file

@ -1 +1 @@
[?25l[?2004h[?1002h[?1006h[>4;2m[=1;1u success[>4m[=0;1u [?25h[?2004l[?1002l[?1003l[?1006l[?2026$p[?2027$p
[?25l[?2004h[?1002h[?1006h[>4;2m[>1u success[>4m[<1u [?25h[?2004l[?1002l[?1003l[?1006l[?2026$p[?2027$p

View file

@ -1 +1 @@
[?25l[?2004h[>4;2m[=1;1u success[>4m[=0;1u [?25h[?2004l[?2026$p[?2027$p
[?25l[?2004h[>4;2m[>1u success[>4m[<1u [?25h[?2004l[?2026$p[?2027$p