Work on implementing the fzf algorithm for the choose files kitten

People are more used to that, and it is more optimized for use case of
finding files.
This commit is contained in:
Kovid Goyal
2025-06-06 12:29:35 +05:30
parent 3059c87bd0
commit ed45e1354b
6 changed files with 816 additions and 4 deletions

48
tools/fzf/api.go Normal file
View File

@@ -0,0 +1,48 @@
package fzf
import (
"fmt"
)
var _ = fmt.Print
func NewFuzzyMatcher(scheme Scheme) (ans *FuzzyMatcher) {
return new_fuzzy_matcher(scheme)
}
// Score the specified items using runtime.NumCPU() go routines. This function
// reports a panic in any worker go routine as a regular error.
func (m *FuzzyMatcher) Score(items []string, pattern string) (ans []Result, err error) {
return m.score(items, pattern, func(item string, pat []rune, pattern_is_ascii bool, slab *slab, as_chars func(string) Chars) Result {
c := as_chars(item)
return m.score_one(&c, pat, pattern_is_ascii, slab)
})
}
// Clear the cache used ScoreWithCache(). Useful if you change some of the
// settings used for scoring.
func (m *FuzzyMatcher) ClearScoreCache() {
m.cache_mutex.Lock()
m.cache = make(map[string]Result)
m.cache_mutex.Unlock()
}
// Same as Score, except that it uses a cache. Remember to call
// ClearScoreCache() if you change any scoring settings on this FuzzyMatcher.
func (m *FuzzyMatcher) ScoreWithCache(items []string, pattern string) (ans []Result, err error) {
key_prefix := pattern + "\x00"
return m.score(items, pattern, func(item string, pat []rune, pattern_is_ascii bool, slab *slab, as_chars func(string) Chars) Result {
key := key_prefix + item
m.cache_mutex.Lock()
res, found := m.cache[key]
m.cache_mutex.Unlock()
if !found {
c := as_chars(item)
res = m.score_one(&c, pat, pattern_is_ascii, slab)
m.cache_mutex.Lock()
m.cache[key] = res
m.cache_mutex.Unlock()
}
return res
})
}