-
Notifications
You must be signed in to change notification settings - Fork 7
/
keybinding.go
185 lines (169 loc) · 5.14 KB
/
keybinding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package keybinding
import (
"fmt"
"github.com/jroimartin/gocui"
"strings"
"unicode"
)
var translate = map[string]string{
"/": "Slash",
"\\": "Backslash",
"[": "LsqBracket",
"]": "RsqBracket",
"_": "Underscore",
"escape": "Esc",
"~": "Tilde",
"pageup": "Pgup",
"pagedown": "Pgdn",
"pgup": "Pgup",
"pgdown": "Pgdn",
"up": "ArrowUp",
"down": "ArrowDown",
"right": "ArrowRight",
"left": "ArrowLeft",
"ctl": "Ctrl",
}
var display = map[string]string{
"Slash": "/",
"Backslash": "\\",
"LsqBracket": "[",
"RsqBracket": "]",
"Underscore": "_",
"Tilde": "~",
"Ctrl": "^",
}
var supportedKeybindings = map[string]gocui.Key{
"KeyF1": gocui.KeyF1,
"KeyF2": gocui.KeyF2,
"KeyF3": gocui.KeyF3,
"KeyF4": gocui.KeyF4,
"KeyF5": gocui.KeyF5,
"KeyF6": gocui.KeyF6,
"KeyF7": gocui.KeyF7,
"KeyF8": gocui.KeyF8,
"KeyF9": gocui.KeyF9,
"KeyF10": gocui.KeyF10,
"KeyF11": gocui.KeyF11,
"KeyF12": gocui.KeyF12,
"KeyInsert": gocui.KeyInsert,
"KeyDelete": gocui.KeyDelete,
"KeyHome": gocui.KeyHome,
"KeyEnd": gocui.KeyEnd,
"KeyPgup": gocui.KeyPgup,
"KeyPgdn": gocui.KeyPgdn,
"KeyArrowUp": gocui.KeyArrowUp,
"KeyArrowDown": gocui.KeyArrowDown,
"KeyArrowLeft": gocui.KeyArrowLeft,
"KeyArrowRight": gocui.KeyArrowRight,
"KeyCtrlTilde": gocui.KeyCtrlTilde,
"KeyCtrl2": gocui.KeyCtrl2,
"KeyCtrlSpace": gocui.KeyCtrlSpace,
"KeyCtrlA": gocui.KeyCtrlA,
"KeyCtrlB": gocui.KeyCtrlB,
"KeyCtrlC": gocui.KeyCtrlC,
"KeyCtrlD": gocui.KeyCtrlD,
"KeyCtrlE": gocui.KeyCtrlE,
"KeyCtrlF": gocui.KeyCtrlF,
"KeyCtrlG": gocui.KeyCtrlG,
"KeyBackspace": gocui.KeyBackspace,
"KeyCtrlH": gocui.KeyCtrlH,
"KeyTab": gocui.KeyTab,
"KeyCtrlI": gocui.KeyCtrlI,
"KeyCtrlJ": gocui.KeyCtrlJ,
"KeyCtrlK": gocui.KeyCtrlK,
"KeyCtrlL": gocui.KeyCtrlL,
"KeyEnter": gocui.KeyEnter,
"KeyCtrlM": gocui.KeyCtrlM,
"KeyCtrlN": gocui.KeyCtrlN,
"KeyCtrlO": gocui.KeyCtrlO,
"KeyCtrlP": gocui.KeyCtrlP,
"KeyCtrlQ": gocui.KeyCtrlQ,
"KeyCtrlR": gocui.KeyCtrlR,
"KeyCtrlS": gocui.KeyCtrlS,
"KeyCtrlT": gocui.KeyCtrlT,
"KeyCtrlU": gocui.KeyCtrlU,
"KeyCtrlV": gocui.KeyCtrlV,
"KeyCtrlW": gocui.KeyCtrlW,
"KeyCtrlX": gocui.KeyCtrlX,
"KeyCtrlY": gocui.KeyCtrlY,
"KeyCtrlZ": gocui.KeyCtrlZ,
"KeyEsc": gocui.KeyEsc,
"KeyCtrlLsqBracket": gocui.KeyCtrlLsqBracket,
"KeyCtrl3": gocui.KeyCtrl3,
"KeyCtrl4": gocui.KeyCtrl4,
"KeyCtrlBackslash": gocui.KeyCtrlBackslash,
"KeyCtrl5": gocui.KeyCtrl5,
"KeyCtrlRsqBracket": gocui.KeyCtrlRsqBracket,
"KeyCtrl6": gocui.KeyCtrl6,
"KeyCtrl7": gocui.KeyCtrl7,
"KeyCtrlSlash": gocui.KeyCtrlSlash,
"KeyCtrlUnderscore": gocui.KeyCtrlUnderscore,
"KeySpace": gocui.KeySpace,
"KeyBackspace2": gocui.KeyBackspace2,
"KeyCtrl8": gocui.KeyCtrl8,
}
type Key struct {
Value gocui.Key
Modifier gocui.Modifier
Tokens []string
}
func Parse(input string) (Key, error) {
f := func(c rune) bool { return unicode.IsSpace(c) || c == '+' }
tokens := strings.FieldsFunc(input, f)
var normalizedTokens []string
var modifier = gocui.ModNone
for _, token := range tokens {
normalized := strings.ToLower(token)
if value, exists := translate[normalized]; exists {
normalized = value
} else {
normalized = strings.Title(normalized)
}
if normalized == "Alt" {
modifier = gocui.ModAlt
continue
}
if len(normalized) == 1 {
normalizedTokens = append(normalizedTokens, strings.ToUpper(normalized))
continue
}
normalizedTokens = append(normalizedTokens, normalized)
}
lookup := "Key" + strings.Join(normalizedTokens, "")
if key, exists := supportedKeybindings[lookup]; exists {
return Key{key, modifier, normalizedTokens}, nil
}
if modifier != gocui.ModNone {
return Key{0, modifier, normalizedTokens}, fmt.Errorf("unsupported keybinding: %s (+%+v)", lookup, modifier)
}
return Key{0, modifier, normalizedTokens}, fmt.Errorf("unsupported keybinding: %s", lookup)
}
func ParseAll(input string) ([]Key, error) {
ret := make([]Key, 0)
for _, value := range strings.Split(input, ",") {
key, err := Parse(value)
if err != nil {
return nil, fmt.Errorf("could not parse keybinding '%s' from request '%s': %+v", value, input, err)
}
ret = append(ret, key)
}
if len(ret) == 0 {
return nil, fmt.Errorf("must have at least one keybinding")
}
return ret, nil
}
func (key Key) String() string {
displayTokens := make([]string, 0)
prefix := ""
for _, token := range key.Tokens {
if token == "Ctrl" {
prefix = "^"
continue
}
if value, exists := display[token]; exists {
token = value
}
displayTokens = append(displayTokens, token)
}
return prefix + strings.Join(displayTokens, "+")
}