Multiple search args now narrow the search. Resolves #3

This commit is contained in:
Jguer 2017-02-05 04:15:44 +00:00
parent 67c91180b9
commit 1f4bb8eb16
3 changed files with 78 additions and 31 deletions

View file

@ -9,35 +9,77 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/jguer/yay/aur" aur "github.com/jguer/yay/aur"
pac "github.com/jguer/yay/pacman" pac "github.com/jguer/yay/pacman"
"github.com/jguer/yay/util" "github.com/jguer/yay/util"
) )
// NarrowSearch removes terms that don't contain narrow terms in the description or name.
func narrowSearch(aq aur.Query, pq pac.Query, narrow []string) (raq aur.Query, rpq pac.Query) {
for _, pr := range pq {
match := false
for _, narrowS := range narrow {
if strings.Contains(strings.ToUpper(pr.Name), strings.ToUpper(narrowS)) || strings.Contains(strings.ToUpper(pr.Description), strings.ToUpper(narrowS)) {
match = true
} else {
match = false
}
}
if match {
rpq = append(rpq, pr)
}
}
for _, ar := range aq {
match := false
for _, narrowS := range narrow {
if strings.Contains(strings.ToUpper(ar.Name), strings.ToUpper(narrowS)) || strings.Contains(strings.ToUpper(ar.Description), strings.ToUpper(narrowS)) {
match = true
} else {
match = false
}
}
if match {
raq = append(raq, ar)
}
}
return
}
// NumberMenu presents a CLI for selecting packages to install. // NumberMenu presents a CLI for selecting packages to install.
func NumberMenu(pkgName string, flags []string) (err error) { func NumberMenu(pkgName string, narrow []string, flags []string) (err error) {
var num int var num int
var numberString string var numberString string
a, nA, err := aur.Search(pkgName, true) aq, numaq, err := aur.Search(pkgName, true)
if err != nil { if err != nil {
fmt.Println("Error during AUR search:", err) fmt.Println("Error during AUR search:", err)
} }
r, nR, err := pac.Search(pkgName) pq, numpq, err := pac.Search(pkgName)
if err != nil { if err != nil {
return return
} }
if nR == 0 && nA == 0 { if numpq == 0 && numaq == 0 {
return fmt.Errorf("no packages match search") return fmt.Errorf("no packages match search")
} }
if len(narrow) != 0 {
aq, pq = narrowSearch(aq, pq, narrow)
numaq = len(aq)
numpq = len(pq)
}
if util.SortMode == util.BottomUp { if util.SortMode == util.BottomUp {
a.PrintSearch(nR) aq.PrintSearch(numpq)
r.PrintSearch() pq.PrintSearch()
} else { } else {
r.PrintSearch() pq.PrintSearch()
a.PrintSearch(nR) aq.PrintSearch(numpq)
} }
fmt.Printf("\x1b[32m%s\x1b[0m\nNumbers:", "Type numbers to install. Separate each number with a space.") fmt.Printf("\x1b[32m%s\x1b[0m\nNumbers:", "Type numbers to install. Separate each number with a space.")
@ -58,19 +100,19 @@ func NumberMenu(pkgName string, flags []string) (err error) {
} }
// Install package // Install package
if num > nA+nR-1 || num < 0 { if num > numaq+numpq-1 || num < 0 {
continue continue
} else if num > nR-1 { } else if num > numpq-1 {
if util.SortMode == util.BottomUp { if util.SortMode == util.BottomUp {
aurInstall = append(aurInstall, a[nA+nR-num-1].Name) aurInstall = append(aurInstall, aq[numaq+numpq-num-1].Name)
} else { } else {
aurInstall = append(aurInstall, a[num-nR].Name) aurInstall = append(aurInstall, aq[num-numpq].Name)
} }
} else { } else {
if util.SortMode == util.BottomUp { if util.SortMode == util.BottomUp {
repoInstall = append(repoInstall, r[nR-num-1].Name) repoInstall = append(repoInstall, pq[numpq-num-1].Name)
} else { } else {
repoInstall = append(repoInstall, r[num].Name) repoInstall = append(repoInstall, pq[num].Name)
} }
} }
} }
@ -148,22 +190,26 @@ func Upgrade(flags []string) error {
} }
// Search presents a query to the local repos and to the AUR. // Search presents a query to the local repos and to the AUR.
func Search(pkg string) (err error) { func Search(pkg string, narrow []string) (err error) {
a, _, err := aur.Search(pkg, true) aq, _, err := aur.Search(pkg, true)
if err != nil { if err != nil {
return err return err
} }
r, _, err := pac.Search(pkg) pq, _, err := pac.Search(pkg)
if err != nil { if err != nil {
return err return err
} }
if len(narrow) != 0 {
aq, pq = narrowSearch(aq, pq, narrow)
}
if util.SortMode == util.BottomUp { if util.SortMode == util.BottomUp {
a.PrintSearch(0) aq.PrintSearch(0)
r.PrintSearch() pq.PrintSearch()
} else { } else {
r.PrintSearch() pq.PrintSearch()
a.PrintSearch(0) aq.PrintSearch(0)
} }
return nil return nil

View file

@ -90,8 +90,9 @@ func main() {
} else { } else {
util.SearchVerbosity = util.Minimal util.SearchVerbosity = util.Minimal
} }
for _, pkg := range pkgs {
err = yay.Search(pkg) if pkgs != nil {
err = yay.Search(pkgs[0], pkgs[1:])
} }
case "-S": case "-S":
err = yay.Install(pkgs, options) err = yay.Install(pkgs, options)
@ -101,9 +102,9 @@ func main() {
err = yay.SingleSearch(pkgs, options) err = yay.SingleSearch(pkgs, options)
case "yogurt": case "yogurt":
util.SearchVerbosity = util.NumberMenu util.SearchVerbosity = util.NumberMenu
for _, pkg := range pkgs {
err = yay.NumberMenu(pkg, options) if pkgs != nil {
break err = yay.NumberMenu(pkgs[0], pkgs[1:], options)
} }
case "--help", "-h": case "--help", "-h":
usage() usage()

View file

@ -10,8 +10,8 @@ import (
"github.com/jguer/yay/util" "github.com/jguer/yay/util"
) )
// RepoSearch describes a Repository search. // Query describes a Repository search.
type RepoSearch []Result type Query []Result
// Result describes a pkg. // Result describes a pkg.
type Result struct { type Result struct {
@ -61,7 +61,7 @@ func UpdatePackages(flags []string) error {
} }
// Search handles repo searches. Creates a RepoSearch struct. // Search handles repo searches. Creates a RepoSearch struct.
func Search(pkgName string) (s RepoSearch, n int, err error) { func Search(pkgName string) (s Query, n int, err error) {
h, err := conf.CreateHandle() h, err := conf.CreateHandle()
defer h.Release() defer h.Release()
if err != nil { if err != nil {
@ -143,7 +143,7 @@ func Search(pkgName string) (s RepoSearch, n int, err error) {
} }
//PrintSearch receives a RepoSearch type and outputs pretty text. //PrintSearch receives a RepoSearch type and outputs pretty text.
func (s RepoSearch) PrintSearch() { func (s Query) PrintSearch() {
for i, res := range s { for i, res := range s {
var toprint string var toprint string
if util.SearchVerbosity == util.NumberMenu { if util.SearchVerbosity == util.NumberMenu {