-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathkeybindings.go
137 lines (116 loc) · 2.83 KB
/
keybindings.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* ZEUS - An Electrifying Build System
* Copyright (c) 2017 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"errors"
"strings"
"github.com/dreadl0ck/readline"
)
var (
// global listener for key events
listener = readline.FuncListener(func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) {
if key > 26 {
return
}
if keyName, ok := keyMap[key]; ok {
if command, ok := projectData.fields.KeyBindings[keyName]; ok {
println()
handleLine(command)
}
}
return
})
// mapped runes to keyComb strings
keyMap = map[rune]string{
1: "Ctrl-A",
2: "Ctrl-B",
5: "Ctrl-E",
6: "Ctrl-F",
7: "Ctrl-G",
8: "Ctrl-H",
9: "Ctrl-I",
10: "Ctrl-J",
11: "Ctrl-K",
12: "Ctrl-L",
13: "Ctrl-M",
14: "Ctrl-N",
15: "Ctrl-O",
16: "Ctrl-P",
17: "Ctrl-Q",
18: "Ctrl-R",
19: "Ctrl-S",
20: "Ctrl-T",
21: "Ctrl-U",
22: "Ctrl-V",
23: "Ctrl-W",
24: "Ctrl-X",
25: "Ctrl-Y",
}
// ErrInvalidKeyComb means the KeyComb does not exist
ErrInvalidKeyComb = errors.New("invalid key combination")
)
// handle keys shell command
func handleKeysCommand(args []string) {
if len(args) < 2 {
printKeybindings()
return
}
if len(args) < 3 {
printKeybindingsCommmandUsageErr()
return
}
var ok bool
// check if KeyComb exists
for _, s := range keyMap {
if s == args[2] {
ok = true
}
}
if !ok {
Log.Error(ErrInvalidKeyComb)
return
}
if args[1] == "set" {
if len(args) < 4 {
printKeybindingsCommmandUsageErr()
return
}
projectData.Lock()
projectData.fields.KeyBindings[args[2]] = strings.Join(args[3:], " ")
projectData.Unlock()
projectData.update()
Log.Info("key binding added")
} else if args[1] == "remove" {
projectData.Lock()
delete(projectData.fields.KeyBindings, args[2])
projectData.Unlock()
projectData.update()
Log.Info("key binding removed")
} else {
printKeybindingsCommmandUsageErr()
}
}
func printKeybindingsCommmandUsageErr() {
l.Println(ErrInvalidUsage)
l.Println("usage: keys [set <KeyComb> <commandChain>] [remove <KeyComb>]")
}
func printKeybindings() {
for key, cmd := range projectData.fields.KeyBindings {
l.Println(key, "=", cmd)
}
}