Debian Codegraph @

key.go

raw
1 package main
2
3 import (
4 "fmt"
5 "regexp"
6 "strings"
7
8 "github.com/gdamore/tcell/v3"
9 )
10
11 var gKeyVal = map[tcell.Key]string{
12 tcell.KeyEnter: "<enter>",
13 tcell.KeyBackspace: "<backspace>",
14 tcell.KeyTab: "<tab>",
15 tcell.KeyBacktab: "<backtab>",
16 tcell.KeyEsc: "<esc>",
17 tcell.KeyDelete: "<delete>",
18 tcell.KeyInsert: "<insert>",
19 tcell.KeyUp: "<up>",
20 tcell.KeyDown: "<down>",
21 tcell.KeyLeft: "<left>",
22 tcell.KeyRight: "<right>",
23 tcell.KeyHome: "<home>",
24 tcell.KeyEnd: "<end>",
25 tcell.KeyUpLeft: "<upleft>",
26 tcell.KeyUpRight: "<upright>",
27 tcell.KeyDownLeft: "<downleft>",
28 tcell.KeyDownRight: "<downright>",
29 tcell.KeyCenter: "<center>",
30 tcell.KeyPgDn: "<pgdn>",
31 tcell.KeyPgUp: "<pgup>",
32 tcell.KeyClear: "<clear>",
33 tcell.KeyExit: "<exit>",
34 tcell.KeyCancel: "<cancel>",
35 tcell.KeyPause: "<pause>",
36 tcell.KeyPrint: "<print>",
37 tcell.KeyF1: "<f-1>",
38 tcell.KeyF2: "<f-2>",
39 tcell.KeyF3: "<f-3>",
40 tcell.KeyF4: "<f-4>",
41 tcell.KeyF5: "<f-5>",
42 tcell.KeyF6: "<f-6>",
43 tcell.KeyF7: "<f-7>",
44 tcell.KeyF8: "<f-8>",
45 tcell.KeyF9: "<f-9>",
46 tcell.KeyF10: "<f-10>",
47 tcell.KeyF11: "<f-11>",
48 tcell.KeyF12: "<f-12>",
49 tcell.KeyF13: "<f-13>",
50 tcell.KeyF14: "<f-14>",
51 tcell.KeyF15: "<f-15>",
52 tcell.KeyF16: "<f-16>",
53 tcell.KeyF17: "<f-17>",
54 tcell.KeyF18: "<f-18>",
55 tcell.KeyF19: "<f-19>",
56 tcell.KeyF20: "<f-20>",
57 tcell.KeyF21: "<f-21>",
58 tcell.KeyF22: "<f-22>",
59 tcell.KeyF23: "<f-23>",
60 tcell.KeyF24: "<f-24>",
61 tcell.KeyF25: "<f-25>",
62 tcell.KeyF26: "<f-26>",
63 tcell.KeyF27: "<f-27>",
64 tcell.KeyF28: "<f-28>",
65 tcell.KeyF29: "<f-29>",
66 tcell.KeyF30: "<f-30>",
67 tcell.KeyF31: "<f-31>",
68 tcell.KeyF32: "<f-32>",
69 tcell.KeyF33: "<f-33>",
70 tcell.KeyF34: "<f-34>",
71 tcell.KeyF35: "<f-35>",
72 tcell.KeyF36: "<f-36>",
73 tcell.KeyF37: "<f-37>",
74 tcell.KeyF38: "<f-38>",
75 tcell.KeyF39: "<f-39>",
76 tcell.KeyF40: "<f-40>",
77 tcell.KeyF41: "<f-41>",
78 tcell.KeyF42: "<f-42>",
79 tcell.KeyF43: "<f-43>",
80 tcell.KeyF44: "<f-44>",
81 tcell.KeyF45: "<f-45>",
82 tcell.KeyF46: "<f-46>",
83 tcell.KeyF47: "<f-47>",
84 tcell.KeyF48: "<f-48>",
85 tcell.KeyF49: "<f-49>",
86 tcell.KeyF50: "<f-50>",
87 tcell.KeyF51: "<f-51>",
88 tcell.KeyF52: "<f-52>",
89 tcell.KeyF53: "<f-53>",
90 tcell.KeyF54: "<f-54>",
91 tcell.KeyF55: "<f-55>",
92 tcell.KeyF56: "<f-56>",
93 tcell.KeyF57: "<f-57>",
94 tcell.KeyF58: "<f-58>",
95 tcell.KeyF59: "<f-59>",
96 tcell.KeyF60: "<f-60>",
97 tcell.KeyF61: "<f-61>",
98 tcell.KeyF62: "<f-62>",
99 tcell.KeyF63: "<f-63>",
100 tcell.KeyF64: "<f-64>",
101 tcell.KeyCtrlA: "<c-a>",
102 tcell.KeyCtrlB: "<c-b>",
103 tcell.KeyCtrlC: "<c-c>",
104 tcell.KeyCtrlD: "<c-d>",
105 tcell.KeyCtrlE: "<c-e>",
106 tcell.KeyCtrlF: "<c-f>",
107 tcell.KeyCtrlG: "<c-g>",
108 tcell.KeyCtrlH: "<c-h>",
109 tcell.KeyCtrlI: "<c-i>",
110 tcell.KeyCtrlJ: "<c-j>",
111 tcell.KeyCtrlK: "<c-k>",
112 tcell.KeyCtrlL: "<c-l>",
113 tcell.KeyCtrlM: "<c-m>",
114 tcell.KeyCtrlN: "<c-n>",
115 tcell.KeyCtrlO: "<c-o>",
116 tcell.KeyCtrlP: "<c-p>",
117 tcell.KeyCtrlQ: "<c-q>",
118 tcell.KeyCtrlR: "<c-r>",
119 tcell.KeyCtrlS: "<c-s>",
120 tcell.KeyCtrlT: "<c-t>",
121 tcell.KeyCtrlU: "<c-u>",
122 tcell.KeyCtrlV: "<c-v>",
123 tcell.KeyCtrlW: "<c-w>",
124 tcell.KeyCtrlX: "<c-x>",
125 tcell.KeyCtrlY: "<c-y>",
126 tcell.KeyCtrlZ: "<c-z>",
127 }
128
129 var gValKey map[string]tcell.Key
130
131 func init() {
132 gValKey = make(map[string]tcell.Key, len(gKeyVal))
133 for k, v := range gKeyVal {
134 gValKey[v] = k
135 }
136 }
137
138 // for simplicity, assume there will only be one modifier (ctrl, shift or alt)
139 var reModKey = regexp.MustCompile(`<(c|s|a)-(.+)>`)
140
141 func wrapModifier(s string, mod string) string {
142 s = strings.TrimPrefix(s, "<")
143 s = strings.TrimSuffix(s, ">")
144 return fmt.Sprintf("<%s-%s>", mod, s)
145 }
146
147 func addKeyModifier(s string, mod tcell.ModMask) string {
148 if reModKey.MatchString(s) {
149 return s
150 }
151
152 switch {
153 case mod&tcell.ModCtrl != 0:
154 return wrapModifier(s, "c")
155 case mod&tcell.ModShift != 0:
156 return wrapModifier(s, "s")
157 case mod&tcell.ModAlt != 0:
158 return wrapModifier(s, "a")
159 default:
160 return s
161 }
162 }
163
164 func readKey(ev *tcell.EventKey) string {
165 var s string
166 if ev.Key() == tcell.KeyRune {
167 switch ev.Str() {
168 case "<":
169 s = "<lt>"
170 case ">":
171 s = "<gt>"
172 case " ":
173 s = "<space>"
174 default:
175 s = ev.Str()
176 }
177 } else {
178 s = gKeyVal[ev.Key()]
179 }
180
181 return addKeyModifier(s, ev.Modifiers())
182 }
183
184 func parseKeyModifier(s string) (tcell.ModMask, string) {
185 matches := reModKey.FindStringSubmatch(s)
186 if matches == nil {
187 return tcell.ModNone, s
188 }
189
190 mod := tcell.ModNone
191 switch matches[1] {
192 case "c":
193 mod = tcell.ModCtrl
194 case "s":
195 mod = tcell.ModShift
196 case "a":
197 mod = tcell.ModAlt
198 }
199
200 s = matches[2]
201 if len(s) > 1 {
202 s = "<" + s + ">"
203 }
204
205 return mod, s
206 }
207
208 func parseKey(s string) *tcell.EventKey {
209 if key, ok := gValKey[s]; ok {
210 return tcell.NewEventKey(key, "", tcell.ModNone)
211 }
212
213 mod, s := parseKeyModifier(s)
214
215 k := tcell.KeyRune
216 if key, ok := gValKey[s]; ok {
217 k = key
218 s = ""
219 } else {
220 switch s {
221 case "<lt>":
222 s = "<"
223 case "<gt>":
224 s = ">"
225 case "<space>":
226 s = " "
227 }
228 }
229
230 return tcell.NewEventKey(k, s, mod)
231 }