Switch to sahilm/fuzzy for fuzzy-filtering and text highlighting

This commit is contained in:
Nicolas Martin 2020-11-16 21:25:23 +01:00 committed by Christian Rocha
parent 43a5ac1b64
commit d98e3c11ba
4 changed files with 26 additions and 15 deletions

3
go.mod
View file

@ -10,7 +10,7 @@ require (
github.com/charmbracelet/glamour v0.2.1-0.20200829234023-6c0e29c4dae5
github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac
github.com/google/uuid v1.1.2 // indirect
github.com/lithammer/fuzzysearch v1.1.1
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mattn/go-runewidth v0.0.9
github.com/meowgorithm/babyenv v1.3.0
github.com/microcosm-cc/bluemonday v1.0.4 // indirect
@ -18,6 +18,7 @@ require (
github.com/muesli/go-app-paths v0.2.1
github.com/muesli/reflow v0.2.0
github.com/muesli/termenv v0.7.4
github.com/sahilm/fuzzy v0.1.0
github.com/spf13/cobra v1.1.1
github.com/spf13/viper v1.7.0
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897

6
go.sum
View file

@ -158,8 +158,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lithammer/fuzzysearch v1.1.1 h1:8F9OAV2xPuYblToVohjanztdnPjbtA0MLgMvDKQ0Z08=
github.com/lithammer/fuzzysearch v1.1.1/go.mod h1:H2bng+w5gsR7NlfIJM8ElGZI0sX6C/9uzGqicVXGU6c=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
@ -239,6 +239,8 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sabhiram/go-gitignore v0.0.0-20180611051255-d3107576ba94 h1:G04eS0JkAIVZfaJLjla9dNxkJCPiKIGZlw9AfOhzOD0=
github.com/sabhiram/go-gitignore v0.0.0-20180611051255-d3107576ba94/go.mod h1:b18R55ulyQ/h3RaWyloPyER7fWQVZvimKKhnI5OfrJQ=
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=

View file

@ -17,10 +17,10 @@ import (
"github.com/charmbracelet/charm"
"github.com/charmbracelet/charm/ui/common"
"github.com/dustin/go-humanize"
"github.com/lithammer/fuzzysearch/fuzzy"
runewidth "github.com/mattn/go-runewidth"
"github.com/muesli/reflow/ansi"
te "github.com/muesli/termenv"
"github.com/sahilm/fuzzy"
)
const (
@ -243,12 +243,12 @@ func (m *stashModel) getNotes() []*markdown {
targets = append(targets, note)
}
ranks := fuzzy.RankFindFold(m.filterInput.Value(), targets)
ranks := fuzzy.Find(m.filterInput.Value(), targets)
sort.Sort(ranks)
filtered := []*markdown{}
for _, r := range ranks {
filtered = append(filtered, m.markdowns[r.OriginalIndex])
filtered = append(filtered, m.markdowns[r.Index])
}
return filtered

View file

@ -3,11 +3,11 @@ package ui
import (
"fmt"
"strings"
"unicode"
"github.com/charmbracelet/charm/ui/common"
rw "github.com/mattn/go-runewidth"
"github.com/muesli/termenv"
"github.com/sahilm/fuzzy"
)
const (
@ -121,16 +121,24 @@ func stashItemView(b *strings.Builder, m stashModel, index int, md *markdown) {
func styleFilteredText(haystack, needles string, defaultStyle, matchedStyle termenv.Style) string {
b := strings.Builder{}
n := []rune(needles)
j := 0
for _, h := range []rune(haystack) {
if j < len(n) && unicode.ToLower(h) == unicode.ToLower(n[j]) {
b.WriteString(matchedStyle.Styled(string(h)))
j++
continue
matches := fuzzy.Find(needles, []string{haystack})
if len(matches) == 0 {
return defaultStyle.Styled(haystack)
}
m := matches[0] // only one match exists
for i, rune := range haystack {
styled := false
for _, mi := range m.MatchedIndexes {
if i == mi {
b.WriteString(matchedStyle.Styled(string(rune)))
styled = true
}
}
if !styled {
b.WriteString(defaultStyle.Styled(string(rune)))
}
b.WriteString(defaultStyle.Styled(string(h)))
}
return b.String()