-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeymap.go
69 lines (58 loc) · 1.44 KB
/
keymap.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
package selection
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
)
// NewDefaultKeyMap returns a KeyMap with sensible default key mappings that can
// also be used as a starting point for customization.
func NewDefaultKeyMap() *KeyMap {
return &KeyMap{
Down: []string{"down"},
Up: []string{"up"},
Select: []string{"enter"},
Abort: []string{"ctrl+c"},
ClearFilter: []string{"esc"},
ScrollDown: []string{"pgdown"},
ScrollUp: []string{"pgup"},
Left: []string{"left"},
Right: []string{"right"},
}
}
// KeyMap defines the keys that trigger certain actions.
type KeyMap struct {
Down []string
Up []string
Select []string
Abort []string
ClearFilter []string
ScrollDown []string
ScrollUp []string
Left []string
Right []string
}
func keyMatches(key tea.KeyMsg, mapping []string) bool {
for _, m := range mapping {
if m == key.String() {
return true
}
}
return false
}
// validateKeyMap returns true if the given key map contains at
// least the bare minimum set of key bindings for the functional
// prompt and false otherwise.
func validateKeyMap(km *KeyMap) error {
if len(km.Up) == 0 {
return fmt.Errorf("no up key")
}
if len(km.Down) == 0 {
return fmt.Errorf("no down key")
}
if len(km.Select) == 0 {
return fmt.Errorf("no select key")
}
if len(km.Abort) == 0 {
return fmt.Errorf("no abort key")
}
return nil
}