Skip to content

Commit

Permalink
feat: select best matching package after search
Browse files Browse the repository at this point in the history
when a search if performed,
the package that best fits the search term is selected

Signed-off-by: moson-mo <mo-son@mailbox.org>
  • Loading branch information
moson-mo committed Jul 26, 2022
1 parent cb097e8 commit 906eb32
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/Jguer/go-alpm/v2 v2.1.2
github.com/Morganamilo/go-pacmanconf v0.0.0-20210502114700-cff030e927a5
github.com/gdamore/tcell/v2 v2.5.1
github.com/lithammer/fuzzysearch v1.1.5
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/rivo/tview v0.0.0-20220709181631-73bf2902b59a
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1/go.mod h1:Az6Jt+M5idSED2YPGtwnfJV0kXohgdCBPmHGSYc1r04=
github.com/gdamore/tcell/v2 v2.5.1 h1:zc3LPdpK184lBW7syF2a5C6MV827KmErk9jGVnmsl/I=
github.com/gdamore/tcell/v2 v2.5.1/go.mod h1:wSkrPaXoiIWZqW/g7Px4xc79di6FTcpB8tvaKJ6uGBo=
github.com/lithammer/fuzzysearch v1.1.5 h1:Ag7aKU08wp0R9QCfF4GoGST9HbmAIeLP7xwMrOBEp1c=
github.com/lithammer/fuzzysearch v1.1.5/go.mod h1:1R1LRNk7yKid1BaQkmuLQaHruxcC4HmAH30Dh61Ih1Q=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
Expand Down
20 changes: 16 additions & 4 deletions internal/pacseek/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/gdamore/tcell/v2"
"github.com/lithammer/fuzzysearch/fuzzy"
"github.com/rivo/tview"
)

Expand Down Expand Up @@ -105,6 +106,20 @@ func (ps *UI) showPackages(text string) {
}
ps.shownPackages = packages

// rank packages and save index of closest one to search term
bestMatch := 0
prevRank := 9999
for i := 0; i < len(packages); i++ {
rank := fuzzy.RankMatch(text, packages[i].Name)
if rank < prevRank {
bestMatch = i
prevRank = rank
}
if rank == 0 {
break
}
}

// draw packages
ps.app.QueueUpdateDraw(func() {
if text != ps.search.GetText() {
Expand All @@ -115,10 +130,7 @@ func (ps *UI) showPackages(text string) {
ps.right.Clear()
ps.right.AddItem(ps.details, 0, 1, false)
}
r, _ := ps.packages.GetSelection()
if r > 1 {
ps.packages.Select(1, 0)
}
ps.packages.Select(bestMatch+1, 0) // select the best match
})
}()
}
Expand Down

0 comments on commit 906eb32

Please sign in to comment.