Skip to content

Commit

Permalink
fuzzy search initial
Browse files Browse the repository at this point in the history
  • Loading branch information
herzrasen committed Dec 30, 2022
1 parent 55206e2 commit d3d786e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/fatih/color v1.13.0
github.com/gdamore/tcell/v2 v2.5.3
github.com/jmoiron/sqlx v1.3.5
github.com/lithammer/fuzzysearch v1.1.5
github.com/mattn/go-sqlite3 v1.14.16
github.com/rivo/tview v0.0.0-20221221172820-02e38ea9604c
github.com/sirupsen/logrus v1.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
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-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
Expand Down
20 changes: 18 additions & 2 deletions search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"github.com/gdamore/tcell/v2"
"github.com/herzrasen/hist/client"
"github.com/herzrasen/hist/record"
"github.com/lithammer/fuzzysearch/fuzzy"
"github.com/rivo/tview"
"sort"
"strings"
)

Expand All @@ -24,7 +26,8 @@ type Searcher struct {
func NewSearcher(listClient ListClient) *Searcher {
list := tview.NewList().
ShowSecondaryText(false).
SetShortcutStyle(tcell.Style{})
SetShortcutStyle(tcell.Style{}).
SetWrapAround(true)
input := tview.NewInputField()
flex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(list, 0, 20, false).
Expand Down Expand Up @@ -79,9 +82,22 @@ func (s *Searcher) Show() error {
if err != nil {
return fmt.Errorf("search:Searcher:Show: list: %w", err)
}
var commands []string
for _, rec := range records {
s.List.AddItem(rec.Command, "", 0, nil)
commands = append(commands, rec.Command)
}
for _, command := range commands {
s.List.AddItem(command, "", 0, nil)
}
s.Input.SetChangedFunc(func(text string) {
ranks := fuzzy.RankFind(text, commands)
sort.Reverse(ranks)
s.List.Clear()
for _, rankedCommand := range ranks {
s.List.AddItem(rankedCommand.Target, "", 0, nil)
}
s.List.SetCurrentItem(len(ranks) - 1)
})
numItems := s.List.GetItemCount() - 1
currentItem, _ := s.List.GetItemText(numItems)
s.List.SetCurrentItem(numItems)
Expand Down

0 comments on commit d3d786e

Please sign in to comment.