Skip to content

Commit 9d0c3bb

Browse files
committed
Show similar library names in lib search
1 parent 85effc8 commit 9d0c3bb

File tree

6 files changed

+155
-87
lines changed

6 files changed

+155
-87
lines changed

cli/lib/search.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -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 _, lsr := range results {
117-
out.WriteString(fmt.Sprintf("Name: \"%s\"\n", lsr.Name))
118-
if res.namesOnly {
121+
if res.results.GetStatus() == rpc.LibrarySearchStatus_success {
122+
out.WriteString(fmt.Sprintf("Name: \"%s\"\n", lsr.Name))
123+
if res.namesOnly {
124+
continue
125+
}
126+
} else {
127+
out.WriteString(fmt.Sprintf("%s\n", lsr.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
)
2728

2829
// LibrarySearch FIXMEDOC
@@ -33,6 +34,7 @@ func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchReq) (*rpc.Library
3334
}
3435

3536
res := []*rpc.SearchedLibrary{}
37+
status := rpc.LibrarySearchStatus_success
3638

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

48-
searchedlib := &rpc.SearchedLibrary{
50+
searchedLib := &rpc.SearchedLibrary{
4951
Name: lib.Name,
5052
Releases: releases,
5153
Latest: latest,
5254
}
53-
res = append(res, searchedlib)
55+
res = append(res, searchedLib)
5456
}
5557
}
5658

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

6073
// GetLibraryParameters FIXMEDOC

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
github.com/go-errors/errors v1.0.1
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
@@ -60,6 +60,8 @@ github.com/h2non/filetype v1.0.8 h1:le8gpf+FQA0/DlDABbtisA1KiTS0Xi+YSC/E8yY3Y14=
6060
github.com/h2non/filetype v1.0.8/go.mod h1:isekKqOuhMj+s/7r3rIeTErIRy4Rub5uBWHfvMusLMU=
6161
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
6262
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
63+
github.com/imjasonmiller/godice v0.1.2 h1:T1/sW/HoDzFeuwzOOuQjmeMELz9CzZ53I2CnD+08zD4=
64+
github.com/imjasonmiller/godice v0.1.2/go.mod h1:8cTkdnVI+NglU2d6sv+ilYcNaJ5VSTBwvMbFULJd/QQ=
6365
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
6466
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
6567
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=

0 commit comments

Comments
 (0)