Skip to content

Commit

Permalink
Merge pull request #18 from koki-develop/case-sensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
koki-develop committed Mar 26, 2023
2 parents 614c05e + b17f940 commit ec9042a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
8 changes: 6 additions & 2 deletions cmd/gofzf/main.go
Expand Up @@ -24,8 +24,9 @@ var (
)

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

flagPrompt string
flagCursor string
Expand Down Expand Up @@ -91,6 +92,7 @@ var rootCmd = &cobra.Command{
f := fzf.New(
fzf.WithNoLimit(flagNoLimit),
fzf.WithLimit(flagLimit),
fzf.WithCaseSensitive(flagCaseSensitive),

fzf.WithHotReload(mu.RLocker()),

Expand Down Expand Up @@ -259,6 +261,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

0 comments on commit ec9042a

Please sign in to comment.