-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_box.go
117 lines (95 loc) · 2.79 KB
/
list_box.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
package nve
import (
"fmt"
"math"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type ListBox struct {
*tview.List
contentView *ContentBox
}
func NewListBox(contentView *ContentBox, notes *Notes) *ListBox {
box := ListBox{
List: tview.NewList(),
contentView: contentView,
}
box.ShowSecondaryText(false).
SetWrapAround(false).
SetHighlightFullLine(true).
SetSelectedStyle(
tcell.StyleDefault.
Background(tcell.ColorDarkBlue).
Foreground(tcell.ColorLightSkyBlue),
)
box.SetBorder(true).
SetTitle("List Box").
SetTitleColor(tcell.ColorOrange).
SetBorderStyle(tcell.StyleDefault.Dim(true)).
SetBorderPadding(0, 0, 1, 1).
SetTitleAlign(tview.AlignLeft)
box.SetSelectedFocusOnly(true)
box.SetChangedFunc(func(index int, mainText, secondaryText string, shortcut rune) {
if (!box.HasFocus() && notes.LastQuery == "") || len(notes.LastSearchResults) == 0 {
box.contentView.Clear()
} else {
result := notes.LastSearchResults[index]
box.contentView.SetFile(result.FileRef)
}
})
box.SetFocusFunc(func() {
if notes.LastQuery == "" {
result := notes.LastSearchResults[box.GetCurrentItem()]
box.contentView.SetFile(result.FileRef)
}
})
return &box
}
func (b *ListBox) SearchResultsUpdate(notes *Notes) {
emptyQuery := notes.LastQuery == ""
lastResult := notes.LastSearchResults
b.Clear()
b.SetSelectedFocusOnly(emptyQuery)
if len(lastResult) == 0 {
b.contentView.Clear()
}
selectedIndex := -1
for index, result := range lastResult {
displayName := result.DisplayName()
var formattedName string
if len(displayName) > 14 {
formattedName = fmt.Sprintf("%-20.20s..", displayName)
} else {
formattedName = fmt.Sprintf("%-22.22s", displayName)
}
b.AddItem(strings.Join([]string{formattedName, result.Snippet}, " : "), "", 0, nil)
if selectedIndex == -1 && strings.HasPrefix(displayName, notes.LastQuery) {
selectedIndex = index
}
}
_, _, _, height := b.GetInnerRect()
if selectedIndex >= 0 {
// highlights row with exact prefix match to search query.
b.SetCurrentItem(selectedIndex)
// scroll to view; use height of list box
b.SetOffset(int(math.Max(float64(selectedIndex-height+1), 0)), 0)
} else {
// highlight any selected row if not in visible rect
if !b.InRect(b.GetCurrentItem(), 0) {
b.SetOffset(b.GetCurrentItem(), 0)
}
}
}
// InputHandler overrides default handling to switch focus away from search box when necessary.
func (lb *ListBox) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
return lb.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
if event.Key() == tcell.KeyEnter {
setFocus(lb.contentView)
} else {
if handler := lb.List.InputHandler(); handler != nil {
handler(event, setFocus)
}
}
})
}