Sort by levenshtein distance

This commit is contained in:
comp500 2019-06-13 22:58:46 +01:00
parent 10beffb30c
commit 310078b1fb
No known key found for this signature in database
GPG Key ID: 214C822FFEC586B5

View File

@ -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)