-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkey_handling.go
143 lines (138 loc) · 3.05 KB
/
key_handling.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
138
139
140
141
142
143
package main
import "strconv"
// KeyPress sends a keypress
type KeyPress struct {
Key string
}
// Apply the KeyPress
func (action KeyPress) Apply(state AppState) AppState {
key := action.Key
switch state.CurrentMode {
case normal:
switch key {
case "<enter>":
state = state.changeMode(selectCategory)
case "<tab>":
if !state.showMods {
state.CurrentMode = modifierMode
}
state.showMods = !state.showMods
case "?", "/":
state = state.changeMode(search)
case "w":
state.wrap = !state.wrap
case "<backspace>":
// Actually c-h
state = state.changeSelection(left)
case "C-l":
state = state.changeSelection(right)
case "<up>", "k":
state = state.scroll(up, 1)
case "<down>", "j":
state = state.scroll(down, 1)
case "b", "C-u":
state = state.scroll(up, state.termHeight/2)
case "C-d":
state = state.scroll(down, state.termHeight/2)
case "G":
state = state.scroll(bottom, 0)
case "n":
state = state.findNext(up)
case "N":
state = state.findNext(down)
case "!", "@", "#", "$", "%", "^", "&", "*", "(", ")":
state = state.toggleModifier(numFromSymbol(key))
case "1", "2", "3", "4":
choice, _ := strconv.Atoi(key)
state = state.changeLayout(choice)
default:
state.StatusBar.Text = key
}
case selectCategory:
switch key {
case "<enter>":
bestMatch, ok := state.getBestMatch()
if ok {
state = state.selectCategory(bestMatch)
}
fallthrough
case "<escape>":
state = state.changeMode(normal)
state.selectCategoryBuffer.text = ""
default:
state = state.typeKey(key)
}
case search:
switch key {
case "<enter>":
// quit search here...
fallthrough
case "<escape>":
state = state.changeMode(normal)
default:
state = state.typeKey(key)
}
case modifierMode:
switch key {
case "<tab>":
state.showMods = false
state = state.changeMode(normal)
case "<enter>":
if state.selectedMod == len(state.modifiers) {
state.modifiers = append(state.modifiers, modifier{active: true, kind: filter, fgColor: "black", bgColor: "white"})
}
state = state.changeMode(editModifier)
case "<space>":
state = state.toggleModifier(state.selectedMod)
case "<backspace>":
state.selected = state.layout - 1
state = state.changeMode(normal)
case "!", "@", "#", "$", "%", "^", "&", "*", "(", ")":
state = state.toggleModifier(numFromSymbol(key))
case "j":
// Allow going one past the end in each list
if state.selectedMod < len(state.modifiers) {
state.selectedMod++
}
case "k":
if state.selectedMod > 0 {
state.selectedMod--
}
}
case editModifier:
switch key {
case "<enter>", "<escape>":
state = state.changeMode(modifierMode)
default:
state = state.typeKey(key)
}
default:
panic("Didn't handle keypress!")
}
return state
}
func numFromSymbol(key string) int {
switch key {
case "!":
return 0
case "@":
return 1
case "#":
return 2
case "$":
return 3
case "%":
return 4
case "^":
return 5
case "&":
return 6
case "*":
return 7
case "(":
return 8
case ")":
return 9
}
return 0
}