forked from boyter/cs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tui.go
418 lines (360 loc) · 12.1 KB
/
tui.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// // SPDX-License-Identifier: MIT OR Unlicense
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
str "github.com/boyter/go-string"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type displayResult struct {
Title *tview.TextView
Body *tview.TextView
BodyHeight int
SpacerOne *tview.TextView
SpacerTwo *tview.TextView
Location string
}
type codeResult struct {
Title string
Content string
Score float64
Location string
}
type tuiApplicationController struct {
Query string
Offset int
Results []*FileJob
DocumentCount int64
Mutex sync.Mutex
DrawMutex sync.Mutex
SearchMutex sync.Mutex
// View requirements
SpinString string
SpinLocation int
SpinRun int
}
func (cont *tuiApplicationController) SetQuery(q string) {
cont.Mutex.Lock()
cont.Query = q
cont.Mutex.Unlock()
}
func (cont *tuiApplicationController) IncrementOffset() {
cont.Offset++
}
func (cont *tuiApplicationController) DecrementOffset() {
if cont.Offset > 0 {
cont.Offset--
}
}
func (cont *tuiApplicationController) ResetOffset() {
cont.Offset = 0
}
func (cont *tuiApplicationController) GetOffset() int {
return cont.Offset
}
// After any change is made that requires something drawn on the screen this is the method that does it
// NB this can never be called directly because it triggers a draw at the end.
func (cont *tuiApplicationController) drawView() {
cont.DrawMutex.Lock()
defer cont.DrawMutex.Unlock()
// reset the elements by clearing out every one so we have a clean slate to start with
for _, t := range tuiDisplayResults {
t.Title.SetText("")
t.Body.SetText("")
t.SpacerOne.SetText("")
t.SpacerTwo.SetText("")
resultsFlex.ResizeItem(t.Body, 0, 0)
}
resultsCopy := make([]*FileJob, len(cont.Results))
copy(resultsCopy, cont.Results)
// rank all results
// then go and get the relevant portion for display
rankResults(int(cont.DocumentCount), resultsCopy)
documentTermFrequency := calculateDocumentTermFrequency(resultsCopy)
// after ranking only get the details for as many as we actually need to
// cut down on processing
if len(resultsCopy) > len(tuiDisplayResults) {
resultsCopy = resultsCopy[:len(tuiDisplayResults)]
}
// We use this to swap out the highlights after we escape to ensure that we don't escape
// out own colours
md5Digest := md5.New()
fmtBegin := hex.EncodeToString(md5Digest.Sum([]byte(fmt.Sprintf("begin_%d", makeTimestampNano()))))
fmtEnd := hex.EncodeToString(md5Digest.Sum([]byte(fmt.Sprintf("end_%d", makeTimestampNano()))))
// go and get the codeResults the user wants to see using selected as the offset to display from
var codeResults []codeResult
for i, res := range resultsCopy {
if i >= cont.Offset {
// TODO run in parallel for performance boost...
snippets := extractRelevantV3(res, documentTermFrequency, int(SnippetLength))
if len(snippets) == 0 { // false positive most likely
continue
}
snippet := snippets[0]
// now that we have the relevant portion we need to get just the bits related to highlight it correctly
// which this method does. It takes in the snippet, we extract and all of the locations and then return
l := getLocated(res, snippet)
coloredContent := str.HighlightString(snippet.Content, l, fmtBegin, fmtEnd)
coloredContent = tview.Escape(coloredContent)
coloredContent = strings.Replace(coloredContent, fmtBegin, "[red]", -1)
coloredContent = strings.Replace(coloredContent, fmtEnd, "[white]", -1)
codeResults = append(codeResults, codeResult{
Title: res.Location,
Content: coloredContent,
Score: res.Score,
Location: res.Location,
})
}
}
// render out what the user wants to see based on the results that have been chosen
for i, t := range codeResults {
tuiDisplayResults[i].Title.SetText(fmt.Sprintf("[fuchsia]%s (%f)[-:-:-]", t.Title, t.Score))
tuiDisplayResults[i].Body.SetText(t.Content)
tuiDisplayResults[i].Location = t.Location
//we need to update the item so that it displays everything we have put in
resultsFlex.ResizeItem(tuiDisplayResults[i].Body, len(strings.Split(t.Content, "\n")), 0)
}
// because the search runs async through debounce we need to now draw
tviewApplication.QueueUpdateDraw(func() {})
}
func (cont *tuiApplicationController) DoSearch() {
cont.Mutex.Lock()
query := cont.Query
cont.Mutex.Unlock()
cont.SearchMutex.Lock()
defer cont.SearchMutex.Unlock()
// have a spinner which indicates if things are running as we expect
running := true
var wg sync.WaitGroup
wg.Add(1)
go func() {
for {
statusView.SetText(fmt.Sprintf("%s searching for '%s'", string(cont.SpinString[cont.SpinLocation]), query))
tviewApplication.QueueUpdateDraw(func() {})
cont.RotateSpin()
time.Sleep(20 * time.Millisecond)
if !running {
wg.Done()
return
}
}
}()
var results []*FileJob
var status string
if strings.TrimSpace(query) != "" {
files := FindFiles(query)
toProcessQueue := make(chan *FileJob, runtime.NumCPU()) // Files to be read into memory for processing
summaryQueue := make(chan *FileJob, runtime.NumCPU()) // Files that match and need to be displayed
q, fuzzy := PreParseQuery(strings.Fields(query))
fileReaderWorker := NewFileReaderWorker(files, toProcessQueue)
fileReaderWorker.FuzzyMatch = fuzzy
fileSearcher := NewSearcherWorker(toProcessQueue, summaryQueue)
fileSearcher.SearchString = q
resultSummarizer := NewResultSummarizer(summaryQueue)
resultSummarizer.FileReaderWorker = fileReaderWorker
resultSummarizer.SnippetCount = SnippetCount
go fileReaderWorker.Start()
go fileSearcher.Start()
// First step is to collect results so we can rank them
fileMatches := []string{}
for f := range summaryQueue {
results = append(results, f)
fileMatches = append(fileMatches, f.Location)
}
searchToFileMatchesCache[query] = fileMatches
plural := "s"
if len(results) == 1 {
plural = ""
}
status = fmt.Sprintf("%d result%s for '%s' from %d files", len(results), plural, query, fileReaderWorker.GetFileCount())
cont.DocumentCount = fileReaderWorker.GetFileCount()
}
running = false
wg.Wait()
statusView.SetText(status)
cont.Results = results
cont.drawView()
}
func (cont *tuiApplicationController) RotateSpin() {
cont.SpinRun++
if cont.SpinRun == 4 {
cont.SpinLocation++
if cont.SpinLocation >= len(cont.SpinString) {
cont.SpinLocation = 0
}
cont.SpinRun = 0
}
}
// Sets up the UI components we need to actually display
var overallFlex *tview.Flex
var inputField *tview.InputField
var queryFlex *tview.Flex
var resultsFlex *tview.Flex
var statusView *tview.TextView
var tuiDisplayResults []displayResult
var tviewApplication *tview.Application
var snippetInputField *tview.InputField
// setup debounce to improve ui feel
var debounced = NewDebouncer(200 * time.Millisecond)
func NewTuiSearch() {
// start indexing by walking from the current directory and updating
// this needs to run in the background with searches spawning from that
tviewApplication = tview.NewApplication()
applicationController := tuiApplicationController{
Mutex: sync.Mutex{},
SpinString: `\|/-`,
}
// Create the elements we use to display the code results here
for i := 1; i < 50; i++ {
var textViewTitle *tview.TextView
var textViewBody *tview.TextView
textViewTitle = tview.NewTextView().
SetDynamicColors(true).
SetRegions(true).
ScrollToBeginning()
textViewBody = tview.NewTextView().
SetDynamicColors(true).
SetRegions(true).
ScrollToBeginning()
tuiDisplayResults = append(tuiDisplayResults, displayResult{
Title: textViewTitle,
Body: textViewBody,
BodyHeight: -1,
SpacerOne: tview.NewTextView(),
SpacerTwo: tview.NewTextView(),
})
}
// input field which deals with the user input for the main search which ultimately triggers a search
inputField = tview.NewInputField().
SetFieldBackgroundColor(tcell.Color16).
SetLabel("> ").
SetLabelColor(tcell.ColorWhite).
SetFieldWidth(0).
SetDoneFunc(func(key tcell.Key) {
// this deals with the keys that trigger "done" functions such as up/down/enter
switch key {
case tcell.KeyEnter:
tviewApplication.Stop()
// we want to work like fzf for piping into other things hence print out the selected version
if len(applicationController.Results) != 0 {
fmt.Println(tuiDisplayResults[0].Location)
}
os.Exit(0)
case tcell.KeyTab:
tviewApplication.SetFocus(snippetInputField)
case tcell.KeyBacktab:
tviewApplication.SetFocus(snippetInputField)
case tcell.KeyUp:
applicationController.DecrementOffset()
debounced(applicationController.drawView)
case tcell.KeyDown:
applicationController.IncrementOffset()
debounced(applicationController.drawView)
case tcell.KeyESC:
tviewApplication.Stop()
os.Exit(0)
}
}).
SetChangedFunc(func(text string) {
// after the text has changed set the query and trigger a search
text = strings.TrimSpace(text)
applicationController.ResetOffset() // reset so we are at the first element again
applicationController.SetQuery(text)
debounced(applicationController.DoSearch)
})
// Decide how large a snippet we should be displaying
snippetInputField = tview.NewInputField().
SetFieldBackgroundColor(tcell.ColorDefault).
SetAcceptanceFunc(tview.InputFieldInteger).
SetText(strconv.Itoa(int(SnippetLength))).
SetFieldWidth(4).
SetChangedFunc(func(text string) {
if strings.TrimSpace(text) == "" {
SnippetLength = 300 // default
} else {
t, _ := strconv.Atoi(text)
if t == 0 {
SnippetLength = 300
} else {
SnippetLength = int64(t)
}
}
}).
SetDoneFunc(func(key tcell.Key) {
switch key {
case tcell.KeyTab:
tviewApplication.SetFocus(inputField)
case tcell.KeyBacktab:
tviewApplication.SetFocus(inputField)
case tcell.KeyEnter:
fallthrough
case tcell.KeyUp:
SnippetLength = min(SnippetLength+100, 8000)
snippetInputField.SetText(fmt.Sprintf("%d", SnippetLength))
debounced(applicationController.DoSearch)
case tcell.KeyPgUp:
SnippetLength = min(SnippetLength+200, 8000)
snippetInputField.SetText(fmt.Sprintf("%d", SnippetLength))
debounced(applicationController.DoSearch)
case tcell.KeyDown:
SnippetLength = max(100, SnippetLength-100)
snippetInputField.SetText(fmt.Sprintf("%d", SnippetLength))
debounced(applicationController.DoSearch)
case tcell.KeyPgDn:
SnippetLength = max(100, SnippetLength-200)
snippetInputField.SetText(fmt.Sprintf("%d", SnippetLength))
debounced(applicationController.DoSearch)
case tcell.KeyESC:
tviewApplication.Stop()
os.Exit(0)
}
})
statusView = tview.NewTextView().
SetDynamicColors(false).
SetRegions(false).
ScrollToBeginning()
// setup the flex containers to have everything rendered neatly
queryFlex = tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(inputField, 0, 8, false).
AddItem(snippetInputField, 5, 1, false)
resultsFlex = tview.NewFlex().SetDirection(tview.FlexRow)
overallFlex = tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(queryFlex, 1, 0, false).
AddItem(nil, 1, 0, false).
AddItem(statusView, 1, 0, false).
AddItem(resultsFlex, 0, 1, false)
// add all of the display codeResults into the container ready to be populated
for _, t := range tuiDisplayResults {
resultsFlex.AddItem(t.SpacerOne, 1, 0, false)
resultsFlex.AddItem(t.Title, 1, 0, false)
resultsFlex.AddItem(t.SpacerTwo, 1, 0, false)
resultsFlex.AddItem(t.Body, t.BodyHeight, 1, false)
}
if err := tviewApplication.SetRoot(overallFlex, true).SetFocus(inputField).Run(); err != nil {
panic(err)
}
}
func getLocated(res *FileJob, v3 Snippet) [][]int {
var l [][]int
// For all the match locations we have only keep the ones that should be inside
// where we are matching
for _, value := range res.MatchLocations {
for _, s := range value {
if s[0] >= v3.StartPos && s[1] <= v3.EndPos {
// Have to create a new one to avoid changing the position
// unlike in others where we throw away the results afterwards
t := []int{s[0] - v3.StartPos, s[1] - v3.StartPos}
l = append(l, t)
}
}
}
return l
}