// Package keymap translates user-facing key notation to Bubble Tea key names. package keymap import ( "fmt" "strings" "unicode" "unicode/utf8" ) const maxNotationBytes = 64 var namedKeys = map[string]string{ "space": "space", "ret": "enter", "esc": "esc", "tab": "tab", "backspace": "backspace", "delete": "delete", "insert": "insert", "home": "home", "end": "end", "pageup": "pgup", "pagedown": "pgdown", "up": "up", "down": "down", "left": "left", "right": "right", } var displayKeys = map[string]string{ "enter": "ret", "pgup": "pageup", "pgdown": "pagedown", } // ParseKey parses one Helix-style keystroke into Bubble Tea's matching form. func ParseKey(notation string) (string, error) { if notation == "" || len(notation) > maxNotationBytes || !utf8.ValidString(notation) { return "", fmt.Errorf("invalid key notation %q", notation) } modifiers, rest, err := parseModifiers(notation) if err != nil { return "", err } key, err := parseBaseKey(rest) if err != nil { return "", err } if modifiers == modifierShift && utf8.RuneCountInString(key) == 1 { physical, _ := utf8.DecodeRuneInString(key) if unicode.ToLower(physical) == unicode.ToUpper(physical) { return "", fmt.Errorf("ambiguous shifted key %q: use the resulting literal character instead", notation) } return string(unicode.ToUpper(physical)), nil } if modifiers != 0 && utf8.RuneCountInString(key) == 1 { r, _ := utf8.DecodeRuneInString(key) if lower := unicode.ToLower(r); lower != r { modifiers |= modifierShift key = string(lower) } } var result strings.Builder if modifiers&modifierControl != 0 { result.WriteString("ctrl+") } if modifiers&modifierAlt != 0 { result.WriteString("alt+") } if modifiers&modifierShift != 0 { result.WriteString("shift+") } result.WriteString(key) return result.String(), nil } const ( modifierControl = 1 << iota modifierAlt modifierShift ) func parseModifiers(notation string) (int, string, error) { modifiers := 0 rest := notation for len(rest) >= 2 && rest[1] == '-' { modifier := 0 switch rest[0] { case 'C': modifier = modifierControl case 'A': modifier = modifierAlt case 'S': modifier = modifierShift default: return 0, "", fmt.Errorf("unknown modifier in %q", notation) } if modifiers&modifier != 0 { return 0, "", fmt.Errorf("duplicate modifier in %q", notation) } modifiers |= modifier rest = rest[2:] } return modifiers, rest, nil } func parseBaseKey(notation string) (string, error) { if key, exists := namedKeys[notation]; exists { return key, nil } if isFunctionKey(notation) { return notation, nil } if utf8.RuneCountInString(notation) == 1 { r, _ := utf8.DecodeRuneInString(notation) if unicode.IsPrint(r) && !unicode.IsSpace(r) { return notation, nil } } return "", fmt.Errorf("unknown key %q", notation) } func isFunctionKey(key string) bool { if len(key) < 2 || len(key) > 3 || key[0] != 'f' { return false } value := 0 for _, digit := range key[1:] { if digit < '0' || digit > '9' { return false } value = value*10 + int(digit-'0') } return value >= 1 && value <= 24 && key == fmt.Sprintf("f%d", value) } // FormatKey formats a Bubble Tea matching string as Helix-style notation. func FormatKey(key string) string { var result strings.Builder for _, modifier := range []struct { canonical string display string }{ {"ctrl+", "C-"}, {"alt+", "A-"}, {"shift+", "S-"}, } { if strings.HasPrefix(key, modifier.canonical) { result.WriteString(modifier.display) key = strings.TrimPrefix(key, modifier.canonical) } } if display, ok := displayKeys[key]; ok { key = display } result.WriteString(key) return result.String() }