/
model.go
313 lines (257 loc) Β· 6.19 KB
/
model.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
package fzf
import (
"fmt"
"strconv"
"strings"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/sahilm/fuzzy"
)
var (
_ tea.Model = (*model)(nil)
)
type model struct {
fzf *FZF
items *items
// state
abort bool
cursor string
nocursor string
cursorPosition int
selectedPrefix string
unselectedPrefix string
matchesStyle lipgloss.Style
cursorLineStyle lipgloss.Style
cursorLineMatchesStyle lipgloss.Style
matches fuzzy.Matches
choices []int
// window
windowWidth int
windowHeight int
windowYPosition int
// components
input textinput.Model
}
func newModel(fzf *FZF, items *items) *model {
input := textinput.New()
input.Prompt = fzf.option.prompt
input.Placeholder = fzf.option.inputPlaceholder
input.Focus()
if !fzf.multiple() {
fzf.option.keymap.Toggle.SetEnabled(false)
}
return &model{
fzf: fzf,
items: items,
// state
abort: false,
cursor: fzf.option.styles.option.cursor.Render(fzf.option.cursor),
nocursor: strings.Repeat(" ", lipgloss.Width(fzf.option.cursor)),
cursorPosition: 0,
selectedPrefix: fzf.option.styles.option.selectedPrefix.Render(fzf.option.selectedPrefix),
unselectedPrefix: fzf.option.styles.option.unselectedPrefix.Render(fzf.option.unselectedPrefix),
matchesStyle: fzf.option.styles.option.matches,
cursorLineStyle: fzf.option.styles.option.cursorLine,
cursorLineMatchesStyle: lipgloss.NewStyle().Inherit(fzf.option.styles.option.matches).Inherit(fzf.option.styles.option.cursorLine),
matches: fuzzy.Matches{},
choices: []int{},
// window
windowWidth: 0,
windowHeight: 0,
windowYPosition: 0,
// components
input: input,
}
}
func (m *model) Init() tea.Cmd {
return tea.Batch(
textinput.Blink,
tea.EnterAltScreen,
)
}
/*
* view
*/
func (m *model) View() string {
return fmt.Sprintf("%s\n%s", m.headerView(), m.itemsView())
}
func (m *model) headerView() string {
var v strings.Builder
// input
_, _ = v.WriteString(m.input.View())
_, _ = v.WriteRune('\n')
// count
var cv strings.Builder
_, _ = cv.WriteString(strconv.Itoa(m.matches.Len()))
_, _ = cv.WriteRune('/')
_, _ = cv.WriteString(strconv.Itoa(m.items.Len()))
_, _ = cv.WriteRune(' ')
_, _ = v.WriteString(cv.String())
_, _ = v.WriteString(strings.Repeat("β", max(m.windowWidth-cv.Len(), 0)))
return v.String()
}
func (m *model) headerHeight() int {
return lipgloss.Height(m.headerView())
}
func (m *model) itemsView() string {
var v strings.Builder
headerHeight := m.headerHeight()
for i, match := range m.matches[m.windowYPosition:] {
cursorLine := m.cursorPosition == i+m.windowYPosition
// write cursor
if cursorLine {
_, _ = v.WriteString(m.cursor)
} else {
_, _ = v.WriteString(m.nocursor)
}
// write toggle
if m.fzf.multiple() {
if intContains(m.choices, match.Index) {
_, _ = v.WriteString(m.selectedPrefix)
} else {
_, _ = v.WriteString(m.unselectedPrefix)
}
}
// write item prefix
if m.items.HasItemPrefixFunc() {
_, _ = v.WriteString(stringLinesToSpace(m.items.itemPrefixFunc(match.Index)))
}
// write item
for ci, c := range match.Str {
// matches
if intContains(match.MatchedIndexes, ci) {
if cursorLine {
_, _ = v.WriteString(m.cursorLineMatchesStyle.Render(string(c)))
} else {
_, _ = v.WriteString(m.matchesStyle.Render(string(c)))
}
} else if cursorLine {
_, _ = v.WriteString(m.cursorLineStyle.Render(string(c)))
} else {
_, _ = v.WriteRune(c)
}
}
if i+1 == m.windowHeight-headerHeight {
break
}
v.WriteString("\n")
}
return v.String()
}
/*
* update
*/
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
// key
switch {
case key.Matches(msg, m.fzf.option.keymap.Abort):
// abort
m.abort = true
return m, tea.Quit
case key.Matches(msg, m.fzf.option.keymap.Choose):
// choose
m.choice()
return m, tea.Quit
case key.Matches(msg, m.fzf.option.keymap.Toggle):
// toggle
m.toggle()
case key.Matches(msg, m.fzf.option.keymap.Up):
// up
m.cursorUp()
case key.Matches(msg, m.fzf.option.keymap.Down):
// down
m.cursorDown()
}
case tea.WindowSizeMsg:
// window
m.windowWidth = msg.Width
m.windowHeight = msg.Height
m.input.Width = m.windowWidth - 3
}
var cmds []tea.Cmd
{
input, cmd := m.input.Update(msg)
m.input = input
cmds = append(cmds, cmd)
}
m.filter()
m.fixYPosition()
m.fixCursor()
return m, tea.Batch(cmds...)
}
func (m *model) choice() {
if len(m.choices) > 0 {
return
}
if m.matches.Len() == 0 {
return
}
m.choices = append(m.choices, m.matches[m.cursorPosition].Index)
}
func (m *model) toggle() {
if m.matches.Len() == 0 {
return
}
match := m.matches[m.cursorPosition]
if intContains(m.choices, match.Index) {
m.choices = intFilter(m.choices, func(i int) bool { return i != match.Index })
} else {
if m.fzf.option.noLimit || len(m.choices) < m.fzf.option.limit {
m.choices = append(m.choices, match.Index)
}
}
}
func (m *model) cursorUp() {
if m.cursorPosition > 0 {
m.cursorPosition--
}
}
func (m *model) cursorDown() {
if m.cursorPosition+1 < len(m.matches) {
m.cursorPosition++
}
}
func (m *model) filter() {
var matches fuzzy.Matches
s := m.input.Value()
if s == "" {
for i := 0; i < m.items.Len(); i++ {
matches = append(matches, fuzzy.Match{
Str: m.items.String(i),
Index: i,
})
}
m.matches = matches
return
}
m.matches = fuzzy.FindFrom(s, m.items)
}
func (m *model) fixCursor() {
if m.cursorPosition < 0 {
m.cursorPosition = 0
return
}
if m.cursorPosition+1 > len(m.matches) {
m.cursorPosition = len(m.matches) - 1
return
}
}
func (m *model) fixYPosition() {
headerHeight := m.headerHeight()
if m.windowHeight-headerHeight > len(m.matches) {
m.windowYPosition = 0
return
}
if m.cursorPosition < m.windowYPosition {
m.windowYPosition = m.cursorPosition
return
}
if m.cursorPosition+1 >= (m.windowHeight-headerHeight)+m.windowYPosition {
m.windowYPosition = m.cursorPosition + 1 - (m.windowHeight - headerHeight)
return
}
}