Skip to content

Commit

Permalink
Merge pull request #68 from koki-develop/feature/with-preselect
Browse files Browse the repository at this point in the history
  • Loading branch information
koki-develop committed Jul 28, 2023
2 parents f85e7e6 + f1b6700 commit f6dce46
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
1 change: 1 addition & 0 deletions examples/preselect/README.md
@@ -0,0 +1 @@
![](./demo.gif)
Binary file added examples/preselect/demo.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions examples/preselect/main.go
@@ -0,0 +1,30 @@
package main

import (
"fmt"
"log"

"github.com/koki-develop/go-fzf"
)

func main() {
items := []string{"hello", "world", "foo", "bar"}

f, err := fzf.New(fzf.WithNoLimit(true))
if err != nil {
log.Fatal(err)
}

idxs, err := f.Find(
items,
func(i int) string { return items[i] },
fzf.WithPreselect([]int{0, 2}),
)
if err != nil {
log.Fatal(err)
}

for _, i := range idxs {
fmt.Println(items[i])
}
}
22 changes: 18 additions & 4 deletions fzf.go
Expand Up @@ -13,6 +13,7 @@ var defaultFindOption = findOption{
itemPrefixFunc: nil,
previewWindowFunc: nil,
preselectAll: false,
preselect: []int{},
}

// Fuzzy Finder.
Expand Down Expand Up @@ -64,10 +65,15 @@ func (fzf *FZF) Find(items interface{}, itemFunc func(i int) string, opts ...Fin
}
fzf.model.loadItems(is)
fzf.model.setFindOption(&findOption)
if fzf.model.option.noLimit && findOption.preselectAll {
fzf.model.choices = make([]int, is.Len())
for i := 0; i < is.Len(); i++ {
fzf.model.choices[i] = i
if fzf.model.option.noLimit {
if findOption.preselectAll {
fzf.model.choices = make([]int, is.Len())
for i := 0; i < is.Len(); i++ {
fzf.model.choices[i] = i
}
} else {
fzf.model.choices = make([]int, len(findOption.preselect))
copy(fzf.model.choices, findOption.preselect)
}
}

Expand Down Expand Up @@ -111,6 +117,7 @@ type findOption struct {
itemPrefixFunc func(i int) string
previewWindowFunc func(i, width, height int) string
preselectAll bool
preselect []int
}

// WithItemPrefix sets the prefix function of the item.
Expand All @@ -132,3 +139,10 @@ func WithPreselectAll(preselect bool) FindOption {
o.preselectAll = preselect
}
}

// WithPreselect sets the preselected indexes.
func WithPreselect(idxs []int) FindOption {
return func(o *findOption) {
o.preselect = idxs
}
}
36 changes: 36 additions & 0 deletions tapes/library/preselect.tape
@@ -0,0 +1,36 @@
# configuration
Output ./examples/preselect/demo.gif
Set Shell "bash"
Set FontSize 32
Set Width 1200
Set Height 600

# setup
Hide
Type "mkdir ./tmp" Enter
Type "cp ./examples/preselect/main.go ./tmp/main.go" Enter
Type "cd ./tmp" Enter
Ctrl+l
Show

# ---

Type "go run ./main.go" Sleep 750ms Enter
Sleep 2s

Down 2
Sleep 1s

Up 2
Sleep 1s

Enter

Sleep 3s

# ---

# cleanup
Hide
Type "cd ../" Enter
Type "\rm -rf ./tmp" Enter

0 comments on commit f6dce46

Please sign in to comment.