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

allow pass pointer to slice #4

Merged
merged 1 commit into from Mar 25, 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
17 changes: 15 additions & 2 deletions fzf.go
@@ -1,6 +1,11 @@
package fzf

import tea "github.com/charmbracelet/bubbletea"
import (
"fmt"
"reflect"

tea "github.com/charmbracelet/bubbletea"
)

var defaultFindOption = findOption{
itemPrefixFunc: nil,
Expand Down Expand Up @@ -30,7 +35,15 @@ func (fzf *FZF) Find(items interface{}, itemFunc func(i int) string, opts ...Fin
opt(&o)
}

is, err := newItems(items, itemFunc, o.itemPrefixFunc)
rv := reflect.ValueOf(items)
switch {
case rv.Kind() == reflect.Slice:
case rv.Kind() == reflect.Ptr && reflect.Indirect(rv).Kind() == reflect.Slice:
default:
return nil, fmt.Errorf("items must be a slice, but got %T", items)
}

is, err := newItems(rv, itemFunc, o.itemPrefixFunc)
if err != nil {
return nil, err
}
Expand Down
14 changes: 6 additions & 8 deletions item.go
@@ -1,7 +1,6 @@
package fzf

import (
"fmt"
"reflect"
)

Expand All @@ -11,12 +10,7 @@ type items struct {
itemPrefixFunc func(i int) string
}

func newItems(is interface{}, itemFunc func(i int) string, itemPrefixFunc func(i int) string) (*items, error) {
rv := reflect.ValueOf(is)
if rv.Kind() != reflect.Slice {
return nil, fmt.Errorf("items must be a slice, but got %T", is)
}

func newItems(rv reflect.Value, itemFunc func(i int) string, itemPrefixFunc func(i int) string) (*items, error) {
return &items{
items: rv,
itemFunc: itemFunc,
Expand All @@ -29,7 +23,11 @@ func (is items) String(i int) string {
}

func (is items) Len() int {
return is.items.Len()
if is.items.Kind() == reflect.Ptr {
return reflect.Indirect(is.items).Len()
} else {
return is.items.Len()
}
}

func (is items) HasItemPrefixFunc() bool {
Expand Down