From bd619929297187ad4154b3126cc8fb0d68c5d3b1 Mon Sep 17 00:00:00 2001 From: jdholtz Date: Sun, 3 Dec 2023 10:55:48 -0800 Subject: [PATCH] Add the option to display package URLs in the search results --- cmd_test.go | 2 +- completions/bash | 2 +- completions/fish | 1 + completions/zsh | 1 + doc/yay.8 | 4 +++ pkg/db/mock/repo.go | 3 +- pkg/query/query_builder.go | 7 ++-- pkg/query/query_builder_test.go | 60 ++++++++++++++++++++++++++------- pkg/query/types.go | 39 ++++++++++++++------- pkg/runtime/runtime.go | 2 +- pkg/settings/args.go | 2 ++ pkg/settings/config.go | 1 + pkg/settings/parser/parser.go | 1 + query_test.go | 2 +- 14 files changed, 94 insertions(+), 33 deletions(-) diff --git a/cmd_test.go b/cmd_test.go index 5f3a3cdd..15d4d5c2 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -113,7 +113,7 @@ func TestYogurtMenuAURDB(t *testing.T) { CmdBuilder: cmdBuilder, VCSStore: &vcs.Mock{}, QueryBuilder: query.NewSourceQueryBuilder(aurCache, logger, "votes", parser.ModeAny, "name", - true, false, true), + true, false, true, false), AURClient: aurCache, } err = handleCmd(context.Background(), run, cmdArgs, db) diff --git a/completions/bash b/completions/bash index 23743edb..9e09e6ae 100644 --- a/completions/bash +++ b/completions/bash @@ -71,7 +71,7 @@ _yay() { noconfirm noprogressbar noscriptlet quiet root verbose makepkg pacman git gpg gpgflags config requestsplitn sudoloop nosudoloop redownload noredownload redownloadall rebuild rebuildall rebuildtree norebuild sortby - singlelineresults doublelineresults answerclean answerdiff answeredit answerupgrade noanswerclean noanswerdiff + singlelineresults doublelineresults showpackageurls answerclean answerdiff answeredit answerupgrade noanswerclean noanswerdiff noansweredit noanswerupgrade cleanmenu diffmenu editmenu cleanafter nocleanafter keepsrc nocleanmenu nodiffmenu provides noprovides pgpfetch nopgpfetch useask nouseask combinedupgrade nocombinedupgrade aur repo makepkgconf diff --git a/completions/fish b/completions/fish index eb89d0b1..38da5428 100644 --- a/completions/fish +++ b/completions/fish @@ -227,6 +227,7 @@ complete -c $progname -n "not $noopt" -l topdown -d 'Shows repository packages f complete -c $progname -n "not $noopt" -l bottomup -d 'Shows aur packages first and then repository' -f complete -c $progname -n "not $noopt" -l singlelineresults -d 'List each search result on its own line' -f complete -c $progname -n "not $noopt" -l doublelineresults -d 'List each search result on two lines, like pacman' -f +complete -c $progname -n "not $noopt" -l showpackageurls -d 'Show the URL of each package in the search results' -f complete -c $progname -n "not $noopt" -l devel -d 'Check -git/-svn/-hg development version' -f complete -c $progname -n "not $noopt" -l nodevel -d 'Disable development version checking' -f complete -c $progname -n "not $noopt" -l cleanafter -d 'Clean package sources after successful build' -f diff --git a/completions/zsh b/completions/zsh index e1f1ec43..93bf36f9 100644 --- a/completions/zsh +++ b/completions/zsh @@ -82,6 +82,7 @@ _pacman_opts_common=( '--topdown[Show repository packages first]' '--singlelineresults[List each search result on its own line]' '--doublelineresults[List each search result on two lines, like pacman]' + '--showpackageurls[Show the URL of each package in the search results]' '--devel[Check -git/-svn/-hg development version]' '--nodevel[Disable development version checking]' '--cleanafter[Clean package sources after successful build]' diff --git a/doc/yay.8 b/doc/yay.8 index 2e0314ce..c17a6185 100644 --- a/doc/yay.8 +++ b/doc/yay.8 @@ -359,6 +359,10 @@ on its own line. Follow pacman's double-line search result format and list each result using two lines. +.TP +.B \-\-showpackageurls +Show the URL of each package in the search results. + .TP .B \-\-devel During sysupgrade also check AUR development packages for updates. Currently diff --git a/pkg/db/mock/repo.go b/pkg/db/mock/repo.go index d8feae53..1e334183 100644 --- a/pkg/db/mock/repo.go +++ b/pkg/db/mock/repo.go @@ -27,6 +27,7 @@ func (d DependList) ForEach(f func(*alpm.Depend) error) error { } type Package struct { + PArchitecture string PBase string PBuildDate time.Time PDB *DB @@ -95,7 +96,7 @@ func (p *Package) Validation() alpm.Validation { // Architecture returns the package target Architecture. func (p *Package) Architecture() string { - panic("not implemented") + return p.PArchitecture } // Backup returns a list of package backups. diff --git a/pkg/query/query_builder.go b/pkg/query/query_builder.go index e607f9eb..cace9583 100644 --- a/pkg/query/query_builder.go +++ b/pkg/query/query_builder.go @@ -47,6 +47,7 @@ type SourceQueryBuilder struct { bottomUp bool singleLineResults bool separateSources bool + showPackageURLs bool aurClient aur.QueryClient logger *text.Logger @@ -61,6 +62,7 @@ func NewSourceQueryBuilder( bottomUp, singleLineResults bool, separateSources bool, + showPackageURLs bool, ) *SourceQueryBuilder { return &SourceQueryBuilder{ aurClient: aurClient, @@ -71,6 +73,7 @@ func NewSourceQueryBuilder( searchBy: searchBy, singleLineResults: singleLineResults, separateSources: separateSources, + showPackageURLs: showPackageURLs, queryMap: map[string]map[string]interface{}{}, results: make([]abstractResult, 0, 100), } @@ -237,9 +240,9 @@ func (s *SourceQueryBuilder) Results(dbExecutor db.Executor, verboseSearch Searc switch pPkg := pkg.(type) { case aur.Pkg: - toPrint += aurPkgSearchString(&pPkg, dbExecutor, s.singleLineResults) + toPrint += aurPkgSearchString(&pPkg, dbExecutor, s.singleLineResults, s.showPackageURLs) case alpm.IPackage: - toPrint += syncPkgSearchString(pPkg, dbExecutor, s.singleLineResults) + toPrint += syncPkgSearchString(pPkg, dbExecutor, s.singleLineResults, s.showPackageURLs) } s.logger.Println(toPrint) diff --git a/pkg/query/query_builder_test.go b/pkg/query/query_builder_test.go index 6d87bfed..1c1ac40d 100644 --- a/pkg/query/query_builder_test.go +++ b/pkg/query/query_builder_test.go @@ -31,6 +31,7 @@ func TestSourceQueryBuilder(t *testing.T) { targetMode parser.TargetMode singleLineResults bool searchBy string + showPackageURLs bool wantResults []string wantOutput []string } @@ -260,6 +261,37 @@ func TestSourceQueryBuilder(t *testing.T) { "\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\tThe Linux ZEN kernel and modules\n", }, }, + { + desc: "sort-by-name showpackageurls", + search: []string{"linux"}, + bottomUp: true, + separateSources: true, + sortBy: "name", + verbosity: Detailed, + showPackageURLs: true, + wantResults: []string{"linux-ck", "linux", "linux-zen"}, + wantOutput: []string{ + "\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n The Linux-ck kernel and modules with ck's hrtimer patches\n Package URL: https://aur.archlinux.org/packages/linux-ck\n", + "\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux kernel and modules\n Package URL: https://archlinux.org/packages/core/any/linux\n", + "\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux ZEN kernel and modules\n Package URL: https://archlinux.org/packages/core/any/linux-zen\n", + }, + }, + { + desc: "sort-by-name singleline showpackageurls", + search: []string{"linux"}, + bottomUp: true, + separateSources: true, + sortBy: "name", + verbosity: Detailed, + singleLineResults: true, + showPackageURLs: true, + wantResults: []string{"linux-ck", "linux", "linux-zen"}, + wantOutput: []string{ + "\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\tThe Linux-ck kernel and modules with ck's hrtimer patches\tPackage URL: https://aur.archlinux.org/packages/linux-ck\n", + "\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\tThe Linux kernel and modules\tPackage URL: https://archlinux.org/packages/core/any/linux\n", + "\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\tThe Linux ZEN kernel and modules\tPackage URL: https://archlinux.org/packages/core/any/linux-zen\n", + }, + }, { desc: "sort-by-name search-by-name", search: []string{"linux-ck"}, @@ -293,20 +325,22 @@ func TestSourceQueryBuilder(t *testing.T) { mockDB := mock.NewDB("core") return []mock.IPackage{ &mock.Package{ - PName: "linux", - PVersion: "5.16.0", - PDescription: "The Linux kernel and modules", - PSize: 1, - PISize: 1, - PDB: mockDB, + PName: "linux", + PVersion: "5.16.0", + PDescription: "The Linux kernel and modules", + PArchitecture: "any", + PSize: 1, + PISize: 1, + PDB: mockDB, }, &mock.Package{ - PName: "linux-zen", - PVersion: "5.16.0", - PDescription: "The Linux ZEN kernel and modules", - PSize: 1, - PISize: 1, - PDB: mockDB, + PName: "linux-zen", + PVersion: "5.16.0", + PDescription: "The Linux ZEN kernel and modules", + PArchitecture: "any", + PSize: 1, + PISize: 1, + PDB: mockDB, }, } }, @@ -344,7 +378,7 @@ func TestSourceQueryBuilder(t *testing.T) { queryBuilder := NewSourceQueryBuilder(mockAUR, text.NewLogger(w, io.Discard, strings.NewReader(""), false, "test"), tc.sortBy, tc.targetMode, tc.searchBy, tc.bottomUp, - tc.singleLineResults, tc.separateSources) + tc.singleLineResults, tc.separateSources, tc.showPackageURLs) queryBuilder.Execute(context.Background(), mockDB, tc.search) diff --git a/pkg/query/types.go b/pkg/query/types.go index 2b158348..9a132b07 100644 --- a/pkg/query/types.go +++ b/pkg/query/types.go @@ -51,7 +51,13 @@ func aurPkgSearchString( pkg *aur.Pkg, dbExecutor db.Executor, singleLineResults bool, + showPackageURLs bool, ) string { + lineEnding := "\n " + if singleLineResults { + lineEnding = "\t" + } + toPrint := text.Bold(text.ColorHash("aur")) + "/" + text.Bold(pkg.Name) + " " + text.Cyan(pkg.Version) + text.Bold(" (+"+strconv.Itoa(pkg.NumVotes)) + @@ -73,19 +79,24 @@ func aurPkgSearchString( } } - if singleLineResults { - toPrint += "\t" - } else { - toPrint += "\n " - } - + toPrint += lineEnding toPrint += pkg.Description + if showPackageURLs { + toPrint += lineEnding + toPrint += "Package URL: https://aur.archlinux.org/packages/" + pkg.Name + } + return toPrint } // PrintSearch receives a RepoSearch type and outputs pretty text. -func syncPkgSearchString(pkg alpm.IPackage, dbExecutor db.Executor, singleLineResults bool) string { +func syncPkgSearchString(pkg alpm.IPackage, dbExecutor db.Executor, singleLineResults, showPackageURLs bool) string { + lineEnding := "\n " + if singleLineResults { + lineEnding = "\t" + } + toPrint := text.Bold(text.ColorHash(pkg.DB().Name())) + "/" + text.Bold(pkg.Name()) + " " + text.Cyan(pkg.Version()) + text.Bold(" ("+text.Human(pkg.Size())+ @@ -104,13 +115,15 @@ func syncPkgSearchString(pkg alpm.IPackage, dbExecutor db.Executor, singleLineRe } } - if singleLineResults { - toPrint += "\t" - } else { - toPrint += "\n " - } - + toPrint += lineEnding toPrint += pkg.Description() + if showPackageURLs { + toPrint += lineEnding + toPrint += fmt.Sprintf( + "Package URL: https://archlinux.org/packages/%s/%s/%s", + pkg.DB().Name(), pkg.Architecture(), pkg.Name(), + ) + } return toPrint } diff --git a/pkg/runtime/runtime.go b/pkg/runtime/runtime.go index 499ee4f0..8b6da1f6 100644 --- a/pkg/runtime/runtime.go +++ b/pkg/runtime/runtime.go @@ -108,7 +108,7 @@ func NewRuntime(cfg *settings.Configuration, cmdArgs *parser.Arguments, version aurClient, logger.Child("mixed.querybuilder"), cfg.SortBy, cfg.Mode, cfg.SearchBy, - cfg.BottomUp, cfg.SingleLineResults, cfg.SeparateSources) + cfg.BottomUp, cfg.SingleLineResults, cfg.SeparateSources, cfg.ShowPackageURLs) run := &Runtime{ Cfg: cfg, diff --git a/pkg/settings/args.go b/pkg/settings/args.go index 856ec9dc..109c2211 100644 --- a/pkg/settings/args.go +++ b/pkg/settings/args.go @@ -85,6 +85,8 @@ func (c *Configuration) handleOption(option, value string, logger *text.Logger) c.SingleLineResults = true case "doublelineresults": c.SingleLineResults = false + case "showpackageurls": + c.ShowPackageURLs = boolValue case "completioninterval": n, err := strconv.Atoi(value) if err == nil { diff --git a/pkg/settings/config.go b/pkg/settings/config.go index aba88249..8e10711e 100644 --- a/pkg/settings/config.go +++ b/pkg/settings/config.go @@ -67,6 +67,7 @@ type Configuration struct { BatchInstall bool `json:"batchinstall"` SingleLineResults bool `json:"singlelineresults"` SeparateSources bool `json:"separatesources"` + ShowPackageURLs bool `json:"showpackageurls"` Debug bool `json:"debug"` UseRPC bool `json:"rpc"` DoubleConfirm bool `json:"doubleconfirm"` // confirm install before and after build diff --git a/pkg/settings/parser/parser.go b/pkg/settings/parser/parser.go index 885bf9c1..97035917 100644 --- a/pkg/settings/parser/parser.go +++ b/pkg/settings/parser/parser.go @@ -451,6 +451,7 @@ func isArg(arg string) bool { case "singlelineresults": case "doublelineresults": case "separatesources", "noseparatesources": + case "showpackageurls": default: return false } diff --git a/query_test.go b/query_test.go index 11231a9c..f2333ed8 100644 --- a/query_test.go +++ b/query_test.go @@ -272,7 +272,7 @@ func TestSyncSearchAURDB(t *testing.T) { CmdBuilder: cmdBuilder, AURClient: mockAUR, QueryBuilder: query.NewSourceQueryBuilder(mockAUR, newTestLogger(), "votes", parser.ModeAny, "name", - tc.bottomUp, tc.singleLine, tc.mixed), + tc.bottomUp, tc.singleLine, tc.mixed, false), Logger: newTestLogger(), Cfg: &settings.Configuration{}, }