-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple.go
197 lines (169 loc) · 4.22 KB
/
simple.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
package list
import (
"github.com/sahilm/fuzzy"
)
type SimpleItem struct {
Title, Desc string
Disabled bool
}
type SimpleItemList []SimpleItem
var _ fuzzy.Source = (SimpleItemList)(nil)
func (s SimpleItemList) Len() int {
return len(s)
}
func (s SimpleItemList) String(i int) string {
return s[i].Title
}
type SimpleAdapter struct {
items SimpleItemList
filterResult []fuzzy.Match
lastFilterPattern string
StyleNormal, StyleDimmed *SimpleAdapterStyle
}
func NewSimpleAdapter(items SimpleItemList) *SimpleAdapter {
styleNormal, styleDimmed := SimpleDefaultStyle()
return &SimpleAdapter{
items: items,
StyleNormal: styleNormal,
StyleDimmed: styleDimmed,
}
}
var _ Adapter = (*SimpleAdapter)(nil)
func (s *SimpleAdapter) Len() int {
if s.filterResult == nil {
return len(s.items)
}
return len(s.filterResult)
}
func (s *SimpleAdapter) Sep() string {
return "\n\n"
}
func (s *SimpleAdapter) View(pos, focus int) string {
var match *fuzzy.Match
if s.filterResult != nil {
match = &s.filterResult[pos]
pos = match.Index
if focus >= 0 {
focus = s.filterResult[focus].Index
}
}
item := s.items[pos]
var renderTitle, renderDesc, renderBorder, renderFilterMatch func(string) string
if !item.Disabled {
if focus == pos {
// focused
st := s.StyleNormal
renderTitle = st.titleSelected.Render
renderDesc = st.descSelected.Render
renderBorder = st.borderSelected.Render
renderFilterMatch = st.filterMatchSelected.Render
} else if focus == FocusDisabled {
// disabled
st := s.StyleDimmed
renderTitle = st.title.Render
renderDesc = st.desc.Render
renderBorder = border.Render
renderFilterMatch = st.filterMatch.Render
} else {
// blurred/viewmode
st := s.StyleNormal
renderTitle = st.title.Render
renderDesc = st.desc.Render
renderBorder = border.Render
renderFilterMatch = st.filterMatch.Render
}
} else {
st := s.StyleDimmed
if focus == pos {
// focused
renderTitle = st.titleSelected.Render
renderDesc = st.descSelected.Render
renderBorder = st.borderSelected.Render
renderFilterMatch = st.filterMatchSelected.Render
} else {
// blurred/disabled
renderTitle = st.title.Render
renderDesc = st.desc.Render
renderBorder = border.Render
renderFilterMatch = st.filterMatch.Render
}
}
var title = item.Title
if match != nil {
var highlightedTitle string
Outer:
for i, r := range title {
for _, i2 := range match.MatchedIndexes {
if i == i2 {
highlightedTitle += renderFilterMatch(string(r))
continue Outer
}
}
highlightedTitle += renderTitle(string(r))
}
title = highlightedTitle
}
return renderBorder(renderTitle(title) + "\n" + renderDesc(item.Desc))
}
func (s *SimpleAdapter) Append(item ...SimpleItem) {
s.items = append(s.items, item...)
if s.filterResult != nil {
s.Filter(s.lastFilterPattern)
}
}
func (s *SimpleAdapter) Insert(i int, item ...SimpleItem) {
s.items = append(s.items[:i+len(item)], s.items[i:]...)
for i2 := 0; i2 < len(item); i2++ {
s.items[i+i2] = item[i2]
}
if s.filterResult != nil {
s.Filter(s.lastFilterPattern)
}
}
func (s *SimpleAdapter) Remove(i int) {
s.items = append(s.items[:i], s.items[i+1:]...)
if s.filterResult != nil {
s.Filter(s.lastFilterPattern)
}
}
func (s *SimpleAdapter) Filter(pattern string) {
s.lastFilterPattern = pattern
if len(pattern) > 0 {
s.filterResult = fuzzy.FindFrom(pattern, s.items)
if s.filterResult == nil {
s.filterResult = make([]fuzzy.Match, 0)
}
} else {
s.filterResult = nil
}
}
func (s *SimpleAdapter) OriginalItemLen() int {
return len(s.items)
}
func (s *SimpleAdapter) FilteredIndex(pos int) int {
if s.filterResult == nil {
return pos
}
return s.filterResult[pos].Index
}
func (s *SimpleAdapter) FilteredItemAt(pos int) SimpleItem {
if s.filterResult == nil {
return s.items[pos]
}
return s.items[s.filterResult[pos].Index]
}
func (s *SimpleAdapter) ItemAt(pos int) SimpleItem {
return s.items[pos]
}
func (s *SimpleAdapter) SetItemAt(pos int, item SimpleItem) {
s.items[pos] = item
if s.filterResult != nil {
s.Filter(s.lastFilterPattern)
}
}
func (s *SimpleAdapter) SetItems(items SimpleItemList) {
s.items = items
if s.filterResult != nil {
s.Filter(s.lastFilterPattern)
}
}