Skip to content
Myr

feat: support extended keyboard enhancements (#1626)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This adds support for the Kitty keyboard protocol's extended enhancements, allowing applications to request that the terminal report additional information about key events, such as whether they're part of a repeat sequence, alternate key values, and associated text.

authored by Ayman Bagabas · committed by GitHub · parent ee06e98 · Browse files · 3 files+56 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Supersedes: https://github.com/charmbracelet/bubbletea/pull/1622
  • Fixes: https://github.com/charmbracelet/bubbletea/issues/1621
  • Fixes: https://github.com/charmbracelet/bubbletea/issues/1623

Diff stats

3 files+56 added−8 removed

  • other:+56 added−8 removed

3 files+56 added−8 removed

modified:cursed_renderer.go+19 added-8 removed
129 unmodified lines
130 // Enable modifyOtherKeys and Kitty keyboard protocol.131 // Both can coexist; terminals ignore what they don't support.132 _, _ = s.scr.WriteString(ansi.SetModifyOtherKeys2)133134 kittyFlags := ansi.KittyDisambiguateEscapeCodes135 if s.lastView.KeyboardEnhancements.ReportEventTypes {136 kittyFlags |= ansi.KittyReportEventTypes137 }134 kittyFlags := keyboardEnhancementsFlags(s.lastView.KeyboardEnhancements)135 _, _ = s.scr.WriteString(ansi.KittyKeyboard(kittyFlags, 1))136}137138// close implements renderer.
236 unmodified lines
375376 // Enable modifyOtherKeys and Kitty keyboard protocol.377 _, _ = s.scr.WriteString(ansi.SetModifyOtherKeys2)378382 kittyFlags := ansi.KittyDisambiguateEscapeCodes // always enable basic key disambiguation383 if view.KeyboardEnhancements.ReportEventTypes {384 kittyFlags |= ansi.KittyReportEventTypes385 }379 kittyFlags := keyboardEnhancementsFlags(view.KeyboardEnhancements)380 _, _ = s.scr.WriteString(ansi.KittyKeyboard(kittyFlags, 1))381 if !closing {382 // Request keyboard enhancements when they change383 _, _ = s.scr.WriteString(ansi.RequestKittyKeyboard)
437 unmodified lines
821 }822823 return true824}825826func keyboardEnhancementsFlags(ke KeyboardEnhancements) int {827 flags := 1 // always enable basic key disambiguation828 if ke.ReportEventTypes {829 flags |= ansi.KittyReportEventTypes830 }831 if ke.ReportAlternateKeys {832 flags |= ansi.KittyReportAlternateKeys833 }834 if ke.ReportAllKeysAsEscapeCodes {835 flags |= ansi.KittyReportAllKeysAsEscapeCodes836 }837 if ke.ReportAssociatedText {838 flags |= ansi.KittyReportAssociatedKeys839 }840 return flags841}
modified:keyboard.go+18 added
37 unmodified lines
38// different types of key events (press, release, and repeat).39func (k KeyboardEnhancementsMsg) SupportsEventTypes() bool {40 return k.Flags&ansi.KittyReportEventTypes != 041}4243// SupportsAlternateKeys returns whether the terminal supports reporting44// alternate key codes.45func (k KeyboardEnhancementsMsg) SupportsAlternateKeys() bool {46 return k.Flags&ansi.KittyReportAlternateKeys != 047}4849// SupportsAllKeysAsEscapeCodes returns whether the terminal supports reporting50// all keys as escape codes.51func (k KeyboardEnhancementsMsg) SupportsAllKeysAsEscapeCodes() bool {52 return k.Flags&ansi.KittyReportAllKeysAsEscapeCodes != 053}5455// SupportsAssociatedText returns whether the terminal supports reporting56// associated text with key events.57func (k KeyboardEnhancementsMsg) SupportsAssociatedText() bool {58 return k.Flags&ansi.KittyReportAssociatedKeys != 059}
modified:tea.go+19 added
243 unmodified lines
244 // If supported, your program will receive [KeyReleaseMsg]s and245 // [KeyPressMsg] with the [Key.IsRepeat] field set indicating that this is246 // a it's part of a key repeat sequence.247 ReportEventTypes bool248249 // ReportAlternateKeys requests the terminal to report alternate key values250 // in addition to the main ones.251 // Note that only key events represented as escape codes will affected by252 // this enhancement.253 ReportAlternateKeys bool254255 // ReportAllKeysAsEscapeCodes requests the terminal to report all key256 // events, including plain text keys, as escape codes.257 // When this is enabled, text won't be sent as plain text but instead as258 // escape codes that encode the key value and modifiers.259 ReportAllKeysAsEscapeCodes bool260261 // ReportAssociatedText requests the terminal to report the text associated262 // with key events.263 // Note that this is an enhancement to264 // [KeyboardEnhancements.ReportAllKeysAsEscapeCodes] and only has an effect265 // if that is enabled.266 ReportAssociatedText bool267}268269// SetContent is a helper method to set the content of a [View] with a styled270// string. A styled string represents text with styles and hyperlinks encoded
1167 unmodified lines