Use external library for fuzzy string search

pull/9/head v0.1.1
Trevor Slocum 2019-07-16 07:19:40 -07:00
parent 1624767cde
commit ceb8c40dbc
5 changed files with 86 additions and 24 deletions

32
LICENSE
View File

@ -1,4 +1,7 @@
MIT License
gmenu - Desktop application launcher
https://git.sr.ht/~tslocum/gmenu
The MIT License (MIT)
Copyright (c) 2019 Trevor Slocum <trevor@rocketnine.space>
@ -19,3 +22,30 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
----------
fuzzysearch - Fuzzy string matching library
https://github.com/lithammer/fuzzysearch
The MIT License (MIT)
Copyright (c) 2018 Peter Lithammer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -10,6 +10,7 @@ import (
"git.sr.ht/~tslocum/desktop"
"github.com/kballard/go-shellquote"
"github.com/lithammer/fuzzysearch/fuzzy"
"github.com/pkg/errors"
)
@ -18,32 +19,53 @@ func updateEntries(buf string) {
buf = strings.ToLower(strings.TrimSpace(buf))
if buf == "" {
for _, entries := range desktopEntries {
filteredEntries = append(filteredEntries, entries...)
if cap(filteredEntries) != len(desktopEntries) {
filteredEntries = make([]*desktop.Entry, len(desktopEntries))
}
} else {
var ranks []int
var rank int
var i int
for _, entries := range desktopEntries {
for _, entry := range entries {
rank = -1
if strings.Contains(strings.ToLower(entry.Name), buf) {
rank = 1
}
if rank == -1 {
continue
}
copy(filteredEntries, desktopEntries)
filteredEntries = append(filteredEntries, entry)
ranks = append(ranks, rank)
i++
sort.Slice(filteredEntries, func(i, j int) bool {
ilower := strings.ToLower(filteredEntries[i].Name)
jlower := strings.ToLower(filteredEntries[j].Name)
if ilower != jlower {
return ilower < jlower
} else {
return i < j
}
})
} else {
matches := fuzzy.RankFindFold(buf, desktopNames)
sort.Sort(matches)
if cap(filteredEntries) != len(matches) {
filteredEntries = make([]*desktop.Entry, len(matches))
}
for i, match := range matches {
filteredEntries[i] = desktopEntries[match.OriginalIndex]
}
sort.Slice(filteredEntries, func(i, j int) bool {
return ranks[i] < ranks[j]
ilower := strings.ToLower(filteredEntries[i].Name)
jlower := strings.ToLower(filteredEntries[j].Name)
ipre := strings.HasPrefix(ilower, buf)
jpre := strings.HasPrefix(jlower, buf)
icon := strings.Contains(ilower, buf)
jcon := strings.Contains(jlower, buf)
if ipre != jpre {
return ipre && !jpre
} else if icon != jcon {
return icon && !jcon
} else if ilower != jlower {
return ilower < jlower
} else {
return i < j
}
})
}

View File

@ -19,7 +19,8 @@ var (
disableMouseSupport bool
hideAppDetails bool
desktopEntries map[int][]*desktop.Entry
desktopEntries []*desktop.Entry
desktopNames []string
filteredEntries []*desktop.Entry
gui *gocui.Gui
@ -52,12 +53,18 @@ func main() {
dirs = desktop.DataDirs()
}
var err error
desktopEntries, err = desktop.Scan(dirs)
allEntries, err := desktop.Scan(dirs)
if err != nil {
log.Fatal(err)
}
for _, entries := range allEntries {
for _, entry := range entries {
desktopEntries = append(desktopEntries, entry)
desktopNames = append(desktopNames, strings.ToLower(entry.Name))
}
}
gui, err = initGUI()
if err != nil {
log.Fatal(err)

1
go.mod
View File

@ -6,6 +6,7 @@ require (
git.sr.ht/~tslocum/desktop v0.1.0
github.com/jroimartin/gocui v0.4.0
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/lithammer/fuzzysearch v1.0.2
github.com/mattn/go-isatty v0.0.8
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/nsf/termbox-go v0.0.0-20190624072549-eeb6cd0a1762 // indirect

4
go.sum
View File

@ -1,9 +1,11 @@
git.sr.ht/~tslocum/desktop v0.1.0 h1:+zF9xEJMkmgRnKWxqDab+QkebPG7Hi83PhGXMQQUvKE=
git.sr.ht/~tslocum/desktop v0.1.0 h1:mLmT1m+IhhpTbyfbXejtrJyAyV0S9tJzd8ZyWTkN5Es=
git.sr.ht/~tslocum/desktop v0.1.0/go.mod h1:cUn0Q8ALjkAq40qSei795yN3CfO5pkeYKo2gmzaZ2SI=
github.com/jroimartin/gocui v0.4.0 h1:52jnalstgmc25FmtGcWqa0tcbMEWS6RpFLsOIO+I+E8=
github.com/jroimartin/gocui v0.4.0/go.mod h1:7i7bbj99OgFHzo7kB2zPb8pXLqMBSQegY7azfqXMkyY=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/lithammer/fuzzysearch v1.0.2 h1:AjCE2iwc5y+8K+h2nXVc0Pmrpjvu+JVqMgiZ0oakXDM=
github.com/lithammer/fuzzysearch v1.0.2/go.mod h1:bvAJyokfCQ7Vknrd4Kgc+izmMrPj5CiBAu2t6rK1Kak=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=