Skip to content

Commit

Permalink
Merge pull request #4 from koki-develop/allow-pass-pointer-to-slice
Browse files Browse the repository at this point in the history
  • Loading branch information
koki-develop committed Mar 25, 2023
2 parents 5d90481 + 3754cb8 commit 6b66fa6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
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

0 comments on commit 6b66fa6

Please sign in to comment.