Skip to content

lib search returns exact match as the first result. #1975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions cli/lib/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,23 @@ import (
semver "go.bug.st/relaxed-semver"
)

var (
namesOnly bool // if true outputs lib names only.
)

func initSearchCommand() *cobra.Command {
var namesOnly bool // if true outputs lib names only.
searchCommand := &cobra.Command{
Use: fmt.Sprintf("search [%s]", tr("LIBRARY_NAME")),
Short: tr("Searches for one or more libraries data."),
Long: tr("Search for one or more libraries data (case insensitive search)."),
Example: " " + os.Args[0] + " lib search audio",
Args: cobra.ArbitraryArgs,
Run: runSearchCommand,
Run: func(cmd *cobra.Command, args []string) {
runSearchCommand(args, namesOnly)
},
}
searchCommand.Flags().BoolVar(&namesOnly, "names", false, tr("Show library names only."))
return searchCommand
}

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

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

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

// get a sorted slice of results
sort.Slice(results, func(i, j int) bool {
return results[i].Name < results[j].Name
})

var out strings.Builder

if res.results.GetStatus() == rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_FAILED {
Expand Down
18 changes: 17 additions & 1 deletion commands/lib/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package lib

import (
"context"
"sort"
"strings"

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

func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.LibrariesManager) *rpc.LibrarySearchResponse {
res := []*rpc.SearchedLibrary{}
queryTerms := utils.SearchTermsFromQueryString(req.GetQuery())
query := req.GetQuery()
queryTerms := utils.SearchTermsFromQueryString(query)

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

// get a sorted slice of results
sort.Slice(res, func(i, j int) bool {
// Sort by name, but bubble up exact matches
equalsI := strings.EqualFold(res[i].Name, query)
equalsJ := strings.EqualFold(res[j].Name, query)
if equalsI && !equalsJ {
return true
} else if !equalsI && equalsJ {
return false
}
return res[i].Name < res[j].Name
})

return &rpc.LibrarySearchResponse{Libraries: res, Status: rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS}
}

Expand Down
6 changes: 4 additions & 2 deletions commands/lib/search_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lib

import (
"sort"
"strings"
"testing"

Expand Down Expand Up @@ -52,7 +51,6 @@ func TestSearchLibraryFields(t *testing.T) {
for _, lib := range searchLibrary(&rpc.LibrarySearchRequest{Query: q}, lm).Libraries {
libs = append(libs, lib.Name)
}
sort.Strings(libs)
return libs
}

Expand All @@ -76,4 +74,8 @@ func TestSearchLibraryFields(t *testing.T) {
require.Len(t, res, 2)
require.Equal(t, "Arduino_ConnectionHandler", res[0])
require.Equal(t, "FlashStorage_SAMD", res[1])

res = query("flashstorage")
require.Len(t, res, 19)
require.Equal(t, "FlashStorage", res[0])
}