@@ -22,47 +22,86 @@ import (
22
22
23
23
"github.com/arduino/arduino-cli/commands"
24
24
rpc "github.com/arduino/arduino-cli/rpc/commands"
25
+ "github.com/lithammer/fuzzysearch/fuzzy"
25
26
)
26
27
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
+
27
32
// ListAll FIXMEDOC
28
33
func ListAll (ctx context.Context , req * rpc.BoardListAllReq ) (* rpc.BoardListAllResp , error ) {
29
34
pm := commands .GetPackageManager (req .GetInstance ().GetId ())
30
35
if pm == nil {
31
36
return nil , errors .New ("invalid instance" )
32
37
}
33
38
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 {
37
43
return true
38
44
}
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
43
48
}
44
49
}
45
- return true
50
+ return false
46
51
}
47
52
48
53
list := & rpc.BoardListAllResp {Boards : []* rpc.BoardListItem {}}
49
54
for _ , targetPackage := range pm .Packages {
50
55
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 {
53
59
continue
54
60
}
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 () {
57
90
continue
58
91
}
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 ) {
60
97
continue
61
98
}
99
+
62
100
list .Boards = append (list .Boards , & rpc.BoardListItem {
63
101
Name : board .Name (),
64
102
FQBN : board .FQBN (),
65
103
IsHidden : board .IsHidden (),
104
+ Platform : rpcPlatform ,
66
105
})
67
106
}
68
107
}
0 commit comments