Improve autofill sorting

This commit is contained in:
Bernd Schoolmann 2023-07-17 20:58:01 +02:00
parent 8ff9ea3ac9
commit 46c66ebd5c
No known key found for this signature in database
1 changed files with 59 additions and 4 deletions

View File

@ -6,6 +6,7 @@ import (
"image/color" "image/color"
"log" "log"
"os" "os"
"sort"
"strings" "strings"
"gioui.org/app" "gioui.org/app"
@ -19,6 +20,7 @@ import (
"gioui.org/unit" "gioui.org/unit"
"gioui.org/widget" "gioui.org/widget"
"gioui.org/widget/material" "gioui.org/widget/material"
gops "github.com/mitchellh/go-ps"
) )
type AutofillEntry struct { type AutofillEntry struct {
@ -31,12 +33,65 @@ var autofillEntries = []AutofillEntry{}
var onAutofill func(string, chan bool) var onAutofill func(string, chan bool)
var selectedEntry = 0 var selectedEntry = 0
type scoredAutofillEntry struct {
autofillEntry AutofillEntry
score int
}
func getProcesses() []string {
var processes []string
procs, err := gops.Processes()
if err != nil {
return []string{}
}
for _, proc := range procs {
processes = append(processes, proc.Executable())
}
return processes
}
func GetFilteredAutofillEntries(entries []AutofillEntry, filter string) []AutofillEntry { func GetFilteredAutofillEntries(entries []AutofillEntry, filter string) []AutofillEntry {
var filteredEntries []AutofillEntry if len(filter) < 2 {
for _, entry := range autofillEntries { return []AutofillEntry{}
if strings.Contains(strings.ToLower(entry.Username), strings.ToLower(filter)) || strings.Contains(strings.ToLower(entry.Name), strings.ToLower(filter)) { }
filteredEntries = append(filteredEntries, entry)
processes := getProcesses()
scoredEntries := []scoredAutofillEntry{}
for _, entry := range entries {
score := 0
if strings.Contains(strings.ToLower(entry.Name), strings.ToLower(filter)) {
score += 10
} }
if strings.HasPrefix(strings.ToLower(entry.Name), strings.ToLower(filter)) {
score += 5
}
if strings.Contains(strings.ToLower(entry.Username), strings.ToLower(filter)) {
score += 3
}
if strings.HasPrefix(strings.ToLower(entry.Username), strings.ToLower(filter)) {
score += 2
}
for _, process := range processes {
if strings.Contains(strings.ToLower(entry.Name), strings.ToLower(process)) {
score += 5
break
}
}
scoredEntries = append(scoredEntries, scoredAutofillEntry{entry, score})
}
sort.Slice(scoredEntries, func(i, j int) bool {
return scoredEntries[i].score > scoredEntries[j].score
})
var filteredEntries []AutofillEntry
for _, scoredEntry := range scoredEntries {
filteredEntries = append(filteredEntries, scoredEntry.autofillEntry)
} }
return filteredEntries return filteredEntries