diff --git a/actions.go b/actions.go index fac7cdaa..3a896e1d 100644 --- a/actions.go +++ b/actions.go @@ -11,35 +11,9 @@ import ( "github.com/jguer/yay/aur" pac "github.com/jguer/yay/pacman" + "github.com/jguer/yay/util" ) -// SearchMode is search without numbers. -const SearchMode int = -1 - -// SortMode NumberMenu and Search -var SortMode = DownTop - -// NoConfirm ignores prompts. -var NoConfirm = false - -// BaseDir is the default building directory for yay -var BaseDir = "/tmp/yaytmp/" - -// Determines NumberMenu and Search Order -const ( - DownTop = iota - TopDown -) - -// Config copies settings over to AUR and Pacman packages -func Config() { - aur.SortMode = SortMode - pac.SortMode = SortMode - aur.NoConfirm = NoConfirm - pac.NoConfirm = NoConfirm - aur.BaseDir = BaseDir -} - // NumberMenu presents a CLI for selecting packages to install. func NumberMenu(pkgName string, flags []string) (err error) { var num int @@ -58,11 +32,11 @@ func NumberMenu(pkgName string, flags []string) (err error) { return fmt.Errorf("no packages match search") } - if aur.SortMode == aur.DownTop { + if util.SortMode == util.BottomUp { a.PrintSearch(nR) - r.PrintSearch(0) + r.PrintSearch() } else { - r.PrintSearch(0) + r.PrintSearch() a.PrintSearch(nR) } @@ -87,13 +61,13 @@ func NumberMenu(pkgName string, flags []string) (err error) { if num > nA+nR-1 || num < 0 { continue } else if num > nR-1 { - if aur.SortMode == aur.DownTop { + if util.SortMode == util.BottomUp { aurInstall = append(aurInstall, a[nA+nR-num-1].Name) } else { aurInstall = append(aurInstall, a[num-nR].Name) } } else { - if aur.SortMode == aur.DownTop { + if util.SortMode == util.BottomUp { repoInstall = append(repoInstall, r[nR-num-1].Name) } else { repoInstall = append(repoInstall, r[num].Name) @@ -184,12 +158,12 @@ func Search(pkg string) (err error) { return err } - if SortMode == aur.DownTop { - a.PrintSearch(SearchMode) - r.PrintSearch(SearchMode) + if util.SortMode == util.BottomUp { + a.PrintSearch(0) + r.PrintSearch() } else { - r.PrintSearch(SearchMode) - a.PrintSearch(SearchMode) + r.PrintSearch() + a.PrintSearch(0) } return nil @@ -326,7 +300,7 @@ func CleanDependencies(pkgs []string) error { } if len(hanging) != 0 { - if !continueTask("Confirm Removal?", "nN") { + if !util.ContinueTask("Confirm Removal?", "nN") { return nil } err = pac.CleanRemove(hanging) @@ -334,26 +308,3 @@ func CleanDependencies(pkgs []string) error { return err } - -func continueTask(s string, def string) (cont bool) { - if NoConfirm { - return true - } - var postFix string - - if def == "nN" { - postFix = "(Y/n)" - } else { - postFix = "(y/N)" - } - - var response string - fmt.Printf("\x1b[1;32m==> %s\x1b[1;37m %s\x1b[0m\n", s, postFix) - - fmt.Scanln(&response) - if response == string(def[0]) || response == string(def[1]) { - return false - } - - return true -} diff --git a/aur/aur.go b/aur/aur.go index 50681712..ab60fafc 100644 --- a/aur/aur.go +++ b/aur/aur.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/jguer/yay/pacman" + "github.com/jguer/yay/util" ) // Install sends system commands to make and install a package from pkgName @@ -61,7 +62,7 @@ func Upgrade(flags []string) error { } // Install updated packages - if !continueTask("Proceed with upgrade?", "nN") { + if !util.ContinueTask("Proceed with upgrade?", "nN") { return nil } diff --git a/aur/query.go b/aur/query.go index 8eb7f2a0..b5792446 100644 --- a/aur/query.go +++ b/aur/query.go @@ -5,6 +5,7 @@ import ( "sort" "github.com/jguer/yay/pacman" + "github.com/jguer/yay/util" ) // Query is a collection of Results @@ -15,7 +16,7 @@ func (q Query) Len() int { } func (q Query) Less(i, j int) bool { - if SortMode == DownTop { + if util.SortMode == util.BottomUp { return q[i].NumVotes < q[j].NumVotes } return q[i].NumVotes > q[j].NumVotes @@ -29,12 +30,15 @@ func (q Query) Swap(i, j int) { func (q Query) PrintSearch(start int) { for i, res := range q { var toprint string - if start != SearchMode { - if SortMode == DownTop { + if util.SearchVerbosity == util.NumberMenu { + if util.SortMode == util.BottomUp { toprint += fmt.Sprintf("%d ", len(q)+start-i-1) } else { toprint += fmt.Sprintf("%d ", start+i) } + } else if util.SearchVerbosity == util.Minimal { + fmt.Println(res.Name) + continue } toprint += fmt.Sprintf("\x1b[1m%s/\x1b[33m%s \x1b[36m%s \x1b[0m(%d) ", "aur", res.Name, res.Version, res.NumVotes) if res.Maintainer == "" { @@ -51,6 +55,8 @@ func (q Query) PrintSearch(start int) { toprint += "\n" + res.Description fmt.Println(toprint) } + + return } // Info returns an AUR search with package details diff --git a/aur/result.go b/aur/result.go index e188afad..ccc38665 100644 --- a/aur/result.go +++ b/aur/result.go @@ -6,6 +6,7 @@ import ( "os/exec" "github.com/jguer/yay/pacman" + "github.com/jguer/yay/util" ) // Result describes an AUR package. @@ -95,22 +96,22 @@ func (a *Result) Install(flags []string) (finalmdeps []string, err error) { if a.Maintainer == "" { fmt.Println("\x1b[1;31;40m==> Warning:\x1b[0;;40m This package is orphaned.\x1b[0m") } - dir := BaseDir + a.PackageBase + "/" + dir := util.BaseDir + a.PackageBase + "/" if _, err = os.Stat(dir); os.IsNotExist(err) { if err = a.setupWorkspace(); err != nil { return } } else { - if !continueTask("Directory exists. Clean Build?", "yY") { - os.RemoveAll(BaseDir + a.PackageBase) + if !util.ContinueTask("Directory exists. Clean Build?", "yY") { + os.RemoveAll(util.BaseDir + a.PackageBase) if err = a.setupWorkspace(); err != nil { return } } } - if !continueTask("Edit PKGBUILD?", "yY") { + if !util.ContinueTask("Edit PKGBUILD?", "yY") { editcmd := exec.Command(Editor, dir+"PKGBUILD") editcmd.Stdin, editcmd.Stdout, editcmd.Stderr = os.Stdin, os.Stdout, os.Stderr editcmd.Run() @@ -127,7 +128,7 @@ func (a *Result) Install(flags []string) (finalmdeps []string, err error) { finalmdeps = append(finalmdeps, makeDeps[1]...) if len(aurDeps) != 0 || len(repoDeps) != 0 { - if !continueTask("Continue?", "nN") { + if !util.ContinueTask("Continue?", "nN") { return finalmdeps, fmt.Errorf("user did not like the dependencies") } } @@ -135,7 +136,7 @@ func (a *Result) Install(flags []string) (finalmdeps []string, err error) { aurQ, n, err := MultiInfo(aurDeps) if n != len(aurDeps) { aurQ.MissingPackage(aurDeps) - if !continueTask("Continue?", "nN") { + if !util.ContinueTask("Continue?", "nN") { return finalmdeps, fmt.Errorf("unable to install dependencies") } } @@ -169,7 +170,7 @@ func (a *Result) Install(flags []string) (finalmdeps []string, err error) { var args []string args = append(args, "-sri") args = append(args, flags...) - makepkgcmd = exec.Command(MakepkgBin, args...) + makepkgcmd = exec.Command(util.MakepkgBin, args...) makepkgcmd.Stdin, makepkgcmd.Stdout, makepkgcmd.Stderr = os.Stdin, os.Stdout, os.Stderr err = makepkgcmd.Run() return @@ -238,7 +239,7 @@ func RemoveMakeDeps(depS []string) (err error) { hanging := pacman.SliceHangingPackages(depS) if len(hanging) != 0 { - if !continueTask("Confirm Removal?", "nN") { + if !util.ContinueTask("Confirm Removal?", "nN") { return nil } err = pacman.CleanRemove(hanging) @@ -249,21 +250,21 @@ func RemoveMakeDeps(depS []string) (err error) { func (a *Result) setupWorkspace() (err error) { // No need to use filepath.separators because it won't run on inferior platforms - err = os.MkdirAll(BaseDir+"builds", 0755) + err = os.MkdirAll(util.BaseDir+"builds", 0755) if err != nil { fmt.Println(err) return } - tarLocation := BaseDir + a.PackageBase + ".tar.gz" - defer os.Remove(BaseDir + a.PackageBase + ".tar.gz") + tarLocation := util.BaseDir + a.PackageBase + ".tar.gz" + defer os.Remove(util.BaseDir + a.PackageBase + ".tar.gz") err = downloadFile(tarLocation, BaseURL+a.URLPath) if err != nil { return } - err = exec.Command(TarBin, "-xf", tarLocation, "-C", BaseDir).Run() + err = exec.Command(util.TarBin, "-xf", tarLocation, "-C", util.BaseDir).Run() if err != nil { return } diff --git a/aur/utils.go b/aur/utils.go index ab2e33fa..8b1366f3 100644 --- a/aur/utils.go +++ b/aur/utils.go @@ -2,41 +2,16 @@ package aur import ( "encoding/json" - "fmt" "io" "net/http" "os" ) -// Editor gives the default system editor, uses vi in last case -var Editor = "vi" - -// TarBin describes the default installation point of tar command. -const TarBin string = "/usr/bin/tar" - // BaseURL givers the AUR default address. const BaseURL string = "https://aur.archlinux.org" -// MakepkgBin describes the default installation point of makepkg command. -const MakepkgBin string = "/usr/bin/makepkg" - -// SearchMode is search without numbers. -const SearchMode int = -1 - -// NoConfirm ignores prompts. -var NoConfirm = false - -// SortMode determines top down package or down top package display -var SortMode = DownTop - -// BaseDir is the default building directory for yay -var BaseDir = "/tmp/yaytmp/" - -// Describes Sorting method for numberdisplay -const ( - DownTop = iota - TopDown -) +// Editor gives the default system editor, uses vi in last case +var Editor = "vi" func init() { if os.Getenv("EDITOR") != "" { @@ -74,26 +49,3 @@ func downloadFile(filepath string, url string) (err error) { _, err = io.Copy(out, resp.Body) return err } - -func continueTask(s string, def string) (cont bool) { - if NoConfirm { - return true - } - var postFix string - - if def == "nN" { - postFix = "(Y/n)" - } else { - postFix = "(y/N)" - } - - var response string - fmt.Printf("\x1b[1;32m==> %s\x1b[1;37m %s\x1b[0m\n", s, postFix) - - fmt.Scanln(&response) - if response == string(def[0]) || response == string(def[1]) { - return false - } - - return true -} diff --git a/cmd/yay/yay.go b/cmd/yay/yay.go index f6610d98..34b4e9c8 100644 --- a/cmd/yay/yay.go +++ b/cmd/yay/yay.go @@ -5,6 +5,7 @@ import ( "os" "github.com/jguer/yay" + "github.com/jguer/yay/util" ) func usage() { @@ -22,6 +23,7 @@ func usage() { New operations: yay -Qstats displays system information + yay -Cd remove unneeded dependencies New options: --topdown shows repository's packages first and then aur's @@ -47,11 +49,11 @@ func parser() (op string, options []string, packages []string, err error) { if arg == "--help" { op = arg } else if arg == "--topdown" { - yay.SortMode = yay.TopDown - } else if arg == "--downtop" { - yay.SortMode = yay.DownTop + util.SortMode = util.TopDown + } else if arg == "--bottomup" { + util.SortMode = util.BottomUp } else if arg == "--noconfirm" { - yay.NoConfirm = true + util.NoConfirm = true options = append(options, arg) } else { options = append(options, arg) @@ -77,14 +79,17 @@ func main() { os.Exit(1) } - yay.Config() - switch op { case "-Cd": err = yay.CleanDependencies(pkgs) case "-Qstats": err = yay.LocalStatistics(version) - case "-Ss": + case "-Ss", "-Ssq": + if op == "-Ss" { + util.SearchVerbosity = util.Detailed + } else { + util.SearchVerbosity = util.Minimal + } for _, pkg := range pkgs { err = yay.Search(pkg) } @@ -95,6 +100,7 @@ func main() { case "-Si": err = yay.SingleSearch(pkgs, options) case "yogurt": + util.SearchVerbosity = util.NumberMenu for _, pkg := range pkgs { err = yay.NumberMenu(pkg, options) break diff --git a/pacman/pacman.go b/pacman/pacman.go index d00715db..1b33ab12 100644 --- a/pacman/pacman.go +++ b/pacman/pacman.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/jguer/go-alpm" + "github.com/jguer/yay/util" ) // RepoSearch describes a Repository search. @@ -25,12 +26,6 @@ type Result struct { // PacmanConf describes the default pacman config file const PacmanConf string = "/etc/pacman.conf" -// NoConfirm ignores prompts. -var NoConfirm = false - -// SortMode NumberMenu and Search -var SortMode = DownTop - // Determines NumberMenu and Search Order const ( DownTop = iota @@ -90,7 +85,7 @@ func Search(pkgName string) (s RepoSearch, n int, err error) { var installed bool dbS := dbList.Slice() var f int - if SortMode == DownTop { + if util.SortMode == DownTop { f = len(dbS) - 1 } else { f = 0 @@ -100,7 +95,7 @@ func Search(pkgName string) (s RepoSearch, n int, err error) { pkgS := dbS[f].PkgCache().Slice() var i int - if SortMode == DownTop { + if util.SortMode == DownTop { i = len(pkgS) - 1 } else { i = 0 @@ -125,7 +120,7 @@ func Search(pkgName string) (s RepoSearch, n int, err error) { n++ } - if SortMode == DownTop { + if util.SortMode == DownTop { if i > 0 { i-- } else { @@ -140,7 +135,7 @@ func Search(pkgName string) (s RepoSearch, n int, err error) { } } - if SortMode == DownTop { + if util.SortMode == DownTop { if f > 0 { f-- } else { @@ -159,15 +154,18 @@ func Search(pkgName string) (s RepoSearch, n int, err error) { } //PrintSearch receives a RepoSearch type and outputs pretty text. -func (s RepoSearch) PrintSearch(mode int) { +func (s RepoSearch) PrintSearch() { for i, res := range s { var toprint string - if mode != -1 { - if mode == 0 { + if util.SearchVerbosity == util.NumberMenu { + if util.SortMode == util.BottomUp { toprint += fmt.Sprintf("%d ", len(s)-i-1) } else { toprint += fmt.Sprintf("%d ", i) } + } else if util.SearchVerbosity == util.Minimal { + fmt.Println(res.Name) + continue } toprint += fmt.Sprintf("\x1b[1m%s/\x1b[33m%s \x1b[36m%s \x1b[0m", res.Repository, res.Name, res.Version) diff --git a/pacman/pacman_test.go b/pacman/pacman_test.go index d2837d89..4f17f5f3 100644 --- a/pacman/pacman_test.go +++ b/pacman/pacman_test.go @@ -1,6 +1,7 @@ package pacman import "testing" +import "github.com/jguer/yay/util" func benchmarkSearch(search string, b *testing.B) { for n := 0; n < b.N; n++ { @@ -8,7 +9,13 @@ func benchmarkSearch(search string, b *testing.B) { } } -func BenchmarkSearchSimpleTopDown(b *testing.B) { SortMode = TopDown; benchmarkSearch("chromium", b) } -func BenchmarkSearchComplexTopDown(b *testing.B) { SortMode = TopDown; benchmarkSearch("linux", b) } -func BenchmarkSearchSimpleDownTop(b *testing.B) { SortMode = DownTop; benchmarkSearch("chromium", b) } -func BenchmarkSearchComplexDownTop(b *testing.B) { SortMode = DownTop; benchmarkSearch("linux", b) } +func BenchmarkSearchSimpleTopDown(b *testing.B) { + util.SortMode = TopDown + benchmarkSearch("chromium", b) +} +func BenchmarkSearchComplexTopDown(b *testing.B) { util.SortMode = TopDown; benchmarkSearch("linux", b) } +func BenchmarkSearchSimpleDownTop(b *testing.B) { + util.SortMode = DownTop + benchmarkSearch("chromium", b) +} +func BenchmarkSearchComplexDownTop(b *testing.B) { util.SortMode = DownTop; benchmarkSearch("linux", b) } diff --git a/util/util.go b/util/util.go new file mode 100644 index 00000000..aea5bdfd --- /dev/null +++ b/util/util.go @@ -0,0 +1,59 @@ +package util + +import "fmt" + +// TarBin describes the default installation point of tar command. +const TarBin string = "/usr/bin/tar" + +// MakepkgBin describes the default installation point of makepkg command. +const MakepkgBin string = "/usr/bin/makepkg" + +// SearchVerbosity determines print method used in PrintSearch +var SearchVerbosity = NumberMenu + +// Verbosity settings for search +const ( + NumberMenu = iota + Detailed + Minimal +) + +// NoConfirm ignores prompts. +var NoConfirm = false + +// SortMode determines top down package or down top package display +var SortMode = BottomUp + +// BaseDir is the default building directory for yay +var BaseDir = "/tmp/yaytmp/" + +// Describes Sorting method for numberdisplay +const ( + BottomUp = iota + TopDown +) + +// ContinueTask prompts if user wants to continue task. +//If NoConfirm is set the action will continue without user input. +func ContinueTask(s string, def string) (cont bool) { + if NoConfirm { + return true + } + var postFix string + + if def == "nN" { + postFix = "(Y/n)" + } else { + postFix = "(y/N)" + } + + var response string + fmt.Printf("\x1b[1;32m==> %s\x1b[1;37m %s\x1b[0m\n", s, postFix) + + fmt.Scanln(&response) + if response == string(def[0]) || response == string(def[1]) { + return false + } + + return true +}