mirror of
https://github.com/packwiz/packwiz.git
synced 2025-04-19 21:16:30 +02:00
Sort by levenshtein distance
This commit is contained in:
parent
10beffb30c
commit
310078b1fb
@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -178,20 +179,14 @@ func cmdInstall(flags core.Flags, mod string, modArgsTail []string) error {
|
|||||||
done = true
|
done = true
|
||||||
} else {
|
} else {
|
||||||
// Find the closest value to the search term
|
// Find the closest value to the search term
|
||||||
searchIndex := 0
|
sort.Slice(results, func(i, j int) bool {
|
||||||
currDistance := levenshtein.ComputeDistance(searchTerm, results[0].Name)
|
return levenshtein.ComputeDistance(searchTerm, results[i].Name) < levenshtein.ComputeDistance(searchTerm, results[j].Name)
|
||||||
for i := 1; i < len(results)-1; i++ {
|
})
|
||||||
newDist := levenshtein.ComputeDistance(searchTerm, results[i].Name)
|
|
||||||
if newDist < currDistance {
|
|
||||||
searchIndex = i
|
|
||||||
currDistance = newDist
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
menu := wmenu.NewMenu("Choose a number:")
|
menu := wmenu.NewMenu("Choose a number:")
|
||||||
|
|
||||||
for i, v := range results {
|
for i, v := range results {
|
||||||
menu.Option(v.Name, v, i == searchIndex, nil)
|
menu.Option(v.Name, v, i == 0, nil)
|
||||||
}
|
}
|
||||||
menu.Option("Cancel", nil, false, nil)
|
menu.Option("Cancel", nil, false, nil)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user