Skip to content

Commit

Permalink
improved searching sort
Browse files Browse the repository at this point in the history
Signed-off-by: Horiodino <[email protected]>
  • Loading branch information
Horiodino authored and dr-housemd committed Nov 18, 2024
1 parent 9d532a9 commit 0d7040c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
28 changes: 25 additions & 3 deletions cmd/search/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package search

import (
"context"
"sort"
"strings"
"time"

"github.com/buildsafedev/bsf/cmd/styles"
Expand All @@ -26,11 +28,31 @@ func (i pkgitem) FilterValue() string {
return i.name
}

func convLPR2Items(packages *buildsafev1.ListPackagesResponse) []list.Item {
func convLPR2Items(packages *buildsafev1.ListPackagesResponse, args ...string) []list.Item {
items := make([]list.Item, 0, len(packages.Packages))
for _, name := range packages.Packages {
items = append(items, pkgitem{name: name})

if len(args) == 0 || args[0] == "" {
return nil
}

for _, pkg := range packages.Packages {
if strings.Contains(pkg, args[0]) {
items = append(items, pkgitem{name: pkg})
}
}
sort.Slice(items, func(i, j int) bool {
nameI := items[i].(pkgitem).name
nameJ := items[j].(pkgitem).name

if strings.HasPrefix(nameI, args[0]) && !strings.HasPrefix(nameJ, args[0]) {
return true
}
if !strings.HasPrefix(nameI, args[0]) && strings.HasPrefix(nameJ, args[0]) {
return false
}

return nameI < nameJ
})

return items
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var SearchCmd = &cobra.Command{
fmt.Println(errorStyle.Render(fmt.Errorf("error: %v", err).Error()))
os.Exit(1)
}
items := convLPR2Items(packages)
items := convLPR2Items(packages, args...)
m := InitSearch(items)
if _, err := tea.NewProgram(m, tea.WithAltScreen()).Run(); err != nil {
fmt.Println(errorStyle.Render(fmt.Errorf("error: %v", err).Error()))
Expand Down

0 comments on commit 0d7040c

Please sign in to comment.