Skip to content

Commit 5cf385d

Browse files
authored
Merge pull request #13 from koki-develop/fix-redundant-filtering
2 parents d575349 + 90e1709 commit 5cf385d

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

fzf.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ func New(opts ...Option) *FZF {
3030

3131
// Find launches the Fuzzy Finder and returns a list of indexes of the selected items.
3232
func (fzf *FZF) Find(items interface{}, itemFunc func(i int) string, opts ...FindOption) ([]int, error) {
33-
o := defaultFindOption
33+
findOption := defaultFindOption
3434
for _, opt := range opts {
35-
opt(&o)
35+
opt(&findOption)
3636
}
3737

3838
rv := reflect.ValueOf(items)
@@ -43,11 +43,11 @@ func (fzf *FZF) Find(items interface{}, itemFunc func(i int) string, opts ...Fin
4343
return nil, fmt.Errorf("items must be a slice, but got %T", items)
4444
}
4545

46-
is, err := newItems(rv, itemFunc, o.itemPrefixFunc)
46+
is, err := newItems(rv, itemFunc)
4747
if err != nil {
4848
return nil, err
4949
}
50-
m := newModel(fzf, is)
50+
m := newModel(fzf, is, &findOption)
5151

5252
p := tea.NewProgram(m)
5353
if _, err := p.Run(); err != nil {

item.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ import (
55
)
66

77
type items struct {
8-
items reflect.Value
9-
itemFunc func(i int) string
10-
itemPrefixFunc func(i int) string
8+
items reflect.Value
9+
itemFunc func(i int) string
1110
}
1211

13-
func newItems(rv reflect.Value, itemFunc func(i int) string, itemPrefixFunc func(i int) string) (*items, error) {
12+
func newItems(rv reflect.Value, itemFunc func(i int) string) (*items, error) {
1413
return &items{
15-
items: rv,
16-
itemFunc: itemFunc,
17-
itemPrefixFunc: itemPrefixFunc,
14+
items: rv,
15+
itemFunc: itemFunc,
1816
}, nil
1917
}
2018

@@ -29,7 +27,3 @@ func (is items) Len() int {
2927
return is.items.Len()
3028
}
3129
}
32-
33-
func (is items) HasItemPrefixFunc() bool {
34-
return is.itemPrefixFunc != nil
35-
}

model.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ var (
1515
)
1616

1717
type model struct {
18-
fzf *FZF
19-
items *items
18+
fzf *FZF
19+
items *items
20+
findOption *findOption
2021

2122
// state
2223
abort bool
@@ -46,7 +47,7 @@ type model struct {
4647
input textinput.Model
4748
}
4849

49-
func newModel(fzf *FZF, items *items) *model {
50+
func newModel(fzf *FZF, items *items, opt *findOption) *model {
5051
input := textinput.New()
5152
input.Prompt = fzf.option.prompt
5253
input.Placeholder = fzf.option.inputPlaceholder
@@ -56,9 +57,18 @@ func newModel(fzf *FZF, items *items) *model {
5657
fzf.option.keymap.Toggle.SetEnabled(false)
5758
}
5859

60+
var matches fuzzy.Matches
61+
for i := 0; i < items.Len(); i++ {
62+
matches = append(matches, fuzzy.Match{
63+
Str: items.String(i),
64+
Index: i,
65+
})
66+
}
67+
5968
return &model{
60-
fzf: fzf,
61-
items: items,
69+
fzf: fzf,
70+
items: items,
71+
findOption: opt,
6272
// state
6373
abort: false,
6474

@@ -75,7 +85,7 @@ func newModel(fzf *FZF, items *items) *model {
7585
cursorLineStyle: fzf.option.styles.option.cursorLine,
7686
cursorLineMatchesStyle: lipgloss.NewStyle().Inherit(fzf.option.styles.option.matches).Inherit(fzf.option.styles.option.cursorLine),
7787

78-
matches: fuzzy.Matches{},
88+
matches: matches,
7989
choices: []int{},
8090
// window
8191
windowWidth: 0,
@@ -150,8 +160,8 @@ func (m *model) itemsView() string {
150160
}
151161

152162
// write item prefix
153-
if m.items.HasItemPrefixFunc() {
154-
_, _ = v.WriteString(stringLinesToSpace(m.items.itemPrefixFunc(match.Index)))
163+
if m.findOption.itemPrefixFunc != nil {
164+
_, _ = v.WriteString(stringLinesToSpace(m.findOption.itemPrefixFunc(match.Index)))
155165
}
156166

157167
// write item
@@ -170,7 +180,7 @@ func (m *model) itemsView() string {
170180
}
171181
}
172182

173-
if i+1 == m.windowHeight-headerHeight {
183+
if i+1 >= m.windowHeight-headerHeight {
174184
break
175185
}
176186
v.WriteString("\n")
@@ -202,27 +212,35 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
202212
case key.Matches(msg, m.fzf.option.keymap.Up):
203213
// up
204214
m.cursorUp()
215+
m.fixYPosition()
216+
m.fixCursor()
205217
case key.Matches(msg, m.fzf.option.keymap.Down):
206218
// down
207219
m.cursorDown()
220+
m.fixYPosition()
221+
m.fixCursor()
208222
}
209223
case tea.WindowSizeMsg:
210224
// window
211225
m.windowWidth = msg.Width
212226
m.windowHeight = msg.Height
213227
m.input.Width = m.windowWidth - m.promptWidth
228+
m.fixYPosition()
229+
m.fixCursor()
214230
}
215231

216232
var cmds []tea.Cmd
233+
beforeValue := m.input.Value()
217234
{
218235
input, cmd := m.input.Update(msg)
219236
m.input = input
220237
cmds = append(cmds, cmd)
221238
}
222-
223-
m.filter()
224-
m.fixYPosition()
225-
m.fixCursor()
239+
if beforeValue != m.input.Value() {
240+
m.filter()
241+
m.fixYPosition()
242+
m.fixCursor()
243+
}
226244

227245
return m, tea.Batch(cmds...)
228246
}
@@ -267,10 +285,9 @@ func (m *model) cursorDown() {
267285
}
268286

269287
func (m *model) filter() {
270-
var matches fuzzy.Matches
271-
272288
s := m.input.Value()
273289
if s == "" {
290+
var matches fuzzy.Matches
274291
for i := 0; i < m.items.Len(); i++ {
275292
matches = append(matches, fuzzy.Match{
276293
Str: m.items.String(i),

0 commit comments

Comments
 (0)