Skip to content

Commit 1f5706f

Browse files
authored
Add fuzzy search to board listall and more information in its json output (#1205)
* Add board's platform to board listall json output * Add fuzzy search to board listall command
1 parent b2b9fba commit 1f5706f

File tree

8 files changed

+469
-363
lines changed

8 files changed

+469
-363
lines changed

commands/board/listall.go

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,86 @@ import (
2222

2323
"github.com/arduino/arduino-cli/commands"
2424
rpc "github.com/arduino/arduino-cli/rpc/commands"
25+
"github.com/lithammer/fuzzysearch/fuzzy"
2526
)
2627

28+
// maximumSearchDistance is the maximum Levenshtein distance accepted when using fuzzy search.
29+
// This value is completely arbitrary and picked randomly.
30+
const maximumSearchDistance = 20
31+
2732
// ListAll FIXMEDOC
2833
func ListAll(ctx context.Context, req *rpc.BoardListAllReq) (*rpc.BoardListAllResp, error) {
2934
pm := commands.GetPackageManager(req.GetInstance().GetId())
3035
if pm == nil {
3136
return nil, errors.New("invalid instance")
3237
}
3338

34-
args := req.GetSearchArgs()
35-
match := func(name string) bool {
36-
if len(args) == 0 {
39+
searchArgs := strings.Join(req.SearchArgs, " ")
40+
41+
match := func(toTest []string) bool {
42+
if len(searchArgs) == 0 {
3743
return true
3844
}
39-
name = strings.ToLower(name)
40-
for _, term := range args {
41-
if !strings.Contains(name, strings.ToLower(term)) {
42-
return false
45+
for _, rank := range fuzzy.RankFindNormalizedFold(searchArgs, toTest) {
46+
if rank.Distance < maximumSearchDistance {
47+
return true
4348
}
4449
}
45-
return true
50+
return false
4651
}
4752

4853
list := &rpc.BoardListAllResp{Boards: []*rpc.BoardListItem{}}
4954
for _, targetPackage := range pm.Packages {
5055
for _, platform := range targetPackage.Platforms {
51-
platformRelease := pm.GetInstalledPlatformRelease(platform)
52-
if platformRelease == nil {
56+
installedPlatformRelease := pm.GetInstalledPlatformRelease(platform)
57+
// We only want to list boards for installed platforms
58+
if installedPlatformRelease == nil {
5359
continue
5460
}
55-
for _, board := range platformRelease.Boards {
56-
if !match(board.Name()) {
61+
62+
installedVersion := installedPlatformRelease.Version.String()
63+
64+
latestVersion := ""
65+
if latestPlatformRelease := platform.GetLatestRelease(); latestPlatformRelease != nil {
66+
latestVersion = latestPlatformRelease.Version.String()
67+
}
68+
69+
rpcPlatform := &rpc.Platform{
70+
ID: platform.String(),
71+
Installed: installedVersion,
72+
Latest: latestVersion,
73+
Name: platform.Name,
74+
Maintainer: platform.Package.Maintainer,
75+
Website: platform.Package.WebsiteURL,
76+
Email: platform.Package.Email,
77+
ManuallyInstalled: platform.ManuallyInstalled,
78+
}
79+
80+
toTest := []string{
81+
platform.String(),
82+
platform.Name,
83+
platform.Architecture,
84+
targetPackage.Name,
85+
targetPackage.Maintainer,
86+
}
87+
88+
for _, board := range installedPlatformRelease.Boards {
89+
if !req.GetIncludeHiddenBoards() && board.IsHidden() {
5790
continue
5891
}
59-
if !req.GetIncludeHiddenBoards() && board.IsHidden() {
92+
93+
toTest := toTest
94+
toTest = append(toTest, strings.Split(board.Name(), " ")...)
95+
toTest = append(toTest, board.FQBN())
96+
if !match(toTest) {
6097
continue
6198
}
99+
62100
list.Boards = append(list.Boards, &rpc.BoardListItem{
63101
Name: board.Name(),
64102
FQBN: board.FQBN(),
65103
IsHidden: board.IsHidden(),
104+
Platform: rpcPlatform,
66105
})
67106
}
68107
}

rpc/commands/board.pb.go

Lines changed: 28 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/commands/board.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,6 @@ message BoardListItem {
235235
string VID = 4;
236236
// Product ID
237237
string PID = 5;
238+
// Platform this board belongs to
239+
Platform platform = 6;
238240
}

0 commit comments

Comments
 (0)