-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpshs_core.go
172 lines (155 loc) · 4.42 KB
/
pshs_core.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
package main
import (
"fmt"
"os"
"regexp"
"strings"
"github.com/gdamore/tcell"
"github.com/gdamore/tcell/encoding"
)
// 0: normal 1: regxep
var mode int8 = 0
func showHistory(fullHistoryList []string) {
encoding.Register()
screen, err := tcell.NewScreen()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
screen.Init()
screen.EnableMouse()
screen.Clear()
drawSearch(screen)
screen.Show()
for i, history := range fullHistoryList {
fullHistoryList[i] = history + "\t"
}
historyList := fullHistoryList
drawHistory(screen, historyList, 0)
highLightLine(screen, 0, historyList[0], 0)
offset := 0
currentLine := 0
searchText := ""
selectHistory := ""
for {
screen.Show()
ev := screen.PollEvent()
switch ev := ev.(type) {
case *tcell.EventResize:
screen.Sync()
screen.Clear()
drawSearch(screen)
drawHistory(screen, historyList[offset:], offset)
highLightLine(screen, currentLine, historyList[currentLine], offset)
case *tcell.EventKey:
if ev.Key() == tcell.KeyDown || ev.Key() == tcell.KeyCtrlN {
if currentLine == len(historyList) - 1 {
continue
}
_, height := screen.Size()
if currentLine - offset == height - 6 {
offset++
screen.Clear()
drawSearch(screen)
drawHistory(screen, historyList[offset:], offset)
}
cancelHighLightLine(screen, currentLine - offset, historyList[currentLine], offset)
currentLine++
highLightLine(screen, currentLine - offset, historyList[currentLine], offset)
screen.Size()
} else if ev.Key() == tcell.KeyUp || ev.Key() == tcell.KeyCtrlP {
if currentLine == 0 {
continue
}
if currentLine == offset {
offset--
screen.Clear()
drawSearch(screen)
drawHistory(screen, historyList[offset:], offset)
}
cancelHighLightLine(screen, currentLine - offset, historyList[currentLine], offset)
currentLine--
highLightLine(screen, currentLine - offset, historyList[currentLine], offset)
} else if ev.Key() == tcell.KeyEnter {
selectHistory = historyList[currentLine]
goto exit
} else if ev.Key() == tcell.KeyCtrlC {
goto exit
} else if ev.Key() == tcell.KeyRune {
if len(searchText) > 100 {
continue
}
searchText += string(ev.Rune())
historyList, _ := searchHistory(searchText, fullHistoryList)
screen.Clear()
offset = 0
currentLine = 0
drawSearch(screen)
drawText(screen, 1, 1, searchStyle, searchText)
if len(historyList) > 0 {
drawHistory(screen, historyList[offset:], offset)
highLightLine(screen, currentLine, historyList[currentLine], offset)
}
} else if ev.Key() == tcell.KeyBS {
if len(searchText) == 0 {
continue
}
if len(searchText) == 1 {
searchText = ""
historyList = fullHistoryList
} else {
searchText = searchText[:len(searchText)-1]
historyList, _ = searchHistory(searchText, fullHistoryList)
}
screen.Clear()
offset = 0
currentLine = 0
drawSearch(screen)
drawText(screen, 1, 1, searchStyle, searchText)
if len(historyList) > 0 {
drawHistory(screen, historyList[offset:], offset)
highLightLine(screen, currentLine, historyList[currentLine], offset)
}
} else if ev.Key() == tcell.KeyCtrlR {
screen.Clear()
if mode == 0 {
mode = 1
} else {
mode = 0
}
historyList, _ := searchHistory(searchText, fullHistoryList)
offset = 0
currentLine = 0
drawSearch(screen)
drawText(screen, 1, 1, searchStyle, searchText)
if len(historyList) > 0 {
drawHistory(screen, historyList[offset:], offset)
highLightLine(screen, currentLine, historyList[currentLine], offset)
}
}
}
}
exit:
screen.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorDefault).Background(tcell.ColorDefault))
screen.Fini()
fmt.Println(selectHistory)
}
func searchHistory(searchText string, fullHistoryList []string) ([]string, bool) {
if mode == 0 {
return filter(fullHistoryList, func(history string) bool {
return strings.Contains(history, searchText)
}), true
} else {
re, err := regexp.Compile(searchText)
if err != nil {
return nil, false
}
var result []string
for _, history := range fullHistoryList {
if re.MatchString(history) {
result = append(result, history)
}
}
return result, true
}
}