-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
66 lines (55 loc) · 1.5 KB
/
list.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
package ilse
import (
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/tjmtmmnk/ilse/filter"
"github.com/tjmtmmnk/ilse/util"
)
var (
list *tview.List
)
func initList() {
initPreview()
list = tview.NewList().ShowSecondaryText(false)
list.SetBackgroundColor(tcell.ColorBlack)
list.SetChangedFunc(func(index int, mainText string, secondaryText string, shortcut rune) {
if index >= len(app.state.matched) {
return
}
item := app.state.matched[index]
text, err := getPreviewContent(item)
if err != nil {
util.Logger.Error("fail to fetch preview content : %v", err)
}
preview.SetText(text)
})
list.SetSelectedFunc(func(index int, mainText string, secondaryText string, shortcut rune) {
item := app.state.matched[index]
if err := app.screen.Suspend(); err != nil {
util.Logger.Error("failed to suspend: " + err.Error())
}
openFile(item.FileName, item.LineNum)
if err := app.screen.Resume(); err != nil {
util.Logger.Error("failed to resume: " + err.Error())
}
})
list.SetDoneFunc(func() {
frame.SetFocus(searchBar)
})
}
func convertToListItems(results []filter.SearchResult) []string {
var items []string
for _, r := range results {
item := fmt.Sprintf("[purple:black:-]%s[-]:[green]%d[-] %s[-:black]", util.ShortFileName(r.FileName), r.LineNum, r.Text)
items = append(items, item)
}
return items
}
func updateList(items []string) {
list.Clear()
for _, item := range items {
text := tview.TranslateANSI(item)
list.AddItem(text, "", 0, nil)
}
}