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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

count view can now be customized #7

Merged
merged 1 commit 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
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
}
}