-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
101 lines (88 loc) Β· 1.62 KB
/
keys.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
package main
type key int32
const (
BackKey key = iota
QuitKey
SaveKey
)
type Help struct {
Key string
Desc string
}
type Binding struct {
help Help
keys []string
disabled bool
}
type keyMap struct {
Back Binding
Quit Binding
Save Binding
Help Binding
FullScreen Binding
CycleCamMode Binding
}
var keys = keyMap{
Back: NewBinding(
WithKeys("b", "back"),
WithHelp("b", "go back"),
),
Quit: NewBinding(
WithKeys("q", "quit"),
WithHelp("q", "quit editor mode"),
),
Save: NewBinding(
WithDisabled(),
WithKeys("s", "save"),
WithHelp("s", "[wip] save the changes"),
),
Help: NewBinding(
WithDisabled(),
WithKeys("h", "help"),
WithHelp("h", "[wip] display the help menu"),
),
FullScreen: NewBinding(
WithKeys("full", "f"),
WithHelp("f", "toggle fullscreen"),
),
CycleCamMode: NewBinding(
WithKeys("c", "cam"),
WithHelp("c", "toggle cam mode"),
),
}
type BindingOpt func(*Binding)
func Matches(key string, b ...Binding) bool {
for _, binding := range b {
for _, v := range binding.keys {
if key == v && binding.Enabled() {
return true
}
}
}
return false
}
func NewBinding(opts ...BindingOpt) Binding {
b := &Binding{}
for _, opt := range opts {
opt(b)
}
return *b
}
func WithKeys(keys ...string) BindingOpt {
return func(b *Binding) {
b.keys = keys
}
}
func (b Binding) Enabled() bool {
return !b.disabled && b.keys != nil
}
func WithHelp(key, desc string) BindingOpt {
return func(b *Binding) {
b.help = Help{Key: key, Desc: desc}
}
}
func WithDisabled() BindingOpt {
return func(b *Binding) {
b.disabled = true
}
}