-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkey_binding_events.go
224 lines (197 loc) · 4.36 KB
/
key_binding_events.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"github.com/jroimartin/gocui"
"strings"
)
// 绑定事件的自定义结构体
type KeyBindingItem struct {
ViewName string
Key interface{}
Mod gocui.Modifier
Handler func(g *gocui.Gui, v *gocui.View) error
}
func KeyBindingAction(g *gocui.Gui) error {
bindingItems := []KeyBindingItem{
{
// 关闭软件,组合按键,Ctrl + C
"",
gocui.KeyCtrlC,
gocui.ModNone,
quit,
},
{
// 切换view,tab键
"",
gocui.KeyTab,
gocui.ModNone,
changeTab,
},
{
// 关闭软件,组合按键,Ctrl + C
"slide",
gocui.KeyArrowUp,
gocui.ModNone,
arrowUpAction,
},
{
// 关闭软件,组合按键,Ctrl + C
"slide",
gocui.KeyArrowDown,
gocui.ModNone,
arrowDownAction,
},
{
// 关闭hosts item 左箭头
"slide",
gocui.KeyArrowLeft,
gocui.ModNone,
arrowLeftAction,
},
{
// 打开hosts item 右箭头
"slide",
gocui.KeyArrowRight,
gocui.ModNone,
arrowRightAction,
},
{
// 添加新的hosts item, 组合键 Shift + a
"slide",
'A',
gocui.ModNone,
shiftAAction,
},
{
// 删除hosts item, 组合键 Shift + D TODO
"slide",
'D',
gocui.ModNone,
shiftDAction,
},
{
// new-hosts-item-msg view 上面按下回车
"new-hosts-item-msg",
gocui.KeyEnter,
gocui.ModNone,
newHostsItemMsgEnterAction,
},
}
for _, bindingItem := range bindingItems {
if err := g.SetKeybinding(bindingItem.ViewName, bindingItem.Key, bindingItem.Mod, bindingItem.Handler); err != nil {
return err
}
}
return nil
}
func quit(g *gocui.Gui, view *gocui.View) error {
return gocui.ErrQuit
}
func changeTab(g *gocui.Gui, view *gocui.View) error {
tabViewIndex++
if tabViewIndex >= len(tabViews) {
tabViewIndex = 0
}
return nil
}
func arrowDownAction(g *gocui.Gui, v *gocui.View) error {
// 到底了
if slideCursorY+slideOriginY > len(hItems)-2 {
return nil
}
if slideCursorY < getSlideRowCount() {
slideCursorY++
} else {
slideOriginY++
}
hItemCursorChanged = true
mainContentChanged = true
return nil
}
func arrowUpAction(g *gocui.Gui, v *gocui.View) error {
if slideOriginY > 0 {
slideOriginY--
} else if slideCursorY > 0 {
slideCursorY--
}
hItemCursorChanged = true
mainContentChanged = true
return nil
}
func arrowLeftAction(g *gocui.Gui, v *gocui.View) error {
hostsItemIdx := getCurrentHostsItemIndex()
hItems[hostsItemIdx].toggleHostsItemSwitch(false)
hItemCursorChanged = true
jsonencodeHostsInfoToPath(dataPath, hItems)
hItemsToSystemHosts(hItems)
return nil
}
func arrowRightAction(g *gocui.Gui, v *gocui.View) error {
hostsItemIdx := getCurrentHostsItemIndex()
hItems[hostsItemIdx].toggleHostsItemSwitch(true)
hItemCursorChanged = true
jsonencodeHostsInfoToPath(dataPath, hItems)
hItemsToSystemHosts(hItems)
return nil
}
func shiftAAction(g *gocui.Gui, v *gocui.View) error {
if v, err := g.SetView("new-hosts-item-msg", maxX/2-30, maxY/2, maxX/2+30, maxY/2+2); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "new hosts item name"
v.Editable = true
onMsgView = true
if _, err := setViewOnTop(g, "new-hosts-item-msg"); err != nil {
return err
}
}
return nil
}
// 判断输入的名称是否合法
func checkHostsItemName(hostsName string) bool {
// 长度小于35
if len(hostsName) > 35 {
return false
}
// 不能和已有的名称重复
for _, host := range hItems {
if host.HostsName == hostsName {
return false
}
}
return true
}
func newHostsItemMsgEnterAction(g *gocui.Gui, v *gocui.View) error {
// 输入的名称
hostsName := v.ViewBuffer()
hostsName = strings.Trim(hostsName, "\n ")
//appendToFile("/tmp/yrt", hostsName + ", " + strconv.Itoa(len(hostsName)) + "###1\n")
if !checkHostsItemName(hostsName) {
// 提示不合法 TODO
return nil
}
if hostsName != "" {
hItems = append(hItems, hostsItem{
hostsName,
"# " + hostsName + " config\n",
false,
})
jsonencodeHostsInfoToPath(dataPath, hItems)
// 调整鼠标焦点
slideCursorY = getSlideRowCount()
if len(hItems) - 1 < slideCursorY {
slideCursorY = len(hItems) - 1
}
slideOriginY = len(hItems) - getSlideRowCount() - 1
hItemCursorChanged = true
mainContentChanged = true
}
if err := g.DeleteView("new-hosts-item-msg"); err != nil {
return err
}
onMsgView = false
return nil
}
func shiftDAction(g *gocui.Gui, view *gocui.View) error {
return nil
}