-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
356 lines (344 loc) · 13.7 KB
/
main.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package main
import (
"fmt"
"github.com/eiannone/keyboard"
"regexp"
"runtime"
)
func main() {
initStateData()
getTerminalColumnsAndRows()
getCommandState()
getScreenLineNumber()
if onlyPrintHelpInstructions() {
printHelpInstructions()
return
}
if gCommandState.Help {
printHelpInstructions()
}
var re *regexp.Regexp = nil
if len(gCommandState.SearchPattern) > 0 {
re = regexp.MustCompile("(?i)" + gCommandState.SearchPattern)
}
printCurrentDirFiles(re)
if len(gSearchData.FileDataArr) == 0 {
fmt.Println("Search results are Empty.")
return
}
clearPreviousNthLine(getSelectedGroupDisplayFileNamesLength(gSearchData.DisplayFileNamesInGroup))
printContentLineWithSelectedDisplayName(gTerminalState.SelectedGroupIndex, gTerminalState.SelectedLineIndex)
err := keyboard.Open()
if err != nil {
fmt.Println("Error occurred when opening keyboard:", err)
return
}
defer keyboard.Close()
keysEvents, err := keyboard.GetKeys(10)
if err != nil {
fmt.Println("Error occurred when getting keys:", err)
return
}
for {
event := <-keysEvents
if event.Err != nil {
panic(event.Err)
}
if event.Key == keyboard.KeyArrowUp {
if gTerminalState.CurrentMenuLevel == 0 {
if gTerminalState.SelectedLineIndex < 0 {
continue
} else if gTerminalState.SelectedLineIndex == 0 {
if 0 < gTerminalState.SelectedGroupIndex {
showPreviousPage(gTerminalState.SelectedLineIndex, gTerminalState.MaxLineLength-1)
}
} else {
clearCurrentLine()
printContentLineWithUnselectedDisplayName(gTerminalState.SelectedGroupIndex, gTerminalState.SelectedLineIndex)
gTerminalState.SelectedLineIndex = gTerminalState.SelectedLineIndex - 1
if gTerminalState.SelectedLineIndex < getSelectedGroupDisplayFileNamesLength(gSearchData.DisplayFileNamesInGroup) {
clearPreviousNthLine(1)
printContentLineWithSelectedDisplayName(gTerminalState.SelectedGroupIndex, gTerminalState.SelectedLineIndex)
}
}
} else if gTerminalState.CurrentMenuLevel == 1 {
// 在菜单中上下切换
if gTerminalState.SelectedLineIndex <= gTerminalState.MenuTopRowIndex+1 {
continue
}
clearCurrentLine()
printContentLineWithUnselectedDisplayName(gTerminalState.SelectedGroupIndex, gTerminalState.SelectedLineIndex)
// 光标移动到合适的列位置
moveCursorToColumnIndex(gTerminalState.MenuLeftColumnIndex)
// 输出文字
//fmt.Print(gMenu[gTerminalState.SelectedLineIndex-gTerminalState.MenuTopRowIndex])
printUnselectedMenuLevel1Line(gTerminalState.SelectedLineIndex - gTerminalState.MenuTopRowIndex)
// 光标移动到上一行
gTerminalState.SelectedLineIndex = gTerminalState.SelectedLineIndex - 1
clearPreviousNthLine(1)
moveCursorToLeft()
if gTerminalState.SelectedLineIndex < getSelectedGroupDisplayFileNamesLength(gSearchData.DisplayFileNamesInGroup) {
printContentLineWithUnselectedDisplayName(gTerminalState.SelectedGroupIndex, gTerminalState.SelectedLineIndex)
}
moveCursorToColumnIndex(gTerminalState.MenuLeftColumnIndex)
printSelectedMenuLevel1Line(gTerminalState.SelectedLineIndex - gTerminalState.MenuTopRowIndex)
//fmt.Print(gMenu[gTerminalState.SelectedLineIndex-gTerminalState.MenuTopRowIndex])
moveCursorToColumnIndex(gTerminalState.MenuCursorColumnIndex)
}
} else if event.Key == keyboard.KeyArrowDown {
if gTerminalState.CurrentMenuLevel == 0 {
if gTerminalState.SelectedLineIndex < getSelectedGroupDisplayFileNamesLength(gSearchData.DisplayFileNamesInGroup)-1 {
// 当前页面
clearCurrentLine()
printContentLineWithUnselectedDisplayName(gTerminalState.SelectedGroupIndex, gTerminalState.SelectedLineIndex)
gTerminalState.SelectedLineIndex = gTerminalState.SelectedLineIndex + 1
clearNextLine()
fmt.Print("\r")
printContentLineWithSelectedDisplayName(gTerminalState.SelectedGroupIndex, gTerminalState.SelectedLineIndex)
} else {
if gTerminalState.SelectedGroupIndex < getGroupLength(gSearchData.DisplayFileNamesInGroup)-1 {
// 如果有下一页,则进入下一页
showNextPage(gTerminalState.SelectedLineIndex, 0)
} else {
// 没有下一页,到底了
continue
}
}
} else if gTerminalState.CurrentMenuLevel == 1 {
// 在菜单中上下切换
if gTerminalState.MenuBottomRowIndex-1 <= gTerminalState.SelectedLineIndex {
continue
}
clearCurrentLine()
printContentLineWithUnselectedDisplayName(gTerminalState.SelectedGroupIndex, gTerminalState.SelectedLineIndex)
// 光标移动到合适的列位置
moveCursorToColumnIndex(gTerminalState.MenuLeftColumnIndex)
// 输出文字
printUnselectedMenuLevel1Line(gTerminalState.SelectedLineIndex - gTerminalState.MenuTopRowIndex)
//fmt.Print(gMenu[gTerminalState.SelectedLineIndex-gTerminalState.MenuTopRowIndex])
// 光标移动到下一行
gTerminalState.SelectedLineIndex = gTerminalState.SelectedLineIndex + 1
clearNextNthLine(1)
moveCursorToLeft()
if gTerminalState.SelectedLineIndex < getSelectedGroupDisplayFileNamesLength(gSearchData.DisplayFileNamesInGroup) {
printContentLineWithUnselectedDisplayName(gTerminalState.SelectedGroupIndex, gTerminalState.SelectedLineIndex)
}
moveCursorToColumnIndex(gTerminalState.MenuLeftColumnIndex)
//fmt.Print(gMenu[gTerminalState.SelectedLineIndex-gTerminalState.MenuTopRowIndex])
printSelectedMenuLevel1Line(gTerminalState.SelectedLineIndex - gTerminalState.MenuTopRowIndex)
moveCursorToColumnIndex(gTerminalState.MenuCursorColumnIndex)
}
} else if event.Key == keyboard.KeyArrowRight {
// 切屏
if gTerminalState.SelectedGroupIndex >= getGroupLength(gSearchData.DisplayFileNamesInGroup)-1 {
continue
}
showNextPage(gTerminalState.SelectedLineIndex, gTerminalState.SelectedLineIndex)
} else if event.Key == keyboard.KeyArrowLeft {
// 切屏
if gTerminalState.SelectedGroupIndex <= 0 {
continue
}
showPreviousPage(gTerminalState.SelectedLineIndex, gTerminalState.SelectedLineIndex)
} else if (event.Key == keyboard.KeyEsc) || (event.Rune == 'Q') || (event.Rune == 'q') {
if gTerminalState.CurrentMenuLevel > 0 {
if gTerminalState.CurrentMenuLevel == 1 {
dismissMenuLevel1()
}
} else {
restoreCurrentLineWithSearchResult()
clearNextNthLine(gTerminalState.MaxLineLength - gTerminalState.SelectedLineIndex)
break
}
} else if event.Key == keyboard.KeyCtrlC {
restoreCurrentLineWithSearchResult()
clearNextNthLine(gTerminalState.MaxLineLength - gTerminalState.SelectedLineIndex)
break
} else if event.Key == keyboard.KeyEnter {
if isMac() {
// 打开文件
ret := openCurrentFile()
if ret == 0 {
clearNextNthLine(gTerminalState.MaxLineLength - gTerminalState.SelectedLineIndex)
break
}
} else {
popMenu()
}
} else if (event.Rune == 'P') || (event.Rune == 'p') {
if isMac() {
// 打开文件的父目录
ret := openCurrentFilesParentDir()
if ret == 0 {
clearNextNthLine(gTerminalState.MaxLineLength - gTerminalState.SelectedLineIndex)
break
}
} else {
popMenu()
}
} else if event.Key == keyboard.KeySpace {
popMenu()
}
}
}
func restoreCurrentLineWithSearchResult() {
clearCurrentLine()
printContentLineWithUnselectedDisplayName(gTerminalState.SelectedGroupIndex, gTerminalState.SelectedLineIndex)
fmt.Print("\r")
}
func isMac() bool {
return runtime.GOOS == "darwin"
}
// 打开菜单对话框
func popMenu() {
if gTerminalState.CurrentMenuLevel == 0 {
gTerminalState.CurrentMenuLevel = 1
popMenuLevel1()
}
}
func popMenuLevel1() {
// 菜单高度
gTerminalState.MenuHeight = len(gMenu)
gTerminalState.MenuLeftColumnIndex = 10
gTerminalState.MenuCursorColumnIndex = 12
// 菜单顶部
gTerminalState.MenuTopRowIndex = 0
if gTerminalState.MaxLineLength-gTerminalState.SelectedLineIndex > gTerminalState.MenuHeight {
// 当前光标的下部分高度大于menu高度
gTerminalState.MenuTopRowIndex = gTerminalState.SelectedLineIndex + 1
} else if gTerminalState.SelectedLineIndex >= gTerminalState.MenuHeight {
// 当前光标的下部分高度小于menu高度,且光标上部分高度大于menu高度
gTerminalState.MenuTopRowIndex = gTerminalState.SelectedLineIndex - gTerminalState.MenuHeight
} else if gTerminalState.MaxLineLength > gTerminalState.MenuHeight {
// 居中
gTerminalState.MenuTopRowIndex = (gTerminalState.MaxLineLength-1-gTerminalState.MenuHeight)/2 + 1
} else {
// todo 拓展高度?
}
gTerminalState.MenuBottomRowIndex = gTerminalState.MenuTopRowIndex + gTerminalState.MenuHeight - 1
if gTerminalState.SelectedLineIndex < gTerminalState.MenuTopRowIndex {
// 下移光标
moveCursorToNextNthLines(gTerminalState.MenuTopRowIndex - gTerminalState.SelectedLineIndex)
} else {
// 上移光标
moveCursorToPreviousNthLines(gTerminalState.SelectedLineIndex - gTerminalState.MenuTopRowIndex)
}
gTerminalState.MenuLevelOCursorIndex = gTerminalState.SelectedLineIndex
gTerminalState.SelectedLineIndex = gTerminalState.MenuTopRowIndex
// 开始输出文字
for i := 0; i < gTerminalState.MenuHeight; i++ {
// 光标移动到合适的列位置
moveCursorToColumnIndex(gTerminalState.MenuLeftColumnIndex)
// 输出文字
if i == 1 {
printSelectedMenuLevel1Line(i)
//fmt.Print(gMenu[i])
} else {
printUnselectedMenuLevel1Line(i)
//fmt.Print(gMenu[i])
}
// 光标移动到下一行
moveCursorToNextNthLines(1)
}
gTerminalState.SelectedLineIndex = gTerminalState.MenuTopRowIndex + gTerminalState.MenuHeight
moveCursorToPreviousNthLines(gTerminalState.MenuHeight - 1)
gTerminalState.SelectedLineIndex = gTerminalState.SelectedLineIndex - (gTerminalState.MenuHeight - 1)
moveCursorToColumnIndex(gTerminalState.MenuCursorColumnIndex)
}
func dismissMenuLevel1() {
gTerminalState.CurrentMenuLevel = gTerminalState.CurrentMenuLevel - 1
jumpCursorToCertainLine(gTerminalState.MenuTopRowIndex)
//selectedLineIndexBeforeDismiss := gTerminalState.SelectedLineIndex
// 遍历并清空屏幕
for i := 0; i < gTerminalState.MenuHeight; i++ {
clearCurrentLine()
fmt.Print("\r")
if i < gTerminalState.MenuHeight-1 {
moveCursorToNextNthLines(1)
}
}
// 光标停留在menu最后一行,然后移动到menu最上面一行
moveCursorToPreviousNthLines(gTerminalState.MenuHeight - 1)
for i := 0; i < gTerminalState.MenuHeight; i++ {
clearCurrentLine()
if gTerminalState.SelectedGroupIndex*gTerminalState.SwitchScreenLines+gTerminalState.MenuTopRowIndex+i < len(gSearchData.FileDataArr) {
printContentLineWithUnselectedDisplayName(gTerminalState.SelectedGroupIndex, gTerminalState.MenuTopRowIndex+i)
}
fmt.Print("\r")
if i < gTerminalState.MenuHeight-1 {
moveCursorToNextNthLines(1)
}
}
// 光标停留在menu最后一行,然后移动到原来的位置
gTerminalState.SelectedLineIndex = gTerminalState.MenuTopRowIndex + gTerminalState.MenuHeight - 1
jumpCursorToCertainLine(gTerminalState.MenuLevelOCursorIndex)
gTerminalState.SelectedLineIndex = gTerminalState.MenuLevelOCursorIndex
}
func showNextPage(oldSelectedLineIndex int, newSelectedLineIndex int) {
gTerminalState.SelectedGroupIndex = gTerminalState.SelectedGroupIndex + 1
// 光标先移动到最上面
moveCursorToPreviousNthLines(oldSelectedLineIndex)
// 遍历并清空屏幕
for i := 0; i < gTerminalState.MaxLineLength; i++ {
clearCurrentLine()
fmt.Print("\r")
if i < gTerminalState.MaxLineLength-1 {
moveCursorToNextNthLines(1)
}
}
// 光标移动到最上面
moveCursorToPreviousNthLines(gTerminalState.MaxLineLength - 1)
// 遍历并打印当前group的屏幕内容
currentGroupLinesLength := getSelectedGroupDisplayFileNamesLength(gSearchData.DisplayFileNamesInGroup)
for i := 0; i < currentGroupLinesLength; i++ {
clearCurrentLine()
printContentLineWithUnselectedDisplayName(gTerminalState.SelectedGroupIndex, i)
fmt.Print("\r")
if i < currentGroupLinesLength-1 {
moveCursorToNextNthLines(1)
}
}
// 确保 newSelectedLineIndex 不超过当前页面内容
if newSelectedLineIndex >= currentGroupLinesLength {
newSelectedLineIndex = currentGroupLinesLength - 1
}
// 从当前内容的最后一行,定位到想要光标定位的行
moveCursorToPreviousNthLines(currentGroupLinesLength - 1 - newSelectedLineIndex)
gTerminalState.SelectedLineIndex = newSelectedLineIndex
clearCurrentLine()
printContentLineWithSelectedDisplayName(gTerminalState.SelectedGroupIndex, gTerminalState.SelectedLineIndex)
}
func showPreviousPage(oldPageSelectedLineIndex int, newSelectedLineIndex int) {
gTerminalState.SelectedGroupIndex = gTerminalState.SelectedGroupIndex - 1
// 光标先移动到最上面
moveCursorToPreviousNthLines(oldPageSelectedLineIndex)
// 遍历并清空屏幕
for i := 0; i < gTerminalState.MaxLineLength; i++ {
clearCurrentLine()
fmt.Print("\r")
if i < gTerminalState.MaxLineLength-1 {
moveCursorToNextNthLines(1)
}
}
// 光标移动到最上面
moveCursorToPreviousNthLines(gTerminalState.MaxLineLength - 1)
// 遍历并打印当前group的屏幕内容
currentGroupLinesLength := getSelectedGroupDisplayFileNamesLength(gSearchData.DisplayFileNamesInGroup)
for i := 0; i < currentGroupLinesLength; i++ {
clearCurrentLine()
printContentLineWithUnselectedDisplayName(gTerminalState.SelectedGroupIndex, i)
fmt.Print("\r")
if i < currentGroupLinesLength-1 {
moveCursorToNextNthLines(1)
}
}
// 确保 newSelectedLineIndex 不超过当前页面内容
if newSelectedLineIndex >= currentGroupLinesLength {
newSelectedLineIndex = currentGroupLinesLength - 1
}
// 从当前内容的最后一行,定位到想要光标定位的行
moveCursorToPreviousNthLines(currentGroupLinesLength - 1 - newSelectedLineIndex)
gTerminalState.SelectedLineIndex = newSelectedLineIndex
clearCurrentLine()
printContentLineWithSelectedDisplayName(gTerminalState.SelectedGroupIndex, gTerminalState.SelectedLineIndex)
}