Skip to content

Commit

Permalink
fix: improved general matching behaviour, made visibility_threshold l…
Browse files Browse the repository at this point in the history
…ess aggressive
  • Loading branch information
abenz1267 committed Jan 26, 2025
1 parent a47c5c6 commit 1f224d4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
4 changes: 2 additions & 2 deletions internal/modules/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ func (e Plugin) Entries(term string) []util.Entry {
Sub: e.Config.Name,
Output: e.Config.Src,
ScoreFinal: score,
RecalculateScore: false,
RecalculateScore: true,
Categories: e.Config.Keywords,
Matching: util.AlwaysTop,
Matching: util.TopWhenFuzzyMatch,
Icon: e.Config.Icon,
}

Expand Down
49 changes: 35 additions & 14 deletions internal/ui/interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,11 +811,17 @@ func processAsync(text string) {
} else {
e[k].ScoreFinal = 1000
}
case util.Fuzzy:
case util.Fuzzy, util.TopWhenFuzzyMatch:
e[k].ScoreFinal = fuzzyScore(&e[k], toMatch, g.History)

if e[k].Matching == util.TopWhenFuzzyMatch {
if e[k].ScoreFinal > 0 {
e[k].ScoreFinal = 10000
}
}
case util.AlwaysTop:
if e[k].ScoreFinal == 0 {
e[k].ScoreFinal = 1000
e[k].ScoreFinal = 10000
}
case util.AlwaysBottom:
if e[k].ScoreFinal == 0 {
Expand All @@ -841,18 +847,16 @@ func processAsync(text string) {
}
}
} else {
if e[k].ScoreFinal > float64(config.Cfg.List.VisibilityThreshold) {
if e[k].Prefix != "" && strings.HasPrefix(text, e[k].Prefix) {
hasEntryPrefix = true
if e[k].Prefix != "" && strings.HasPrefix(text, e[k].Prefix) {
hasEntryPrefix = true

toPush = append(toPush, e[k])
} else {
if e[k].IgnoreUnprefixed {
continue
}

toPush = append(toPush, e[k])
toPush = append(toPush, e[k])
} else {
if e[k].IgnoreUnprefixed {
continue
}

toPush = append(toPush, e[k])
}
}
}
Expand All @@ -865,6 +869,19 @@ func processAsync(text string) {

wg.Wait()

// remove entries that don't match the visibility threshold if list is too long
if len(entries) > config.Cfg.List.MaxEntries {
filteredEntries := []util.Entry{}

for _, v := range entries {
if v.ScoreFinal > float64(config.Cfg.List.VisibilityThreshold) {
filteredEntries = append(filteredEntries, v)
}
}

entries = filteredEntries
}

if query != lastQuery {
return
}
Expand Down Expand Up @@ -1124,8 +1141,12 @@ func fuzzyScore(entry *util.Entry, text string, useHistory bool) float64 {
var matchables []string

if !appstate.IsDmenu {
matchables = []string{entry.Label, entry.Sub, entry.Searchable, entry.Searchable2}
matchables = []string{entry.Sub, entry.Searchable, entry.Searchable2}
matchables = append(matchables, entry.Categories...)

if entry.Output == "" {
matchables = append([]string{entry.Label}, matchables...)
}
} else {
matchables = []string{entry.Label}
}
Expand Down Expand Up @@ -1166,7 +1187,7 @@ func fuzzyScore(entry *util.Entry, text string, useHistory bool) float64 {
score, pos = util.FuzzyScore(text, t)
}

if score < 2 {
if score < 1 {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion internal/util/fzf.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func FuzzyScore(input, target string) (float64, *[]int) {
chars := util.ToChars([]byte(target))
res, pos := algo.FuzzyMatchV2(false, true, true, &chars, []rune(strings.ToLower(input)), true, nil)

return float64(res.Score - res.Start), pos
return float64(res.Score), pos
}

func ExactScore(input, target string) (float64, *[]int) {
Expand Down
1 change: 1 addition & 0 deletions internal/util/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type MatchingType int
const (
Fuzzy MatchingType = iota
AlwaysTop
TopWhenFuzzyMatch
AlwaysBottom
AlwaysTopOnEmptySearch
)
Expand Down

0 comments on commit 1f224d4

Please sign in to comment.