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

support toggle case sensitivity #18

Merged
merged 3 commits 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
8 changes: 6 additions & 2 deletions cmd/gofzf/main.go
Expand Up @@ -21,8 +21,9 @@ var (
)

var (
flagLimit int
flagNoLimit bool
flagLimit int
flagNoLimit bool
flagCaseSensitive bool

flagPrompt string
flagCursor string
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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", "● ", "")
Expand Down
2 changes: 1 addition & 1 deletion model.go
Expand Up @@ -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() {
Expand Down
17 changes: 13 additions & 4 deletions option.go
Expand Up @@ -9,8 +9,9 @@ import (
)

var defaultOption = option{
limit: 1,
noLimit: false,
limit: 1,
noLimit: false,
caseSensitive: false,

prompt: "> ",
cursor: "> ",
Expand Down Expand Up @@ -42,8 +43,9 @@ var defaultOption = option{
}

type option struct {
limit int
noLimit bool
limit int
noLimit bool
caseSensitive bool

prompt string
cursor string
Expand Down Expand Up @@ -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
}
}
14 changes: 12 additions & 2 deletions search.go
Expand Up @@ -2,6 +2,7 @@ package fzf

import (
"sort"
"strings"
"sync"
)

Expand Down Expand Up @@ -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{}
Expand All @@ -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

Expand All @@ -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)
}
}
Expand Down