Skip to content

Commit

Permalink
Resolves #3 (again). Filtering is now faster and happens in AUR and P…
Browse files Browse the repository at this point in the history
…acman
  • Loading branch information
Jguer committed Feb 18, 2017
1 parent 14b46a7 commit 1ebe472
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 80 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Yay was created with a few objectives in mind and based on the design of yaourt

### Changelog

#### 1.101
- Search speed and quality improved [#3](https://github.com/Jguer/yay/issues/3)

#### 1.100
- Added manpage
- Improved search [#3](https://github.com/Jguer/yay/issues/3)
Expand Down
63 changes: 9 additions & 54 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,15 @@ import (
"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, narrow []string, flags []string) (err error) {
func NumberMenu(pkgS []string, flags []string) (err error) {
var num int

aq, numaq, err := aur.Search(pkgName, true)
aq, numaq, err := aur.Search(pkgS, true)
if err != nil {
fmt.Println("Error during AUR search:", err)
}
pq, numpq, err := pac.Search(pkgName)
pq, numpq, err := pac.Search(pkgS)
if err != nil {
return
}
Expand All @@ -66,12 +31,6 @@ func NumberMenu(pkgName string, narrow []string, flags []string) (err error) {
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 {
aq.PrintSearch(numpq)
pq.PrintSearch()
Expand Down Expand Up @@ -188,21 +147,17 @@ func Upgrade(flags []string) error {
return erra
}

// Search presents a query to the local repos and to the AUR.
func Search(pkg string, narrow []string) (err error) {
aq, _, err := aur.Search(pkg, true)
// SyncSearch presents a query to the local repos and to the AUR.
func SyncSearch(pkgS []string) (err error) {
aq, _, err := aur.Search(pkgS, true)
if err != nil {
return err
}
pq, _, err := pac.Search(pkg)
pq, _, err := pac.Search(pkgS)
if err != nil {
return err
}

if len(narrow) != 0 {
aq, pq = narrowSearch(aq, pq, narrow)
}

if util.SortMode == util.BottomUp {
aq.PrintSearch(0)
pq.PrintSearch()
Expand All @@ -214,8 +169,8 @@ func Search(pkg string, narrow []string) (err error) {
return nil
}

// SingleSearch serves as a pacman -Si for repo packages and AUR packages.
func SingleSearch(pkgS []string, flags []string) (err error) {
// SyncInfo serves as a pacman -Si for repo packages and AUR packages.
func SyncInfo(pkgS []string, flags []string) (err error) {
aurS, repoS, err := pac.PackageSlices(pkgS)
if err != nil {
return
Expand Down
4 changes: 2 additions & 2 deletions aur/aur_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestSearch(t *testing.T) {

eN := "yay"
result, _, err := Search("yay", true)
result, _, err := Search([]string{"yay"}, true)
if err != nil {
t.Fatalf("Expected err to be nil but it was %s", err)
}
Expand All @@ -30,7 +30,7 @@ func TestSearch(t *testing.T) {
func benchmarkSearch(search string, sort bool, b *testing.B) {

for n := 0; n < b.N; n++ {
Search(search, sort)
Search([]string{search}, sort)
}
}

Expand Down
41 changes: 30 additions & 11 deletions aur/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aur
import (
"fmt"
"sort"
"strings"

"github.com/jguer/yay/pacman"
"github.com/jguer/yay/util"
Expand Down Expand Up @@ -91,27 +92,45 @@ func MultiInfo(pkgS []string) (Query, int, error) {
}

// Search returns an AUR search
func Search(pkg string, sortS bool) (Query, int, error) {
func Search(pkgS []string, sortS bool) (Query, int, error) {
type returned struct {
Results Query `json:"results"`
ResultCount int `json:"resultcount"`
}
r := returned{}
err := getJSON("https://aur.archlinux.org/rpc/?v=5&type=search&arg="+pkg, &r)
err := getJSON("https://aur.archlinux.org/rpc/?v=5&type=search&arg="+pkgS[0], &r)

if sortS {
sort.Sort(r.Results)
}
var aq Query
n := 0
setter := pacman.PFactory(pFSetTrue)
var fri int

for i, res := range r.Results {
if i == len(r.Results)-1 {
setter(res.Name, &r.Results[i], true)
continue
for _, res := range r.Results {
match := true
for _, pkgN := range pkgS[1:] {
if !(strings.Contains(res.Name, pkgN) || strings.Contains(strings.ToLower(res.Description), pkgN)) {
match = false
break
}
}

if match {
n++
aq = append(aq, res)
fri = len(aq) - 1
setter(aq[fri].Name, &aq[fri], false)
}
setter(res.Name, &r.Results[i], false)
}
return r.Results, r.ResultCount, err

if aq != nil {
setter(aq[fri].Name, &aq[fri], true)
}

if sortS {
sort.Sort(aq)
}

return aq, n, err
}

// This is very dirty but it works so good.
Expand Down
9 changes: 3 additions & 6 deletions cmd/yay/yay.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,17 @@ func main() {
}

if pkgs != nil {
err = yay.Search(pkgs[0], pkgs[1:])
err = yay.SyncSearch(pkgs)
}
case "-S":
err = yay.Install(pkgs, options)
case "-Syu", "-Suy":
err = yay.Upgrade(options)
case "-Si":
err = yay.SingleSearch(pkgs, options)
err = yay.SyncInfo(pkgs, options)
case "yogurt":
util.SearchVerbosity = util.NumberMenu

if pkgs != nil {
err = yay.NumberMenu(pkgs[0], pkgs[1:], options)
}
err = yay.NumberMenu(pkgs, options)
default:
err = yay.PassToPacman(op, pkgs, options)
}
Expand Down
17 changes: 12 additions & 5 deletions pacman/pacman.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func UpdatePackages(flags []string) error {
}

// Search handles repo searches. Creates a RepoSearch struct.
func Search(pkgName string) (s Query, n int, err error) {
func Search(pkgInputN []string) (s Query, n int, err error) {
h, err := conf.CreateHandle()
defer h.Release()
if err != nil {
Expand Down Expand Up @@ -108,12 +108,20 @@ func Search(pkgName string) (s Query, n int, err error) {

for i := initL(lenPkgs); compL(lenPkgs, i); i = finalL(i) {

if strings.Contains(pkgS[i].Name(), pkgName) || strings.Contains(strings.ToLower(pkgS[i].Description()), pkgName) {
match := true
for _, pkgN := range pkgInputN {
if !(strings.Contains(pkgS[i].Name(), pkgN) || strings.Contains(strings.ToLower(pkgS[i].Description()), pkgN)) {
match = false
break
}
}

if match {
installed = false
if r, _ := localDb.PkgByName(pkgS[i].Name()); r != nil {
installed = true
} else {
installed = false
}
n++

s = append(s, Result{
Name: pkgS[i].Name(),
Expand All @@ -123,7 +131,6 @@ func Search(pkgName string) (s Query, n int, err error) {
Group: strings.Join(pkgS[i].Groups().Slice(), ","),
Installed: installed,
})
n++
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pacman/pacman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func benchmarkPrintSearch(search string, b *testing.B) {
os.Stdout = w

for n := 0; n < b.N; n++ {
res, _, _ := Search(search)
res, _, _ := Search(append([]string{}, search))
res.PrintSearch()
}
os.Stdout = old
Expand All @@ -36,7 +36,7 @@ func BenchmarkPrintSearchComplexBottomUp(b *testing.B) {

func benchmarkSearch(search string, b *testing.B) {
for n := 0; n < b.N; n++ {
Search(search)
Search(append([]string{}, search))
}
}
func BenchmarkSearchSimpleTopDown(b *testing.B) {
Expand Down

0 comments on commit 1ebe472

Please sign in to comment.