Skip to content

Commit 8ff1f28

Browse files
committed
Show similar library names in lib search
1 parent e8bb327 commit 8ff1f28

File tree

6 files changed

+78
-13
lines changed

6 files changed

+78
-13
lines changed

cli/lib/search.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ func (res result) Data() interface{} {
8888

8989
names := []LibName{}
9090
results := res.results.GetLibraries()
91-
for _, lsr := range results {
92-
names = append(names, LibName{lsr.Name})
91+
for _, lib := range results {
92+
names = append(names, LibName{lib.Name})
9393
}
9494

9595
return NamesOnly{
@@ -113,9 +113,18 @@ func (res result) String() string {
113113

114114
var out strings.Builder
115115

116+
if res.results.GetStatus() == rpc.LibrarySearchStatus_failed {
117+
out.WriteString("No libraries matching your search.\nDid you mean...\n")
118+
}
119+
116120
for _, lib := range results {
117-
out.WriteString(fmt.Sprintf("Name: \"%s\"\n", lib.Name))
118-
if res.namesOnly {
121+
if res.results.GetStatus() == rpc.LibrarySearchStatus_success {
122+
out.WriteString(fmt.Sprintf("Name: \"%s\"\n", lib.Name))
123+
if res.namesOnly {
124+
continue
125+
}
126+
} else {
127+
out.WriteString(fmt.Sprintf("%s\n", lib.Name))
119128
continue
120129
}
121130

commands/lib/search.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
2424
"github.com/arduino/arduino-cli/commands"
2525
rpc "github.com/arduino/arduino-cli/rpc/commands"
26+
"github.com/imjasonmiller/godice"
2627
semver "go.bug.st/relaxed-semver"
2728
)
2829

@@ -34,6 +35,7 @@ func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchReq) (*rpc.Library
3435
}
3536

3637
res := []*rpc.SearchedLibrary{}
38+
status := rpc.LibrarySearchStatus_success
3739

3840
for _, lib := range lm.Index.Libraries {
3941
qry := strings.ToLower(req.GetQuery())
@@ -46,16 +48,27 @@ func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchReq) (*rpc.Library
4648
}
4749
latest := GetLibraryParameters(lib.Latest)
4850

49-
searchedlib := &rpc.SearchedLibrary{
51+
searchedLib := &rpc.SearchedLibrary{
5052
Name: lib.Name,
5153
Releases: releases,
5254
Latest: latest,
5355
}
54-
res = append(res, searchedlib)
56+
res = append(res, searchedLib)
5557
}
5658
}
5759

58-
return &rpc.LibrarySearchResp{Libraries: res}, nil
60+
if len(res) == 0 {
61+
status = rpc.LibrarySearchStatus_failed
62+
for _, lib := range lm.Index.Libraries {
63+
if godice.CompareString(req.GetQuery(), lib.Name) > 0.7 {
64+
res = append(res, &rpc.SearchedLibrary{
65+
Name: lib.Name,
66+
})
67+
}
68+
}
69+
}
70+
71+
return &rpc.LibrarySearchResp{Libraries: res, Status: status}, nil
5972
}
6073

6174
// GetLibraryParameters FIXMEDOC

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
github.com/gofrs/uuid v3.2.0+incompatible
2020
github.com/golang/protobuf v1.3.3
2121
github.com/h2non/filetype v1.0.8 // indirect
22+
github.com/imjasonmiller/godice v0.1.2
2223
github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5 // indirect
2324
github.com/juju/loggo v0.0.0-20190526231331-6e530bcce5d8 // indirect
2425
github.com/juju/testing v0.0.0-20190429233213-dfc56b8c09fc // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ github.com/h2non/filetype v1.0.8 h1:le8gpf+FQA0/DlDABbtisA1KiTS0Xi+YSC/E8yY3Y14=
8585
github.com/h2non/filetype v1.0.8/go.mod h1:isekKqOuhMj+s/7r3rIeTErIRy4Rub5uBWHfvMusLMU=
8686
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
8787
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
88+
github.com/imjasonmiller/godice v0.1.2 h1:T1/sW/HoDzFeuwzOOuQjmeMELz9CzZ53I2CnD+08zD4=
89+
github.com/imjasonmiller/godice v0.1.2/go.mod h1:8cTkdnVI+NglU2d6sv+ilYcNaJ5VSTBwvMbFULJd/QQ=
8890
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
8991
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
9092
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=

rpc/commands/lib.pb.go

+40-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/commands/lib.proto

+6
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,14 @@ message LibrarySearchReq {
8282
string query = 2;
8383
}
8484

85+
enum LibrarySearchStatus {
86+
failed = 0;
87+
success = 1;
88+
}
89+
8590
message LibrarySearchResp {
8691
repeated SearchedLibrary libraries = 1;
92+
LibrarySearchStatus status = 2;
8793
}
8894

8995
message SearchedLibrary {

0 commit comments

Comments
 (0)