-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.go
295 lines (244 loc) · 5.64 KB
/
helpers.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import (
"io/ioutil"
"encoding/json"
"os"
"fmt"
"github.com/jroimartin/gocui"
"regexp"
"path/filepath"
"runtime"
)
func fileExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil { return true, nil }
if os.IsNotExist(err) { return false, nil }
return false, err
}
func gohostsInit() {
userHome := getUserHome()
gohostsDataDir := filepath.Join(userHome, ".gohosts")
// 判断文件夹是否存在,不存在创建
if exist, _ := fileExists(gohostsDataDir); !exist {
os.Mkdir(gohostsDataDir, os.ModePerm)
}
dataPath = filepath.Join(gohostsDataDir, "hostsItems.json")
initFalgPath = filepath.Join(gohostsDataDir, "initFlag")
hostsPath = "/etc/hosts"
if runtime.GOOS == "windows" {
hostsPath = getWinSystemDir()
hostsPath = filepath.Join(hostsPath, "system32", "drivers", "etc", "hosts")
}
}
func hostsItemInit() error {
//
exists, err := fileExists(initFalgPath)
if err != nil {
return err
}
if exists {
return nil
}
if backup, err := ioutil.ReadFile(hostsPath); err != nil {
return err
} else {
hItems = append(hItems, hostsItem{
"backup",
string(backup),
true,
})
jsonencodeHostsInfoToPath(dataPath, hItems)
}
ioutil.WriteFile(initFalgPath, []byte{'y', 'e', 's'}, os.ModePerm)
return nil
}
func appendToFile(fileName string, content string) error {
// 以只写的模式,打开文件
f, err := os.OpenFile(fileName, os.O_WRONLY, 0644)
if err != nil {
fmt.Println("cacheFileList.yml file create failed. err: " + err.Error())
} else {
// 查找文件末尾的偏移量
n, _ := f.Seek(0, os.SEEK_END)
// 从末尾的偏移量开始写入内容
_, err = f.WriteAt([]byte(content), n)
}
defer f.Close()
return err
}
// 从文件中解析出来hostsItem列表
func jsondecodeHostsInfoFromPath(path string) []hostsItem {
var items []hostsItem
b := getHostsItems(path)
json.Unmarshal(b, &items)
return items
}
func jsonencodeHostsInfoToPath(path string, hItems []hostsItem) error {
if b, err := json.Marshal(hItems); err != nil {
return err
} else {
if err := ioutil.WriteFile(path, b, os.ModePerm); err != nil {
return err
}
}
return nil
}
func marshal() []byte {
b, err := json.Marshal(hItems)
if err != nil {
return []byte{}
}
return b
}
// 从数据文件中读取配置信息
func getHostsItems(path string) []byte {
b, err := ioutil.ReadFile(path)
if err != nil {
return []byte{}
}
return b
}
func setCursorView(g *gocui.Gui) {
if !onMsgView {
setViewOnTop(g, getCurrentTabViewName())
}
}
func refreshEnd(g *gocui.Gui) {
if hItemCursorChanged {
setCursor(g, "main", 0, 0)
setOrigin(g, "main", 0, 0)
hItemCursorChanged = !hItemCursorChanged
}
if hItemChanged {
hItemChanged = !hItemChanged
}
}
// 设置当前view聚焦
func setViewOnTop(g *gocui.Gui, name string) (*gocui.View, error) {
if _, err := g.SetCurrentView(name); err != nil {
return nil, err
}
return g.SetViewOnTop(name)
}
// 获取当前所聚焦的tabView名称
func getCurrentTabViewName() string {
viewName := tabViews[tabViewIndex]
return viewName
}
// 渲染一个view中的内容 Editable 有bug,更新之后,不能edit
func renderString(g *gocui.Gui, viewName, s string) error {
g.Update(func(g *gocui.Gui) error {
v, err := g.View(viewName)
// just in case the view disappeared as this function was called, we'll
// silently return if it's not found
if err != nil {
return err
}
v.Wrap = true
v.Clear()
fmt.Fprint(v, s)
return nil
})
return nil
}
// 渲染一个view中的内容
func renderStringOriginCursor(g *gocui.Gui, viewName, s string, originX, originY, cursorX, cursorY int) error {
g.Update(func(*gocui.Gui) error {
v, err := g.View(viewName)
// just in case the view disappeared as this function was called, we'll
// silently return if it's not found
if err != nil {
return nil
}
v.Clear()
fmt.Fprint(v, s)
v.SetOrigin(originX, originY)
v.SetCursor(cursorX, cursorY)
v.Wrap = true
return nil
})
return nil
}
func setCursor(g *gocui.Gui, viewName string, cursorX, cursorY int) error {
v, err := g.View(viewName)
if err != nil {
return err
}
if err := v.SetCursor(cursorX, cursorY); err != nil {
return err
}
return nil
}
func setOrigin(g *gocui.Gui, viewName string, originX, originY int) error {
v, err := g.View(viewName)
if err != nil {
return err
}
if err := v.SetOrigin(originX, originY); err != nil {
return err
}
return nil
}
func getSlideRowCount() int {
ret := 0
ret = maxY - 4
return ret
}
func adjustCursorOrigin() error {
gap := getSlideRowCount() - slideCursorY
if gap > 0 {
if slideOriginY > 0 {
slideCursorY += gap
slideOriginY -= gap
if slideOriginY < 0 {
slideOriginY = 0
}
}
} else if gap < 0 {
slideCursorY += gap
slideOriginY -= gap
}
if slideCursorY < 0 {
slideCursorY = 0
}
if slideOriginY < 0 {
slideOriginY = 0
}
return nil
}
func setCurrentHostsItemContent(content string) {
idx := getCurrentHostsItemIndex()
hItems[idx].Content = content
}
// 获取真实的索引
func getCurrentHostsItemIndex() int {
ret := 0
ret = slideCursorY + slideOriginY
return ret
}
func getCurrentHostsItemContent() string {
ret := ""
ret = hItems[getCurrentHostsItemIndex()].Content
return ret
}
func checkHostsItemLine(line string) bool {
if ret, err := regexp.Match("^#.*", []byte(line)); err != nil {
return false
} else {
if ret {
return true
}
}
if ret, err := regexp.Match("^[ ]*$", []byte(line)); err != nil {
return false
} else {
if ret {
return true
}
}
ret, err := regexp.Match("^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}(?:[ ]{1,})(?:[ 0-9a-zA-Z._-]{1,}){1,}$", []byte(line))
if err != nil {
return false
}
return ret
}