224 lines
6.2 KiB
Go
224 lines
6.2 KiB
Go
package keybind
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
// Key recording.
|
|
//
|
|
// While recording, the editor asks the terminal for the kitty keyboard
|
|
// protocol (flags 1|8: disambiguate escape codes, report all keys as escape
|
|
// codes). Ghostty then reports every key press, including ones with the
|
|
// super/cmd modifier, as "CSI code ; modifiers u" (or the legacy
|
|
// "CSI 1 ; modifiers A" forms for arrows and function keys). Terminals
|
|
// without the protocol send classic sequences, which are decoded from the
|
|
// key names Bubble Tea produces.
|
|
|
|
// PushKitty and PopKitty enable and restore the kitty keyboard protocol.
|
|
// QueryKitty asks the terminal for the current flags; a terminal that
|
|
// supports the protocol answers with "CSI ? flags u".
|
|
const (
|
|
PushKitty = "\x1b[>9u"
|
|
PopKitty = "\x1b[<u"
|
|
QueryKitty = "\x1b[?u"
|
|
)
|
|
|
|
// Combo is one recorded key combination.
|
|
type Combo struct {
|
|
Mods map[string]bool
|
|
Key string
|
|
Modifier bool // a lone modifier key press (ignored by the recorder)
|
|
}
|
|
|
|
func (c Combo) String() string {
|
|
return strings.Join(append(FormatMods(c.Mods), c.Key), "+")
|
|
}
|
|
|
|
// Plain reports whether the combination has no modifiers.
|
|
func (c Combo) Plain() bool {
|
|
for _, on := range c.Mods {
|
|
if on {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// IsKittyReply reports whether seq is the answer to QueryKitty.
|
|
func IsKittyReply(seq []byte) bool {
|
|
s := string(seq)
|
|
return strings.HasPrefix(s, "\x1b[?") && strings.HasSuffix(s, "u")
|
|
}
|
|
|
|
var kittyFunctional = map[int]string{
|
|
9: "tab", 13: "enter", 27: "escape", 32: "space", 127: "backspace",
|
|
57358: "caps_lock", 57359: "scroll_lock", 57360: "num_lock", 57361: "print_screen",
|
|
57362: "pause", 57363: "context_menu",
|
|
57409: "numpad_decimal", 57410: "numpad_divide", 57411: "numpad_multiply",
|
|
57412: "numpad_subtract", 57413: "numpad_add", 57414: "numpad_enter", 57415: "numpad_equal",
|
|
57416: "numpad_separator", 57417: "numpad_left", 57418: "numpad_right", 57419: "numpad_up",
|
|
57420: "numpad_down", 57421: "numpad_page_up", 57422: "numpad_page_down",
|
|
57423: "numpad_home", 57424: "numpad_end", 57425: "numpad_insert", 57426: "numpad_delete",
|
|
57427: "numpad_begin",
|
|
57430: "media_play_pause", 57432: "media_stop", 57435: "media_track_next",
|
|
57436: "media_track_previous", 57438: "audio_volume_down", 57439: "audio_volume_up",
|
|
57440: "audio_volume_mute",
|
|
}
|
|
|
|
var legacyFinal = map[byte]string{
|
|
'A': "arrow_up", 'B': "arrow_down", 'C': "arrow_right", 'D': "arrow_left",
|
|
'H': "home", 'F': "end", 'E': "numpad_begin", 'P': "f1", 'Q': "f2", 'R': "f3", 'S': "f4",
|
|
}
|
|
|
|
var tildeKeys = map[int]string{
|
|
2: "insert", 3: "delete", 5: "page_up", 6: "page_down", 7: "home", 8: "end",
|
|
11: "f1", 12: "f2", 13: "f3", 14: "f4", 15: "f5", 17: "f6", 18: "f7", 19: "f8",
|
|
20: "f9", 21: "f10", 23: "f11", 24: "f12", 29: "context_menu", 57427: "numpad_begin",
|
|
}
|
|
|
|
func init() {
|
|
for i := 0; i <= 9; i++ {
|
|
kittyFunctional[57399+i] = "numpad_" + strconv.Itoa(i)
|
|
}
|
|
for i := 13; i <= 25; i++ {
|
|
kittyFunctional[57376+i-13] = "f" + strconv.Itoa(i)
|
|
}
|
|
}
|
|
|
|
func decodeMods(field string) map[string]bool {
|
|
mods := map[string]bool{}
|
|
v, _, _ := strings.Cut(field, ":") // drop the event type
|
|
n, err := strconv.Atoi(v)
|
|
if err != nil || n < 1 {
|
|
return mods
|
|
}
|
|
n--
|
|
mods["shift"] = n&1 != 0
|
|
mods["alt"] = n&2 != 0
|
|
mods["ctrl"] = n&4 != 0
|
|
mods["super"] = n&8 != 0
|
|
return mods
|
|
}
|
|
|
|
// DecodeCSI decodes a key event escape sequence ("\x1b[...u", "\x1b[1;9A",
|
|
// "\x1b[5;3~", …). ok is false for anything that is not a key event.
|
|
func DecodeCSI(seq []byte) (Combo, bool) {
|
|
s := string(seq)
|
|
if !strings.HasPrefix(s, "\x1b[") || len(s) < 3 || IsKittyReply(seq) {
|
|
return Combo{}, false
|
|
}
|
|
final := s[len(s)-1]
|
|
params := strings.Split(s[2:len(s)-1], ";")
|
|
modField := ""
|
|
if len(params) > 1 {
|
|
modField = params[1]
|
|
}
|
|
c := Combo{Mods: decodeMods(modField)}
|
|
|
|
switch {
|
|
case final == 'u':
|
|
codeStr, _, _ := strings.Cut(params[0], ":") // drop shifted/base-layout keys
|
|
code, err := strconv.Atoi(codeStr)
|
|
if err != nil {
|
|
return Combo{}, false
|
|
}
|
|
if code >= 57441 && code <= 57452 {
|
|
c.Modifier = true // shift, ctrl, alt, super, hyper, meta, … keys
|
|
return c, true
|
|
}
|
|
if name, ok := kittyFunctional[code]; ok {
|
|
c.Key = name
|
|
if code == 57358 || code == 57360 {
|
|
c.Modifier = true // lock keys
|
|
}
|
|
return c, true
|
|
}
|
|
if code < 32 || !utf8.ValidRune(rune(code)) {
|
|
return Combo{}, false
|
|
}
|
|
c.Key = string(rune(code))
|
|
return c, true
|
|
case final == '~':
|
|
code, err := strconv.Atoi(params[0])
|
|
if err != nil {
|
|
return Combo{}, false
|
|
}
|
|
name, ok := tildeKeys[code]
|
|
if !ok {
|
|
return Combo{}, false
|
|
}
|
|
c.Key = name
|
|
return c, true
|
|
default:
|
|
name, ok := legacyFinal[final]
|
|
if !ok {
|
|
return Combo{}, false
|
|
}
|
|
c.Key = name
|
|
return c, true
|
|
}
|
|
}
|
|
|
|
var legacyNames = map[string]string{
|
|
"up": "arrow_up", "down": "arrow_down", "left": "arrow_left", "right": "arrow_right",
|
|
"pgup": "page_up", "pgdown": "page_down", "home": "home", "end": "end",
|
|
"insert": "insert", "delete": "delete", "tab": "tab", "enter": "enter",
|
|
"esc": "escape", "backspace": "backspace", " ": "space", "space": "space",
|
|
}
|
|
|
|
// DecodeKeyName converts a Bubble Tea key name ("ctrl+a", "alt+shift+up",
|
|
// "f5", "A") into a combination. Classic terminal input cannot represent
|
|
// super/cmd, and shifted symbols arrive as the symbol itself.
|
|
func DecodeKeyName(name string) (Combo, bool) {
|
|
c := Combo{Mods: map[string]bool{}}
|
|
if name == "" {
|
|
return c, false
|
|
}
|
|
switch name {
|
|
case "shift+tab":
|
|
c.Mods["shift"] = true
|
|
c.Key = "tab"
|
|
return c, true
|
|
case "ctrl+@", "ctrl+ ":
|
|
c.Mods["ctrl"] = true
|
|
c.Key = "space"
|
|
return c, true
|
|
}
|
|
rest := name
|
|
for {
|
|
found := false
|
|
for _, m := range []string{"ctrl+", "alt+", "shift+"} {
|
|
if strings.HasPrefix(rest, m) && len(rest) > len(m) {
|
|
c.Mods[strings.TrimSuffix(m, "+")] = true
|
|
rest = rest[len(m):]
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
break
|
|
}
|
|
}
|
|
if k, ok := legacyNames[rest]; ok {
|
|
c.Key = k
|
|
return c, true
|
|
}
|
|
if len(rest) >= 2 && rest[0] == 'f' {
|
|
if n, err := strconv.Atoi(rest[1:]); err == nil && n >= 1 && n <= 25 {
|
|
c.Key = rest
|
|
return c, true
|
|
}
|
|
}
|
|
if utf8.RuneCountInString(rest) == 1 {
|
|
r, _ := utf8.DecodeRuneInString(rest)
|
|
if r >= 'A' && r <= 'Z' {
|
|
c.Mods["shift"] = true
|
|
r += 'a' - 'A'
|
|
}
|
|
c.Key = string(r)
|
|
return c, true
|
|
}
|
|
return c, false
|
|
}
|