forked from dhamidi/leader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_keys.go
46 lines (40 loc) · 1.15 KB
/
list_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
package main
import (
"fmt"
"strings"
)
// ListKeys prints all key bindings on the context's terminal.
type ListKeys struct {
*Context
}
// NewListKeys creates a new command instance operating on the provided context.
func NewListKeys(ctx *Context) *ListKeys {
return &ListKeys{
Context: ctx,
}
}
// Execute runs this command.
func (cmd *ListKeys) Execute() error {
var walkKeyMap func(keymap *KeyMap, path []rune, f func(path []rune, b *KeyBinding))
walkKeyMap = func(keymap *KeyMap, path []rune, f func(path []rune, b *KeyBinding)) {
for _, keyBinding := range keymap.Bindings() {
childPath := make([]rune, len(path))
copy(childPath, path)
childPath = append(childPath, keyBinding.Key())
if keyBinding.HasChildren() {
walkKeyMap(keyBinding.Children(), childPath, f)
} else {
f(childPath, keyBinding)
}
}
}
printBinding := func(path []rune, b *KeyBinding) {
pathString := []string{}
for _, r := range path {
pathString = append(pathString, fmt.Sprintf("%c", r))
}
fmt.Fprintf(cmd.Terminal, "%s: %s\n", strings.Join(pathString, " "), b.Description())
}
walkKeyMap(cmd.CurrentKeyMap, []rune{}, printBinding)
return nil
}