From e2b4b8be5173e93b5d215f236ea81bc7b191dd7d Mon Sep 17 00:00:00 2001 From: koki-develop Date: Sun, 26 Mar 2023 10:03:32 +0900 Subject: [PATCH] count view can now be customized --- model.go | 14 ++++---------- option.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/model.go b/model.go index 49540b2..5ef0bde 100644 --- a/model.go +++ b/model.go @@ -2,7 +2,6 @@ package fzf import ( "fmt" - "strconv" "strings" "github.com/charmbracelet/bubbles/key" @@ -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() } diff --git a/option.go b/option.go index 02b32f0..9f78862 100644 --- a/option.go +++ b/option.go @@ -1,6 +1,9 @@ package fzf import ( + "strconv" + "strings" + "github.com/charmbracelet/bubbles/key" ) @@ -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 { @@ -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. @@ -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 + } +}