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"
"strings"
"github.com/jguer/yay/aur"
aur "github.com/jguer/yay/aur"
pac "github.com/jguer/yay/pacman"
"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.
func NumberMenu(pkgName string, flags []string) (err error) {
func NumberMenu(pkgName string, narrow []string, flags []string) (err error) {
var num int
var numberString string
a, nA, err := aur.Search(pkgName, true)
aq, numaq, err := aur.Search(pkgName, true)
if err != nil {
fmt.Println("Error during AUR search:", err)
}
r, nR, err := pac.Search(pkgName)
pq, numpq, err := pac.Search(pkgName)
if err != nil {
return
}
if nR == 0 && nA == 0 {
if numpq == 0 && numaq == 0 {
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 {
a.PrintSearch(nR)
r.PrintSearch()
aq.PrintSearch(numpq)
pq.PrintSearch()
} else {
r.PrintSearch()
a.PrintSearch(nR)
pq.PrintSearch()
aq.PrintSearch(numpq)
}
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
if num > nA+nR-1 || num < 0 {
if num > numaq+numpq-1 || num < 0 {
continue
} else if num > nR-1 {
} else if num > numpq-1 {
if util.SortMode == util.BottomUp {
aurInstall = append(aurInstall, a[nA+nR-num-1].Name)
aurInstall = append(aurInstall, aq[numaq+numpq-num-1].Name)
} else {
aurInstall = append(aurInstall, a[num-nR].Name)
aurInstall = append(aurInstall, aq[num-numpq].Name)
}
} else {
if util.SortMode == util.BottomUp {
repoInstall = append(repoInstall, r[nR-num-1].Name)
repoInstall = append(repoInstall, pq[numpq-num-1].Name)
} 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.
func Search(pkg string) (err error) {
a, _, err := aur.Search(pkg, true)
func Search(pkg string, narrow []string) (err error) {
aq, _, err := aur.Search(pkg, true)
if err != nil {
return err
}
r, _, err := pac.Search(pkg)
pq, _, err := pac.Search(pkg)
if err != nil {
return err
}
if len(narrow) != 0 {
aq, pq = narrowSearch(aq, pq, narrow)
}
if util.SortMode == util.BottomUp {
a.PrintSearch(0)
r.PrintSearch()
aq.PrintSearch(0)
pq.PrintSearch()
} else {
r.PrintSearch()
a.PrintSearch(0)
pq.PrintSearch()
aq.PrintSearch(0)
}
return nil

View file

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

View file

@ -10,8 +10,8 @@ import (
"github.com/jguer/yay/util"
)
// RepoSearch describes a Repository search.
type RepoSearch []Result
// Query describes a Repository search.
type Query []Result
// Result describes a pkg.
type Result struct {
@ -61,7 +61,7 @@ func UpdatePackages(flags []string) error {
}
// 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()
defer h.Release()
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.
func (s RepoSearch) PrintSearch() {
func (s Query) PrintSearch() {
for i, res := range s {
var toprint string
if util.SearchVerbosity == util.NumberMenu {