Thu 1 Aug 15:37:45 CEST 2024

This commit is contained in:
Thomas Avé 2024-08-01 15:37:45 +02:00
parent 3cf5fca630
commit c21c237959
4 changed files with 312699 additions and 13 deletions

84
data.go
View File

@ -5,36 +5,97 @@ import (
"fmt"
"os"
"sort"
"strings"
"sync"
"github.com/charmbracelet/bubbles/list"
)
type Ranking struct {
items []string
indexes []int
filter string
scores []float64
}
func (a Ranking) Len() int { return len(a.items) }
func (a Ranking) Swap(i, j int) {
a.indexes[i], a.indexes[j] = a.indexes[j], a.indexes[i]
func (r Ranking) Len() int { return len(r.items) }
func (r Ranking) Swap(i, j int) {
r.indexes[i], r.indexes[j] = r.indexes[j], r.indexes[i]
}
func (a Ranking) Less(i, j int) bool {
return len(a.items[a.indexes[i]]) < len(a.items[a.indexes[j]])
func (r Ranking) Less(i, j int) bool {
return r.scores[r.indexes[i]] > r.scores[r.indexes[j]]
}
func Filter(term string, targets []string) []list.Rank {
// Function to compute the score for a given string
func computeScore(term string, s string) float64 {
term = strings.ToLower(term)
a := strings.ToLower(s)
splits := strings.Split(a, "/")
term_splits := strings.Split(term, "/")
score := 1.0 / float64(len(splits))
for _, term := range term_splits {
for i, c := range splits {
if len(term) == 0 {
continue
}
if strings.Contains(c, term) {
score += 1.0/float64(i)
splits = splits[i:]
break
}
}
}
return score
}
func rank(term string, targets []string) []int {
scores := make([]float64, len(targets))
var wg sync.WaitGroup
scoreChan := make(chan struct {
index int
score float64
}, len(targets))
// Start a goroutine for each string to compute its score
for i, s := range targets {
wg.Add(1)
go func(i int, s string) {
defer wg.Done()
score := computeScore(term, s)
scoreChan <- struct {
index int
score float64
}{index: i, score: score}
}(i, s)
}
// Close the channel once all goroutines are done
go func() {
wg.Wait()
close(scoreChan)
}()
// Collect scores from the channel
for result := range scoreChan {
scores[result.index] = result.score
}
indexes := make([]int, len(targets))
for i := range targets {
indexes[i] = i
}
ranking := Ranking{targets, indexes, term}
sort.Stable(ranking)
sort.Stable(Ranking{targets, indexes, scores})
return indexes
}
func Filter(term string, targets []string) []list.Rank {
indexes := rank(term, targets)
result := make([]list.Rank, len(targets))
for i := range targets {
result[i] = list.Rank{
Index: indexes[i],
MatchedIndexes: []int{i},
MatchedIndexes: []int{},
}
}
return result
@ -70,7 +131,6 @@ func getPaths() []string {
func getListItems() []list.Item {
items := []list.Item{}
paths := getPaths()
// TODO: sort initial list based on most often used
for _, path := range paths {
items = append(items, item{path})
}

3
go.mod
View File

@ -6,6 +6,8 @@ require (
github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.26.6
github.com/charmbracelet/lipgloss v0.9.1
github.com/muesli/termenv v0.15.2
github.com/texttheater/golang-levenshtein/levenshtein v0.0.0-20200805054039-cae8b0eaed6c
)
require (
@ -23,7 +25,6 @@ require (
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect

2
go.sum
View File

@ -41,6 +41,8 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/texttheater/golang-levenshtein/levenshtein v0.0.0-20200805054039-cae8b0eaed6c h1:HelZ2kAFadG0La9d+4htN4HzQ68Bm2iM9qKMSMES6xg=
github.com/texttheater/golang-levenshtein/levenshtein v0.0.0-20200805054039-cae8b0eaed6c/go.mod h1:JlzghshsemAMDGZLytTFY8C1JQxQPhnatWqNwUXjggo=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=

312623
out Normal file

File diff suppressed because it is too large Load Diff