-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey.go
More file actions
206 lines (183 loc) · 4.16 KB
/
Copy pathkey.go
File metadata and controls
206 lines (183 loc) · 4.16 KB
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
package gbind
import (
"errors"
"strings"
"github.com/gdamore/tcell/v3"
)
// Modifier labels
const (
LabelCtrl = "ctrl"
LabelAlt = "alt"
LabelMeta = "meta"
LabelShift = "shift"
)
// ErrInvalidKeyEvent is the error returned when encoding or decoding a key event fails.
var ErrInvalidKeyEvent = errors.New("invalid key event")
// UnifyEnterKeys is a flag that determines whether or not KPEnter (keypad
// enter) key events are interpreted as Enter key events. When enabled, Ctrl+J
// key events are also interpreted as Enter key events.
var UnifyEnterKeys = true
var fullKeyNames = map[string]string{
"backspace2": "Backspace",
"pgup": "PageUp",
"pgdn": "PageDown",
"esc": "Escape",
}
// Decode decodes a string as a key or combination of keys.
func Decode(s string) (mod tcell.ModMask, key tcell.Key, str string, err error) {
if len(s) == 0 {
return 0, 0, "", ErrInvalidKeyEvent
}
// Special case for plus rune decoding
if s[len(s)-1:] == "+" {
key = tcell.KeyRune
str = "+"
if len(s) == 1 {
return mod, key, str, nil
} else if len(s) == 2 {
return 0, 0, "", ErrInvalidKeyEvent
} else {
s = s[:len(s)-2]
}
}
split := strings.Split(s, "+")
DECODEPIECE:
for _, piece := range split {
// Decode modifiers
pieceLower := strings.ToLower(piece)
switch pieceLower {
case LabelCtrl:
mod |= tcell.ModCtrl
continue
case LabelAlt:
mod |= tcell.ModAlt
continue
case LabelMeta:
mod |= tcell.ModMeta
continue
case LabelShift:
mod |= tcell.ModShift
continue
}
// Decode key
for shortKey, fullKey := range fullKeyNames {
if pieceLower == strings.ToLower(fullKey) {
pieceLower = shortKey
break
}
}
switch pieceLower {
case "backspace":
key = tcell.KeyBackspace2
continue
case "space", "spacebar":
key = tcell.KeyRune
str = " "
continue
}
for k, keyName := range tcell.KeyNames {
if pieceLower == strings.ToLower(strings.ReplaceAll(keyName, "-", "+")) {
key = k
if key < 0x80 {
str = string(rune(k))
}
continue DECODEPIECE
}
}
// Decode rune
if len(piece) > 1 {
return 0, 0, "", ErrInvalidKeyEvent
}
key = tcell.KeyRune
str = string(rune(piece[0]))
}
// Normalize Ctrl+A-Z to lowercase
if mod&tcell.ModCtrl != 0 && key == tcell.KeyRune {
str = strings.ToLower(str)
}
return mod, key, str, nil
}
// Encode encodes a key or combination of keys a string.
func Encode(mod tcell.ModMask, key tcell.Key, str string) (string, error) {
var b strings.Builder
var wrote bool
if mod&tcell.ModCtrl != 0 {
if key == tcell.KeyBackspace || key == tcell.KeyTab || key == tcell.KeyEnter {
mod ^= tcell.ModCtrl
} else {
// Convert KeyCtrlA-Z to rune format.
if key >= tcell.KeyCtrlA && key <= tcell.KeyCtrlZ {
mod |= tcell.ModCtrl
str = string(rune('a' + (key - tcell.KeyCtrlA)))
key = tcell.KeyRune
}
}
}
if key != tcell.KeyRune {
if UnifyEnterKeys && key == tcell.KeyCtrlJ {
key = tcell.KeyEnter
} else if key < 0x80 {
str = string(rune(key))
}
}
// Encode modifiers
if mod&tcell.ModCtrl != 0 {
b.WriteString(upperFirst(LabelCtrl))
wrote = true
}
if mod&tcell.ModAlt != 0 {
if wrote {
b.WriteRune('+')
}
b.WriteString(upperFirst(LabelAlt))
wrote = true
}
if mod&tcell.ModMeta != 0 {
if wrote {
b.WriteRune('+')
}
b.WriteString(upperFirst(LabelMeta))
wrote = true
}
if mod&tcell.ModShift != 0 {
if wrote {
b.WriteRune('+')
}
b.WriteString(upperFirst(LabelShift))
wrote = true
}
if key == tcell.KeyRune && str == " " {
if wrote {
b.WriteRune('+')
}
b.WriteString("Space")
} else if key != tcell.KeyRune {
// Encode key
keyName := tcell.KeyNames[key]
if keyName == "" {
return "", ErrInvalidKeyEvent
}
keyName = strings.ReplaceAll(keyName, "-", "+")
fullKeyName := fullKeyNames[strings.ToLower(keyName)]
if fullKeyName != "" {
keyName = fullKeyName
}
if wrote {
b.WriteRune('+')
}
b.WriteString(keyName)
} else {
// Encode rune
if wrote {
b.WriteRune('+')
}
b.WriteString(str)
}
return b.String(), nil
}
func upperFirst(s string) string {
if len(s) <= 1 {
return strings.ToUpper(s)
}
return strings.ToUpper(s[:1]) + s[1:]
}