Skip to content

Commit 97a0bdc

Browse files
committed
fix(renderer): reset kitty keyboard protocol on alt screen switch
We need to reset the kitty keyboard protocol when switching between main and alt screens. This is because kitty maintains separate keyboard protocol registries for each screen, and failing to reset the protocol can lead to issues such as the inability to use the Ctrl key in some editor that don't support kitty's keyboard protocol.
1 parent 5fa38b7 commit 97a0bdc

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

cursed_renderer.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ func (s *cursedRenderer) close() (err error) {
144144
// so that we can restore them when we start the renderer again. This is
145145
// used when the user suspends the program and then resumes it.
146146
if lv := s.lastView; lv != nil { //nolint:nestif
147+
// NOTE: The Kitty keyboard specs specify that the terminal should have
148+
// two registries for the main and alt screens. We disable keyboard
149+
// enhancements whenever we enter/exit alt screen mode in
150+
// [cursedRenderer.flush].
151+
// Here, we reset the keyboard protocol of the last screen used
152+
// assuming the other screen is already reset when we switched screens.
153+
_, _ = s.buf.WriteString(ansi.KittyKeyboard(0, 1))
154+
147155
if lv.AltScreen {
148156
enableAltScreen(s, false, true)
149157
} else {
@@ -194,9 +202,6 @@ func (s *cursedRenderer) close() (err error) {
194202
if lv.ProgressBar != nil && lv.ProgressBar.State != ProgressBarNone {
195203
_, _ = s.scr.WriteString(ansi.ResetProgressBar)
196204
}
197-
198-
// NOTE: This needs to happen after we exit the alt screen.
199-
_, _ = s.scr.WriteString(ansi.KittyKeyboard(0, 1))
200205
}
201206

202207
if err := s.scr.Flush(); err != nil {
@@ -493,6 +498,10 @@ func (s *cursedRenderer) flush(closing bool) error {
493498

494499
var buf bytes.Buffer
495500
if shouldUpdateAltScreen {
501+
// We always disable keyboard enhancements when switching screens
502+
// because the terminal is expected to have two different keyboard
503+
// registries for main and alt screens.
504+
_, _ = buf.WriteString(ansi.KittyKeyboard(0, 1))
496505
if view.AltScreen {
497506
// Entering alt screen mode.
498507
buf.WriteString(ansi.SetModeAltScreenSaveCursor)

0 commit comments

Comments
 (0)