Skip to content

Commit 0b2c38d

Browse files
authored
lib search returns exact match as the first result. (#1975)
* Removed global variable * Small cosmetic fix * lib: Sort search results and put exact match at the top
1 parent ca39297 commit 0b2c38d

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

Diff for: cli/lib/search.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,23 @@ import (
3434
semver "go.bug.st/relaxed-semver"
3535
)
3636

37-
var (
38-
namesOnly bool // if true outputs lib names only.
39-
)
40-
4137
func initSearchCommand() *cobra.Command {
38+
var namesOnly bool // if true outputs lib names only.
4239
searchCommand := &cobra.Command{
4340
Use: fmt.Sprintf("search [%s]", tr("LIBRARY_NAME")),
4441
Short: tr("Searches for one or more libraries data."),
4542
Long: tr("Search for one or more libraries data (case insensitive search)."),
4643
Example: " " + os.Args[0] + " lib search audio",
4744
Args: cobra.ArbitraryArgs,
48-
Run: runSearchCommand,
45+
Run: func(cmd *cobra.Command, args []string) {
46+
runSearchCommand(args, namesOnly)
47+
},
4948
}
5049
searchCommand.Flags().BoolVar(&namesOnly, "names", false, tr("Show library names only."))
5150
return searchCommand
5251
}
5352

54-
func runSearchCommand(cmd *cobra.Command, args []string) {
53+
func runSearchCommand(args []string, namesOnly bool) {
5554
inst, status := instance.Create()
5655
logrus.Info("Executing `arduino-cli lib search`")
5756

@@ -73,7 +72,7 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
7372

7473
searchResp, err := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchRequest{
7574
Instance: inst,
76-
Query: (strings.Join(args, " ")),
75+
Query: strings.Join(args, " "),
7776
})
7877
if err != nil {
7978
feedback.Errorf(tr("Error searching for Libraries: %v"), err)
@@ -125,11 +124,6 @@ func (res result) String() string {
125124
return tr("No libraries matching your search.")
126125
}
127126

128-
// get a sorted slice of results
129-
sort.Slice(results, func(i, j int) bool {
130-
return results[i].Name < results[j].Name
131-
})
132-
133127
var out strings.Builder
134128

135129
if res.results.GetStatus() == rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_FAILED {

Diff for: commands/lib/search.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package lib
1717

1818
import (
1919
"context"
20+
"sort"
21+
"strings"
2022

2123
"github.com/arduino/arduino-cli/arduino"
2224
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
@@ -38,7 +40,8 @@ func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchRequest) (*rpc.Lib
3840

3941
func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.LibrariesManager) *rpc.LibrarySearchResponse {
4042
res := []*rpc.SearchedLibrary{}
41-
queryTerms := utils.SearchTermsFromQueryString(req.GetQuery())
43+
query := req.GetQuery()
44+
queryTerms := utils.SearchTermsFromQueryString(query)
4245

4346
for _, lib := range lm.Index.Libraries {
4447
toTest := lib.Name + " " +
@@ -54,6 +57,19 @@ func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.Libraries
5457
}
5558
}
5659

60+
// get a sorted slice of results
61+
sort.Slice(res, func(i, j int) bool {
62+
// Sort by name, but bubble up exact matches
63+
equalsI := strings.EqualFold(res[i].Name, query)
64+
equalsJ := strings.EqualFold(res[j].Name, query)
65+
if equalsI && !equalsJ {
66+
return true
67+
} else if !equalsI && equalsJ {
68+
return false
69+
}
70+
return res[i].Name < res[j].Name
71+
})
72+
5773
return &rpc.LibrarySearchResponse{Libraries: res, Status: rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS}
5874
}
5975

Diff for: commands/lib/search_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package lib
22

33
import (
4-
"sort"
54
"strings"
65
"testing"
76

@@ -52,7 +51,6 @@ func TestSearchLibraryFields(t *testing.T) {
5251
for _, lib := range searchLibrary(&rpc.LibrarySearchRequest{Query: q}, lm).Libraries {
5352
libs = append(libs, lib.Name)
5453
}
55-
sort.Strings(libs)
5654
return libs
5755
}
5856

@@ -76,4 +74,8 @@ func TestSearchLibraryFields(t *testing.T) {
7674
require.Len(t, res, 2)
7775
require.Equal(t, "Arduino_ConnectionHandler", res[0])
7876
require.Equal(t, "FlashStorage_SAMD", res[1])
77+
78+
res = query("flashstorage")
79+
require.Len(t, res, 19)
80+
require.Equal(t, "FlashStorage", res[0])
7981
}

0 commit comments

Comments
 (0)