diff --git a/cmd/gofzf/main.go b/cmd/gofzf/main.go index c45e706..4491bfc 100644 --- a/cmd/gofzf/main.go +++ b/cmd/gofzf/main.go @@ -21,8 +21,9 @@ var ( ) var ( - flagLimit int - flagNoLimit bool + flagLimit int + flagNoLimit bool + flagCaseSensitive bool flagPrompt string flagCursor string @@ -125,6 +126,7 @@ var rootCmd = &cobra.Command{ f := fzf.New( fzf.WithNoLimit(flagNoLimit), fzf.WithLimit(flagLimit), + fzf.WithCaseSensitive(flagCaseSensitive), fzf.WithPrompt(flagPrompt), fzf.WithCursor(flagCursor), @@ -222,6 +224,8 @@ func init() { rootCmd.Flags().BoolVar(&flagNoLimit, "no-limit", false, "unlimited number of items to select") rootCmd.MarkFlagsMutuallyExclusive("limit", "no-limit") + rootCmd.Flags().BoolVar(&flagCaseSensitive, "case-sensitive", false, "case sensitive search") + rootCmd.Flags().StringVar(&flagPrompt, "prompt", "> ", "") rootCmd.Flags().StringVar(&flagCursor, "cursor", "> ", "") rootCmd.Flags().StringVar(&flagSelectedPrefix, "selected-prefix", "● ", "") diff --git a/model.go b/model.go index 9ae1946..c0b8325 100644 --- a/model.go +++ b/model.go @@ -329,7 +329,7 @@ func (m *model) filter() { return } - m.matches = fuzzySearch(m.items, s) + m.matches = fuzzySearch(m.items, s, m.option.caseSensitive) } func (m *model) fixCursor() { diff --git a/option.go b/option.go index ce643b1..a878ff9 100644 --- a/option.go +++ b/option.go @@ -9,8 +9,9 @@ import ( ) var defaultOption = option{ - limit: 1, - noLimit: false, + limit: 1, + noLimit: false, + caseSensitive: false, prompt: "> ", cursor: "> ", @@ -42,8 +43,9 @@ var defaultOption = option{ } type option struct { - limit int - noLimit bool + limit int + noLimit bool + caseSensitive bool prompt string cursor string @@ -164,3 +166,10 @@ func WithHotReload(locker sync.Locker) Option { o.hotReloadLocker = locker } } + +// WithCaseSensitive sets the case sensitivity. +func WithCaseSensitive(s bool) Option { + return func(o *option) { + o.caseSensitive = s + } +} diff --git a/search.go b/search.go index 05d8de9..7f2e722 100644 --- a/search.go +++ b/search.go @@ -2,6 +2,7 @@ package fzf import ( "sort" + "strings" "sync" ) @@ -32,7 +33,11 @@ func (m matches) Sort() { }) } -func fuzzySearch(items *items, search string) matches { +func fuzzySearch(items *items, search string, caseSensitive bool) matches { + if !caseSensitive { + search = strings.ToLower(search) + } + result := make(matches, 0, items.Len()) resultMutex := sync.Mutex{} wg := sync.WaitGroup{} @@ -56,6 +61,11 @@ func fuzzySearch(items *items, search string) matches { for index := start; index < end; index++ { item := items.String(index) + + if !caseSensitive { + item = strings.ToLower(item) + } + matchedIndexes := make([]int, 0, len(search)) j := 0 @@ -67,7 +77,7 @@ func fuzzySearch(items *items, search string) matches { } if j == len(search) { - m := match{Str: item, Index: index, MatchedIndexes: matchedIndexes} + m := match{Str: items.String(index), Index: index, MatchedIndexes: matchedIndexes} localMatches = append(localMatches, m) } }