Skip to content

Commit

Permalink
Merge pull request #7 from koki-develop/custom-count-view
Browse files Browse the repository at this point in the history
  • Loading branch information
koki-develop committed Mar 26, 2023
2 parents c6ce05a + e2b4b8b commit e44823a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
14 changes: 4 additions & 10 deletions model.go
Expand Up @@ -2,7 +2,6 @@ package fzf

import (
"fmt"
"strconv"
"strings"

"github.com/charmbracelet/bubbles/key"
Expand Down Expand Up @@ -104,16 +103,11 @@ func (m *model) headerView() string {

// input
_, _ = v.WriteString(m.input.View())
_, _ = v.WriteRune('\n')

// count
var cv strings.Builder
_, _ = cv.WriteString(strconv.Itoa(m.matches.Len()))
_, _ = cv.WriteRune('/')
_, _ = cv.WriteString(strconv.Itoa(m.items.Len()))
_, _ = cv.WriteRune(' ')
_, _ = v.WriteString(cv.String())
_, _ = v.WriteString(strings.Repeat("─", max(m.windowWidth-cv.Len(), 0)))
if m.fzf.option.countViewEnabled {
_, _ = v.WriteRune('\n')
_, _ = v.WriteString(m.fzf.option.countViewFunc(m.items.Len(), m.matches.Len(), m.windowWidth))
}

return v.String()
}
Expand Down
31 changes: 31 additions & 0 deletions option.go
@@ -1,6 +1,9 @@
package fzf

import (
"strconv"
"strings"

"github.com/charmbracelet/bubbles/key"
)

Expand All @@ -22,6 +25,17 @@ var defaultOption = option{
Choose: key.NewBinding(key.WithKeys("enter")),
Abort: key.NewBinding(key.WithKeys("ctrl+c", "esc")),
},

countViewEnabled: true,
countViewFunc: func(itemsCount, matchesCount, windowWidth int) string {
var v strings.Builder
_, _ = v.WriteString(strconv.Itoa(matchesCount))
_, _ = v.WriteRune('/')
_, _ = v.WriteString(strconv.Itoa(itemsCount))
_, _ = v.WriteRune(' ')
_, _ = v.WriteString(strings.Repeat("─", max(windowWidth-v.Len(), 0)))
return v.String()
},
}

type option struct {
Expand All @@ -36,6 +50,9 @@ type option struct {
styles *Styles

keymap *keymap

countViewEnabled bool
countViewFunc func(itemsCount, matchesCount, windowWidth int) string
}

// Option represents a option for the Fuzzy Finder.
Expand Down Expand Up @@ -117,3 +134,17 @@ func WithInputPlaceholder(p string) Option {
o.inputPlaceholder = p
}
}

// WithCountViewEnabled enables or disables count view.
func WithCountViewEnabled(b bool) Option {
return func(o *option) {
o.countViewEnabled = b
}
}

// WithCountView sets the function to create the count view.
func WithCountView(f func(itemsCount, matchesCount, windowWidth int) string) Option {
return func(o *option) {
o.countViewFunc = f
}
}

0 comments on commit e44823a

Please sign in to comment.