Skip to content

Commit

Permalink
pacman: sync - fix package search
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Nov 29, 2022
1 parent ce2deb3 commit 544f0fd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
5 changes: 2 additions & 3 deletions completers/pacman_completer/cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func initSyncCmd(cmd *cobra.Command) {
cmd.Flags().BoolP("quiet", "q", false, "show less information for query and search")
cmd.Flags().BoolP("refresh", "y", false, "download fresh package databases from the server")
cmd.Flags().StringP("root", "r", "", "set an alternate installation root")
cmd.Flags().StringP("search", "s", "", "search remote repositories for matching strings")
cmd.Flags().Bool("sysroot", false, "operate on a mounted guest system (root-only)")
cmd.Flags().BoolP("sysupgrade", "u", false, "upgrade installed packages (-uu enables downgrades)")
cmd.Flags().BoolP("verbose", "v", false, "be verbose")
Expand All @@ -58,8 +59,6 @@ func initSyncCmd(cmd *cobra.Command) {
})

carapace.Gen(cmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return pacman.ActionPackages(pacman.PackageOption{}).Invoke(c).Filter(c.Args).ToA()
}),
pacman.ActionPackageSearch().UniqueList(","),
)
}
34 changes: 34 additions & 0 deletions pkg/actions/tools/pacman/package.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package pacman

import (
"regexp"
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/style"
)

// ActionPackageSearch completes installable packages
//
// a2ps (An Any to PostScript filter)
// a52dec (A free library for decoding ATSC A/52 streams)
func ActionPackageSearch() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return carapace.ActionExecCommand("pacman", "-Ss", "^"+c.CallbackValue)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
r := regexp.MustCompile(`^(?P<group>[^/]+)/(?P<name>[^ ]+) (?P<version>[^ ]+)(?P<context>.*)$`)

vals := make([]string, 0)
for i := 0; i < len(lines)-1; i += 2 {
if matches := r.FindStringSubmatch(lines[i]); matches != nil {
s := style.Default
if strings.Contains(matches[4], "[installed]") {
s = style.Blue
}
vals = append(vals, matches[2], strings.TrimSpace(lines[i+1]), s)
}
}
return carapace.ActionStyledValuesDescribed(vals...)
})
})
}

0 comments on commit 544f0fd

Please sign in to comment.