Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove redundant filtering #13

Merged
merged 4 commits into from Mar 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions fzf.go
Expand Up @@ -30,9 +30,9 @@ func New(opts ...Option) *FZF {

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

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

is, err := newItems(rv, itemFunc, o.itemPrefixFunc)
is, err := newItems(rv, itemFunc)
if err != nil {
return nil, err
}
m := newModel(fzf, is)
m := newModel(fzf, is, &findOption)

p := tea.NewProgram(m)
if _, err := p.Run(); err != nil {
Expand Down
16 changes: 5 additions & 11 deletions item.go
Expand Up @@ -5,16 +5,14 @@ import (
)

type items struct {
items reflect.Value
itemFunc func(i int) string
itemPrefixFunc func(i int) string
items reflect.Value
itemFunc func(i int) string
}

func newItems(rv reflect.Value, itemFunc func(i int) string, itemPrefixFunc func(i int) string) (*items, error) {
func newItems(rv reflect.Value, itemFunc func(i int) string) (*items, error) {
return &items{
items: rv,
itemFunc: itemFunc,
itemPrefixFunc: itemPrefixFunc,
items: rv,
itemFunc: itemFunc,
}, nil
}

Expand All @@ -29,7 +27,3 @@ func (is items) Len() int {
return is.items.Len()
}
}

func (is items) HasItemPrefixFunc() bool {
return is.itemPrefixFunc != nil
}
47 changes: 32 additions & 15 deletions model.go
Expand Up @@ -15,8 +15,9 @@ var (
)

type model struct {
fzf *FZF
items *items
fzf *FZF
items *items
findOption *findOption

// state
abort bool
Expand Down Expand Up @@ -46,7 +47,7 @@ type model struct {
input textinput.Model
}

func newModel(fzf *FZF, items *items) *model {
func newModel(fzf *FZF, items *items, opt *findOption) *model {
input := textinput.New()
input.Prompt = fzf.option.prompt
input.Placeholder = fzf.option.inputPlaceholder
Expand All @@ -56,9 +57,18 @@ func newModel(fzf *FZF, items *items) *model {
fzf.option.keymap.Toggle.SetEnabled(false)
}

var matches fuzzy.Matches
for i := 0; i < items.Len(); i++ {
matches = append(matches, fuzzy.Match{
Str: items.String(i),
Index: i,
})
}

return &model{
fzf: fzf,
items: items,
fzf: fzf,
items: items,
findOption: opt,
// state
abort: false,

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

matches: fuzzy.Matches{},
matches: matches,
choices: []int{},
// window
windowWidth: 0,
Expand Down Expand Up @@ -150,8 +160,8 @@ func (m *model) itemsView() string {
}

// write item prefix
if m.items.HasItemPrefixFunc() {
_, _ = v.WriteString(stringLinesToSpace(m.items.itemPrefixFunc(match.Index)))
if m.findOption.itemPrefixFunc != nil {
_, _ = v.WriteString(stringLinesToSpace(m.findOption.itemPrefixFunc(match.Index)))
}

// write item
Expand All @@ -170,7 +180,7 @@ func (m *model) itemsView() string {
}
}

if i+1 == m.windowHeight-headerHeight {
if i+1 >= m.windowHeight-headerHeight {
break
}
v.WriteString("\n")
Expand Down Expand Up @@ -202,27 +212,35 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, m.fzf.option.keymap.Up):
// up
m.cursorUp()
m.fixYPosition()
m.fixCursor()
case key.Matches(msg, m.fzf.option.keymap.Down):
// down
m.cursorDown()
m.fixYPosition()
m.fixCursor()
}
case tea.WindowSizeMsg:
// window
m.windowWidth = msg.Width
m.windowHeight = msg.Height
m.input.Width = m.windowWidth - m.promptWidth
m.fixYPosition()
m.fixCursor()
}

var cmds []tea.Cmd
beforeValue := m.input.Value()
{
input, cmd := m.input.Update(msg)
m.input = input
cmds = append(cmds, cmd)
}

m.filter()
m.fixYPosition()
m.fixCursor()
if beforeValue != m.input.Value() {
m.filter()
m.fixYPosition()
m.fixCursor()
}

return m, tea.Batch(cmds...)
}
Expand Down Expand Up @@ -267,10 +285,9 @@ func (m *model) cursorDown() {
}

func (m *model) filter() {
var matches fuzzy.Matches

s := m.input.Value()
if s == "" {
var matches fuzzy.Matches
for i := 0; i < m.items.Len(); i++ {
matches = append(matches, fuzzy.Match{
Str: m.items.String(i),
Expand Down