-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard.go
97 lines (85 loc) · 1.77 KB
/
keyboard.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
package main
import (
"strings"
"github.com/gdamore/tcell/v2"
)
type Action struct {
c Command
args ArgList
}
type Keypress struct {
ctrl bool
alt bool
char rune // 0 if key != tcell.KeyRune
key tcell.Key // tcell.KeyRune if char != 0
}
// a more visually clean way of representing Keypress
type KeyLiteral string
var GlobalKeymap = map[Keypress]Command{
keyLiteral("ctrl-q"): ComQuit,
keyLiteral("ctrl-alt-p"): ComLogMessage,
}
func processKeypress(kp Keypress) {
var args ArgList
if comVal, ok := GlobalKeymap[kp]; ok {
switch comVal {
case ComInsertRune:
args = ArgList{kp.char}
case ComLogMessage:
args = ArgList{"firmão"}
}
comVal.Exec(args)
}
}
// Converts tcell.EventKey to Keypress
func eventKeyToKeypress(ev tcell.EventKey) Keypress {
var char rune
var key tcell.Key
if ev.Key() == tcell.KeyRune {
char = ev.Rune()
key = tcell.KeyRune
} else {
nameParts := strings.Split(ev.Name(), "+")
lastItem := nameParts[len(nameParts)-1]
if len(lastItem) == 1 {
lastItem = strings.ToLower(lastItem)
char = []rune(lastItem)[0]
key = tcell.KeyRune
} else {
char = 0
key = ev.Key()
}
}
return Keypress{
ctrl: tcell.ModCtrl & ev.Modifiers() == tcell.ModCtrl,
alt: tcell.ModAlt & ev.Modifiers() == tcell.ModAlt,
char: char,
key: key,
}
}
// Converts KeyLiteral to Keypress
func keyLiteral(key string) Keypress {
rv := Keypress{
false,
false,
0,
tcell.KeyNUL,
}
for _, elmt := range strings.Split(key, "-") {
if elmt == "ctrl" {
rv.ctrl = true
} else if elmt == "alt" {
rv.alt = true
} else if len(elmt) == 1 {
rv.char = rune([]rune(elmt)[0])
rv.key = tcell.KeyRune
} else {
for key, value := range tcell.KeyNames {
if elmt == value {
rv.key = key
}
}
}
}
return rv
}