forked from wagoodman/keybinding
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkeybinding.go
232 lines (210 loc) · 6.44 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package keybinding
import (
"fmt"
"strings"
"unicode"
"github.com/awesome-gocui/gocui"
)
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,
"MouseLeft": gocui.MouseLeft,
"MouseMiddle": gocui.MouseMiddle,
"MouseRight": gocui.MouseRight,
"MouseRelease": gocui.MouseRelease,
"MouseWheelUp": gocui.MouseWheelUp,
"MouseWheelDown": gocui.MouseWheelDown,
}
// Key contains all relevant information about the key
type Key struct {
Value interface{}
Modifier gocui.Modifier
Tokens []string
}
// Parse turns the input string into an actual Key.
// Returns an error when something goes wrong
func Parse(input string) (Key, error) {
f := func(c rune) bool { return unicode.IsSpace(c) || c == '+' }
tokens := strings.FieldsFunc(input, f)
if len(tokens) == 1 && len(tokens[0]) == 1 {
single := rune(tokens[0][0])
if unicode.IsLetter(single) {
return Key{single, gocui.ModNone, tokens}, nil
}
}
var normalizedTokens []string
var modifier = gocui.ModNone
for _, token := range tokens {
normalized := 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 := strings.Join(normalizedTokens, "")
if !strings.Contains(lookup, "Mouse") {
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)
}
// ParseAll parses all strings to a Key.
// Returns an error when something goes wrong.
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
}
// MustParse parses the input string to a key but instead an
// error, it panics when things go wrong.
// This forces the caller to react to an error
func MustParse(input string) Key {
if key, err := Parse(input); err != nil {
panic(err)
} else {
return key
}
}
// MustParseAll parses all the input strings to a key but instead an
// error, it panics when things go wrong.
// This forces the caller to react to an error
func MustParseAll(input string) []Key {
if key, err := ParseAll(input); err != nil {
panic(err)
} else {
return key
}
}
// String returns the Key in string form
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, "+")
}