fix: restore kitty keyboard stack on exit (#1750)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by deepakganesh78 · committed by GitHub · parent 930e18c · Browse files · 16 files·+199 added−29 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: drew <me@andrinoff.com>
- Signed-off-by: drew <me@andrinoff.com>
Diff stats
16 files+199 added−29 removed
- other:+199 added−29 removed
Provenance
- Commit
0d3e281- Tree
5831a2e- Author
deepakganesh78 <40065669+deepakganesh78@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
930e18c
16 files changed+199 added−29 removed
modified:testdata/TestClearMsg/bg_fg_cur_color.golden+1 added-1 removed
1-[?25l[?2004h[>4;2m[=1;1u
[Jsuccess[>4m[=0;1u
[J[?25h[?2004l[?2026$p[?2027$p]10;?]11;?]12;?1+[?25l[?2004h[>4;2m[>1u
[Jsuccess[>4m[<1u
[J[?25h[?2004l[?2026$p[?2027$p]10;?]11;?]12;?modified:testdata/TestClearMsg/clear_screen.golden+1 added-1 removed
1-[?25l
[?2004h[>4;2m[=1;1u[Jsuccess[>4m[=0;1u
[J[?25h[?2004l[?2026$p[?2027$p1+[?25l
[?2004h[>4;2m[>1u[Jsuccess[>4m[<1u
[J[?25h[?2004l[?2026$p[?2027$pmodified:testdata/TestClearMsg/read_set_clipboard.golden+1 added-1 removed
1-[?25l[?2004h[>4;2m[=1;1u
[Jsuccess[>4m[=0;1u
[J[?25h[?2004l[?2026$p[?2027$p]52;c;?]52;c;c3VjY2Vzcw==1+[?25l[?2004h[>4;2m[>1u
[Jsuccess[>4m[<1u
[J[?25h[?2004l[?2026$p[?2027$p]52;c;?]52;c;c3VjY2Vzcw==modified:testdata/TestViewModel/altscreen_autoexit.golden+1 added-1 removed
1-[>4m[=0;1u[?1049h[?25l[?2004h[>4;2m[=1;1u[H[2Jsuccess[>4m[=0;1u[24;1H[?1049l[?25h[?2004l[?2026$p[?2027$p1+[>4m[?1049h[?25l[?2004h[>4;2m[>1u[H[2Jsuccess[>4m[<1u[24;1H[?1049l[?25h[?2004l[?2026$p[?2027$pmodified:testdata/TestViewModel/altscreen.golden+1 added-1 removed
1-[?25l[?2004h[>4;2m[=1;1u
[Jsuccess[>4m[=0;1u
[J[?25h[?2004l[?2026$p[?2027$p1+[?25l[?2004h[>4;2m[>1u
[Jsuccess[>4m[<1u
[J[?25h[?2004l[?2026$p[?2027$pmodified:testdata/TestViewModel/bg_set_color.golden+1 added-1 removed
1-[?25l[?2004h[>4;2m[=1;1u]11;#ffffff
[Jsuccess[>4m[=0;1u
[J[?25h[?2004l]111[?2026$p[?2027$p1+[?25l[?2004h[>4;2m[>1u]11;#ffffff
[Jsuccess[>4m[<1u
[J[?25h[?2004l]111[?2026$p[?2027$pmodified:testdata/TestViewModel/bp_stop_start.golden+1 added-1 removed
1-[?25l[?2004h[>4;2m[=1;1u
[Jsuccess[>4m[=0;1u
[J[?25h[?2004l[?2026$p[?2027$p1+[?25l[?2004h[>4;2m[>1u
[Jsuccess[>4m[<1u
[J[?25h[?2004l[?2026$p[?2027$pmodified:testdata/TestViewModel/cursor_hide.golden+1 added-1 removed
1-[?25l[?2004h[>4;2m[=1;1u
[Jsuccess[>4m[=0;1u
[J[?25h[?2004l[?2026$p[?2027$p1+[?25l[?2004h[>4;2m[>1u
[Jsuccess[>4m[<1u
[J[?25h[?2004l[?2026$p[?2027$pmodified:testdata/TestViewModel/cursor_hideshow.golden+1 added-1 removed
1-[?2004h[>4;2m[=1;1u[1 q
[Jsuccess
[?25h[>4m[=0;1u[J[?2004l[?2026$p[?2027$p1+[?2004h[>4;2m[>1u[1 q
[Jsuccess
[?25h[>4m[<1u[J[?2004l[?2026$p[?2027$pmodified:testdata/TestViewModel/kitty_stop_startreleases.golden+1 added-1 removed
1-[?25l[?2004h[>4;2m[=3;1u
[Jsuccess[>4m[=0;1u
[J[?25h[?2004l[?2026$p[?2027$p1+[?25l[?2004h[>4;2m[>3u
[Jsuccess[>4m[<1u
[J[?25h[?2004l[?2026$p[?2027$pmodified:testdata/TestViewModel/mouse_allmotion.golden+1 added-1 removed
1-[?25l[?2004h[?1003h[?1006h[>4;2m[=1;1u
[Jsuccess[>4m[=0;1u
[J[?25h[?2004l[?1002l[?1003l[?1006l[?2026$p[?2027$p1+[?25l[?2004h[?1003h[?1006h[>4;2m[>1u
[Jsuccess[>4m[<1u
[J[?25h[?2004l[?1002l[?1003l[?1006l[?2026$p[?2027$pmodified:testdata/TestViewModel/mouse_cellmotion.golden+1 added-1 removed
1-[?25l[?2004h[?1002h[?1006h[>4;2m[=1;1u
[Jsuccess[>4m[=0;1u
[J[?25h[?2004l[?1002l[?1003l[?1006l[?2026$p[?2027$p1+[?25l[?2004h[?1002h[?1006h[>4;2m[>1u
[Jsuccess[>4m[<1u
[J[?25h[?2004l[?1002l[?1003l[?1006l[?2026$p[?2027$pmodified:testdata/TestViewModel/mouse_disable.golden+1 added-1 removed
1-[?25l[?2004h[>4;2m[=1;1u
[Jsuccess[>4m[=0;1u
[J[?25h[?2004l[?2026$p[?2027$p1+[?25l[?2004h[>4;2m[>1u
[Jsuccess[>4m[<1u
[J[?25h[?2004l[?2026$p[?2027$pmodified:cursed_renderer_test.go+123 added
1 package tea2 3 import (4+ "bytes"5 "fmt"6 "io"7+ "strings"8 "testing"9 "time"10+11+ "github.com/charmbracelet/x/ansi"12 )13 14 type mouseRaceModel struct {15 i int65 unmodified lines81 case <-time.After(5 * time.Second):82 t.Fatal("program did not exit after Quit")83 }84 }85+86+func assertInOrder(t *testing.T, got string, wants ...string) {87+ t.Helper()88+ rest := got89+ for _, want := range wants {90+ idx := strings.Index(rest, want)91+ if idx < 0 {92+ t.Fatalf("expected %q to appear after the previous sequences in %q", want, got)93+ }94+ rest = rest[idx+len(want):]95+ }96+}97+98+func TestCursedRenderer_restoresKittyKeyboardStack(t *testing.T) {99+ t.Parallel()100+101+ var out bytes.Buffer102+ r := newCursedRenderer(&out, []string{"TERM=xterm-256color"}, 80, 24)103+ r.start()104+105+ view := NewView("hello")106+ view.KeyboardEnhancements.ReportEventTypes = true107+ pushMain := ansi.PushKittyKeyboard(keyboardEnhancementsFlags(view.KeyboardEnhancements))108+ pop := ansi.PopKittyKeyboard(1)109+110+ render := func(v View) {111+ t.Helper()112+ r.render(v)113+ if err := r.flush(false); err != nil {114+ t.Fatal(err)115+ }116+ }117+118+ render(view)119+120+ // Stop the renderer (as on suspend or ExecProcess) and start it again:121+ // close pops the stack entry, start pushes it back.122+ if err := r.close(); err != nil {123+ t.Fatal(err)124+ }125+ r.start()126+127+ // Enter and leave the alt screen. The terminal keeps a separate Kitty128+ // keyboard stack per screen, so each screen gets its own push and pop.129+ view.AltScreen = true130+ render(view)131+ view.AltScreen = false132+ render(view)133+134+ if err := r.close(); err != nil {135+ t.Fatal(err)136+ }137+138+ got := out.String()139+ // The flags are pushed once per screen activation: the first flush,140+ // the flush after the renderer was restarted, and on each screen141+ // switch. start() itself does not write to [out].142+ if n := strings.Count(got, pushMain); n != 4 {143+ t.Fatalf("expected kitty keyboard protocol to be pushed 4 times with %q (%d times), got %q", pushMain, n, got)144+ }145+ // One pop per stop/start cycle and per screen switch: closing pops the146+ // current screen's entry, and switching screens pops the entry of the147+ // screen being left.148+ if n := strings.Count(got, pop); n != 4 {149+ t.Fatalf("expected kitty keyboard protocol to be popped 4 times with %q (%d times), got %q", pop, n, got)150+ }151+ // Every pop must come after a push: the stack is balanced when pushes152+ // and pops alternate. The resumed flush pushes twice in a row (once in153+ // start(), once in flush()), and both entries are popped afterwards.154+ assertInOrder(t, got,155+ pushMain, pop, // close pops the entry pushed by the first flush156+ pop, // entering the alt screen pops the resumed entry157+ pushMain, pop, // leaving the alt screen158+ pushMain, pop, // the resumed main screen entry and the final close159+ )160+ if strings.Contains(got, ansi.KittyKeyboard(0, 1)) {161+ t.Fatalf("expected kitty keyboard protocol not to be reset in-place with %q, got %q", ansi.KittyKeyboard(0, 1), got)162+ }163+}164+165+func TestCursedRenderer_updatesKittyKeyboardFlagsInPlace(t *testing.T) {166+ t.Parallel()167+168+ var out bytes.Buffer169+ r := newCursedRenderer(&out, []string{"TERM=xterm-256color"}, 80, 24)170+171+ render := func(v View) {172+ t.Helper()173+ r.render(v)174+ if err := r.flush(false); err != nil {175+ t.Fatal(err)176+ }177+ }178+179+ view := NewView("hello")180+ render(view)181+182+ // Changing the enhancement flags without switching screens updates the183+ // current stack entry in place instead of pushing a new one.184+ changed := view185+ changed.KeyboardEnhancements.ReportEventTypes = true186+ render(changed)187+188+ wantUpdate := ansi.KittyKeyboard(keyboardEnhancementsFlags(changed.KeyboardEnhancements), 1)189+ got := out.String()190+ if !strings.Contains(got, wantUpdate) {191+ t.Fatalf("expected kitty keyboard flags to be updated in place with %q, got %q", wantUpdate, got)192+ }193+ assertInOrder(t, got,194+ ansi.PushKittyKeyboard(keyboardEnhancementsFlags(view.KeyboardEnhancements)),195+ wantUpdate,196+ )197+ if strings.Contains(got, ansi.PopKittyKeyboard(1)) {198+ t.Fatalf("expected kitty keyboard protocol not to be popped with %q, got %q", ansi.PopKittyKeyboard(1), got)199+ }200+ if n := strings.Count(got, ansi.PushKittyKeyboard(0)); n > 1 {201+ t.Fatalf("expected kitty keyboard protocol to be pushed once, got %d pushes in %q", n, got)202+ }203+}modified:cursed_renderer.go+62 added-16 removed
32 unmodified lines33 mapnl bool34 syncdUpdates bool // whether to use synchronized output mode for updates35 starting bool // indicates whether the renderer is starting after being stopped36 pendingErase bool // an scr.Erase() is pending and hasn't been drained by flush yet37+ noInput bool // whether input is disabled, in which case keyboard enhancement queries are pointless38 }39 40 var _ renderer = &cursedRenderer{}41 14 unmodified lines56 s.logger = logger57 s.mu.Unlock()58 }59 60+// setNoInput disables keyboard enhancement requests. When the program runs61+// without input, the terminal's response to a keyboard enhancement query62+// would arrive after the program has exited and leak into the shell.63+func (s *cursedRenderer) setNoInput(noInput bool) {64+ s.noInput = noInput65+}66+67+// resetKeyboardEnhancements writes the sequences that reset keyboard68+// enhancement protocols when switching between the main and alt screens.69+// modifyOtherKeys has no stack, so it is reset in place; the Kitty keyboard70+// stack is popped, but only if we previously pushed an entry (i.e. this is71+// not the first render). With input disabled the keyboard protocol is never72+// touched.73+func (s *cursedRenderer) resetKeyboardEnhancements(buf *bytes.Buffer) {74+ if s.noInput {75+ return76+ }77+ _, _ = buf.WriteString(ansi.ResetModifyOtherKeys)78+ if s.lastView != nil {79+ _, _ = buf.WriteString(ansi.PopKittyKeyboard(1))80+ }81+}82+83 // setOptimizations sets the cursor movement optimizations.84 func (s *cursedRenderer) setOptimizations(hardTabs, backspace, mapnl bool) {85 s.mu.Lock()86 s.hardTabs = hardTabs68 unmodified lines155 }156 if s.lastView.ProgressBar != nil {157 setProgressBar(s, s.lastView.ProgressBar)158 }159+ if !s.noInput {135- // Enable modifyOtherKeys and Kitty keyboard protocol.136- // Both can coexist; terminals ignore what they don't support.137- _, _ = s.scr.WriteString(ansi.SetModifyOtherKeys2)160+ // Enable modifyOtherKeys and Kitty keyboard protocol.161+ // Both can coexist; terminals ignore what they don't support.162+ _, _ = s.scr.WriteString(ansi.SetModifyOtherKeys2)163 139- kittyFlags := keyboardEnhancementsFlags(s.lastView.KeyboardEnhancements)140- _, _ = s.scr.WriteString(ansi.KittyKeyboard(kittyFlags, 1))164+ kittyFlags := keyboardEnhancementsFlags(s.lastView.KeyboardEnhancements)165+ // The entry was popped when the renderer was stopped, so push a fresh166+ // one for the screen we're about to restore.167+ _, _ = s.scr.WriteString(ansi.PushKittyKeyboard(kittyFlags))168+ }169 }170 171 // close implements renderer.172 func (s *cursedRenderer) close() (err error) {8 unmodified lines181 // NOTE: The Kitty keyboard specs specify that the terminal should have182 // two registries for the main and alt screens. We disable keyboard183 // enhancements whenever we enter/exit alt screen mode in184 // [cursedRenderer.flush].157- // Here, we reset the keyboard protocol of the last screen used158- // assuming the other screen is already reset when we switched screens.159- _, _ = s.buf.WriteString(ansi.ResetModifyOtherKeys)160- _, _ = s.buf.WriteString(ansi.KittyKeyboard(0, 1))185+ // Here, we pop the keyboard protocol of the last screen used186+ // assuming the other screen is already popped when we switched187+ // screens. With input disabled we never pushed an entry, so there is188+ // nothing to pop.189+ if !s.noInput {190+ _, _ = s.buf.WriteString(ansi.ResetModifyOtherKeys)191+ _, _ = s.buf.WriteString(ansi.PopKittyKeyboard(1))192+ }193 194 // Go to the bottom of the screen.195 // We need to go to the bottom of the screen regardless of whether196 // we're in alt screen mode or not to avoid leaving the cursor in the211 unmodified lines408 _, _ = s.scr.WriteString(ansi.SetWindowTitle(view.WindowTitle))409 }410 }411 412+ // kitty keyboard protocol. Skipped entirely when input is disabled: the413+ // enhancements only affect keyboard input, and querying the terminal414+ // would leave its response unconsumed, leaking into the shell after380- // kitty keyboard protocol381- if s.lastView == nil || view.KeyboardEnhancements != s.lastView.KeyboardEnhancements ||382- view.AltScreen != s.lastView.AltScreen {415+ // the program exits.416+ if !s.noInput && (s.lastView == nil || view.KeyboardEnhancements != s.lastView.KeyboardEnhancements ||417+ view.AltScreen != s.lastView.AltScreen) {418 // NOTE: We need to reset the keyboard protocol when switching419 // between main and alt screen. This is because the specs specify420 // two different states for the main and alt screen.421 422 // Enable modifyOtherKeys and Kitty keyboard protocol.423 _, _ = s.scr.WriteString(ansi.SetModifyOtherKeys2)424 425 kittyFlags := keyboardEnhancementsFlags(view.KeyboardEnhancements)426+ if s.lastView == nil || view.AltScreen != s.lastView.AltScreen {427+ // First render or screen switch: the previous screen's entry428+ // (if any) is popped below, so push a fresh one for this429+ // screen.430+ _, _ = s.scr.WriteString(ansi.PushKittyKeyboard(kittyFlags))431+ } else {432+ // Only the flags changed while the same screen stays active.433+ // Update the topmost stack entry in place instead of popping434+ // and re-pushing, so a keyboard change doesn't churn the435+ // stack. Note that this overwrites whatever entry is currently436+ // on top, which is normally ours.391- _, _ = s.scr.WriteString(ansi.KittyKeyboard(kittyFlags, 1))437+ _, _ = s.scr.WriteString(ansi.KittyKeyboard(kittyFlags, 1))438+ }439 if !closing {440 // Request keyboard enhancements when they change441 _, _ = s.scr.WriteString(ansi.RequestKittyKeyboard)442 }116 unmodified lines559 // of showing the cursor flying around the screen during updates.560 561 var buf bytes.Buffer562 if shouldUpdateAltScreen {516- // We always disable keyboard enhancements when switching screens563+ // We always reset keyboard enhancements when switching screens564 // because the terminal is expected to have two different keyboard565 // registries for main and alt screens.519- _, _ = buf.WriteString(ansi.ResetModifyOtherKeys)520- _, _ = buf.WriteString(ansi.KittyKeyboard(0, 1))566+ s.resetKeyboardEnhancements(&buf)567 if view.AltScreen {568 // Entering alt screen mode.569 buf.WriteString(ansi.SetModeAltScreenSaveCursor)570 } else {340 unmodified linesmodified:tea.go+1 added
1071 unmodified lines1072 p.width,1073 p.height,1074 )1075 r.setLogger(p.logger)1076+ r.setNoInput(p.disableInput)1077 // XXX: This breaks many things especially when we want the output1078 // to be compatible with terminals that are not necessary a TTY.1079 // This was originally done to work around a Wish emulated-pty1080 // issue where when a PTY session is detected, and we don't368 unmodified lines