Skip to content

Commit 9e0b642

Browse files
committed
feature: add fuzzy search/querying for branch suggestions. closes #103
1 parent 0d79187 commit 9e0b642

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ Thanks to [Gitless](https://gitless.com/), [git-extras](https://github.com/tj/gi
165165
## Changelog
166166
v1.0
167167
- [X] enhancement: significantly more autocompletions
168+
- [X] enhancement: use fuzzy search for branch suggestions
168169
- [X] refactor: completions now represented in tree data structure
169170
- [X] fix: bit save no longer shows debug error outside debug mode
170171
- [X] fix: use --is-inside-work-tree to determine whether inside a git repo

cmd/bit_cmd.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package cmd
33
import (
44
"fmt"
55
"github.com/chriswalz/complete/v3"
6+
"github.com/google/shlex"
7+
"github.com/lithammer/fuzzysearch/fuzzy"
68
"os"
79
"sort"
810
"strings"
@@ -79,20 +81,41 @@ func specificCommandCompleter(subCmd string, suggestionMap *complete.CompTree) f
7981
}
8082
}
8183

84+
var fuzzyQuery = func(s, prefix string) bool {
85+
return fuzzy.Match(prefix, s)
86+
}
87+
8288
func promptCompleter(suggestionTree *complete.CompTree, text string) []prompt.Suggest {
8389
text = "bit " + text
8490

8591
var sugg []prompt.Suggest
8692

87-
suggestions, err := complete.CompleteLine(text, suggestionTree)
93+
queryFunc := strings.HasPrefix
94+
95+
split, err := shlex.Split(strings.TrimSpace(text))
8896
if err != nil {
8997
log.Debug().Err(err).Send()
9098
return sugg
9199
}
92-
split := strings.Split(strings.TrimSpace(text), " ")
93100
lastToken := split[len(split)-1]
101+
lastCommand := lastToken
102+
if !strings.HasSuffix(text, " ") && len(split) >= 2 {
103+
lastCommand = split[len(split)-2]
104+
}
105+
106+
// use fuzzy search completion when querying branch names
107+
if isBranchCompletionCommand(lastCommand) {
108+
queryFunc = fuzzyQuery
109+
}
110+
111+
suggestions, err := complete.CompleteLine(text, suggestionTree, queryFunc)
112+
if err != nil {
113+
log.Debug().Err(err).Send()
114+
return sugg
115+
}
116+
94117
// for branches dont undo most recent sorts with alphabetical sort
95-
if !isBranchCompletionCommand(lastToken) {
118+
if !isBranchCompletionCommand(lastCommand) {
96119
sort.Slice(suggestions, func(i, j int) bool {
97120
return suggestions[i].Name < suggestions[j].Name
98121
})

cmd/cmd_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"github.com/chriswalz/complete/v3"
6+
"strings"
67
"testing"
78

89
"github.com/c-bata/go-prompt"
@@ -204,7 +205,7 @@ func TestCompletion(t *testing.T) {
204205
},
205206
}
206207
for _, e := range expects {
207-
reality, err := complete.CompleteLine(e.line, suggestionsTree)
208+
reality, err := complete.CompleteLine(e.line, suggestionsTree, strings.HasPrefix)
208209
assert.Equal(t, err, nil)
209210

210211
for _, p := range e.predictions {

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@ require (
88
github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 // indirect
99
github.com/c-bata/go-prompt v0.2.5
1010
github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c // indirect
11-
github.com/chriswalz/complete/v3 v3.0.12
11+
github.com/chriswalz/complete/v3 v3.0.13
1212
github.com/dsnet/compress v0.0.1 // indirect
1313
github.com/google/go-github v17.0.0+incompatible // indirect
1414
github.com/google/go-github/v32 v32.1.0
15+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
1516
github.com/gosuri/uilive v0.0.4 // indirect
1617
github.com/gosuri/uiprogress v0.0.1 // indirect
1718
github.com/hooklift/assert v0.1.0 // indirect
1819
github.com/klauspost/pgzip v1.2.5 // indirect
20+
github.com/lithammer/fuzzysearch v1.1.1 // indirect
1921
github.com/pkg/errors v0.8.1
2022
github.com/rs/zerolog v1.20.0
2123
github.com/spf13/cobra v1.1.1
2224
github.com/stretchr/testify v1.7.0
2325
github.com/thoas/go-funk v0.7.0
2426
github.com/tj/go-update v2.2.4+incompatible
2527
github.com/ulikunitz/xz v0.5.8 // indirect
28+
2629
)

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ github.com/c-bata/go-prompt v0.2.5/go.mod h1:vFnjEGDIIA/Lib7giyE4E9c50Lvl8j0S+7F
4444
github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c h1:aprLqMn7gSPT+vdDSl+/E6NLEuArwD/J7IWd8bJt5lQ=
4545
github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c/go.mod h1:Ie6SubJv/NTO9Q0UBH0QCl3Ve50lu9hjbi5YJUw03TE=
4646
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
47-
github.com/chriswalz/complete/v3 v3.0.12 h1:HW54dvYu7u63NR/eMtCZ9GEq9xTG8gChhEIA/QmD+bU=
48-
github.com/chriswalz/complete/v3 v3.0.12/go.mod h1:U4RZv3y+X3pV/uhqcJhlqlnug7IfJNiVVmIZ7+bsf20=
47+
github.com/chriswalz/complete/v3 v3.0.13 h1:34B9sIn+gSnjb7Q0cCqoqa1WUYRdwOyAZUkcjlYLjEY=
48+
github.com/chriswalz/complete/v3 v3.0.13/go.mod h1:U4RZv3y+X3pV/uhqcJhlqlnug7IfJNiVVmIZ7+bsf20=
4949
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
5050
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
5151
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
@@ -166,6 +166,8 @@ github.com/kr/pty v1.1.4 h1:5Myjjh3JY/NaAi4IsUbHADytDyl1VE1Y9PXDlL+P/VQ=
166166
github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
167167
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
168168
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
169+
github.com/lithammer/fuzzysearch v1.1.1 h1:8F9OAV2xPuYblToVohjanztdnPjbtA0MLgMvDKQ0Z08=
170+
github.com/lithammer/fuzzysearch v1.1.1/go.mod h1:H2bng+w5gsR7NlfIJM8ElGZI0sX6C/9uzGqicVXGU6c=
169171
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
170172
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
171173
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=

0 commit comments

Comments
 (0)