yay/print.go

354 lines
10 KiB
Go
Raw Normal View History

package main
import (
"fmt"
2018-02-19 18:36:33 +01:00
"os"
"strconv"
"strings"
"github.com/leonelquinteros/gotext"
rpc "github.com/mikkeloscar/aur"
"github.com/Jguer/go-alpm"
"github.com/Jguer/yay/v10/pkg/db"
"github.com/Jguer/yay/v10/pkg/intrange"
"github.com/Jguer/yay/v10/pkg/query"
"github.com/Jguer/yay/v10/pkg/settings"
"github.com/Jguer/yay/v10/pkg/stringset"
"github.com/Jguer/yay/v10/pkg/text"
)
// PrintSearch handles printing search results in a given format
func (q aurQuery) printSearch(start int, dbExecutor *db.AlpmExecutor) {
for i := range q {
var toprint string
if config.SearchMode == numberMenu {
switch config.SortMode {
case settings.TopDown:
toprint += magenta(strconv.Itoa(start+i) + " ")
case settings.BottomUp:
toprint += magenta(strconv.Itoa(len(q)+start-i-1) + " ")
default:
2020-05-08 18:13:51 +02:00
text.Warnln(gotext.Get("invalid sort mode. Fix with yay -Y --bottomup --save"))
}
} else if config.SearchMode == minimal {
fmt.Println(q[i].Name)
continue
}
toprint += bold(text.ColorHash("aur")) + "/" + bold(q[i].Name) +
" " + cyan(q[i].Version) +
bold(" (+"+strconv.Itoa(q[i].NumVotes)) +
2020-06-07 14:29:47 +02:00
" " + bold(strconv.FormatFloat(q[i].Popularity, 'f', 2, 64)+") ")
if q[i].Maintainer == "" {
toprint += bold(red(gotext.Get("(Orphaned)"))) + " "
}
if q[i].OutOfDate != 0 {
2020-06-26 09:15:36 +02:00
toprint += bold(red(gotext.Get("(Out-of-date: %s)", text.FormatTime(q[i].OutOfDate)))) + " "
}
if pkg := dbExecutor.LocalPackage(q[i].Name); pkg != nil {
if pkg.Version() != q[i].Version {
2020-05-08 18:13:51 +02:00
toprint += bold(green(gotext.Get("(Installed: %s)", pkg.Version())))
} else {
toprint += bold(green(gotext.Get("(Installed)")))
}
}
toprint += "\n " + q[i].Description
fmt.Println(toprint)
}
}
// PrintSearch receives a RepoSearch type and outputs pretty text.
func (s repoQuery) printSearch(dbExecutor *db.AlpmExecutor) {
for i, res := range s {
var toprint string
if config.SearchMode == numberMenu {
switch config.SortMode {
case settings.TopDown:
toprint += magenta(strconv.Itoa(i+1) + " ")
case settings.BottomUp:
toprint += magenta(strconv.Itoa(len(s)-i) + " ")
default:
2020-05-08 18:13:51 +02:00
text.Warnln(gotext.Get("invalid sort mode. Fix with yay -Y --bottomup --save"))
}
} else if config.SearchMode == minimal {
fmt.Println(res.Name())
continue
}
toprint += bold(text.ColorHash(res.DB().Name())) + "/" + bold(res.Name()) +
" " + cyan(res.Version()) +
bold(" ("+text.Human(res.Size())+
" "+text.Human(res.ISize())+") ")
packageGroups := dbExecutor.PackageGroups(res)
if len(packageGroups) != 0 {
toprint += fmt.Sprint(packageGroups, " ")
}
if pkg := dbExecutor.LocalPackage(res.Name()); pkg != nil {
if pkg.Version() != res.Version() {
toprint += bold(green(gotext.Get("(Installed: %s)", pkg.Version())))
} else {
toprint += bold(green(gotext.Get("(Installed)")))
}
}
toprint += "\n " + res.Description()
fmt.Println(toprint)
}
}
// Pretty print a set of packages from the same package base.
func (u *upgrade) StylizedNameWithRepository() string {
return bold(text.ColorHash(u.Repository)) + "/" + bold(u.Name)
2018-05-02 17:46:21 +02:00
}
// Print prints the details of the packages to upgrade.
2018-05-20 17:17:05 +02:00
func (u upSlice) print() {
2018-05-02 17:46:21 +02:00
longestName, longestVersion := 0, 0
for _, pack := range u {
packNameLen := len(pack.StylizedNameWithRepository())
packVersion, _ := getVersionDiff(pack.LocalVersion, pack.RemoteVersion)
packVersionLen := len(packVersion)
longestName = intrange.Max(packNameLen, longestName)
longestVersion = intrange.Max(packVersionLen, longestVersion)
2018-05-02 17:46:21 +02:00
}
namePadding := fmt.Sprintf("%%-%ds ", longestName)
versionPadding := fmt.Sprintf("%%-%ds", longestVersion)
numberPadding := fmt.Sprintf("%%%dd ", len(fmt.Sprintf("%v", len(u))))
for k, i := range u {
left, right := getVersionDiff(i.LocalVersion, i.RemoteVersion)
2018-05-02 17:46:21 +02:00
fmt.Print(magenta(fmt.Sprintf(numberPadding, len(u)-k)))
fmt.Printf(namePadding, i.StylizedNameWithRepository())
2018-05-02 17:46:21 +02:00
fmt.Printf("%s -> %s\n", fmt.Sprintf(versionPadding, left), right)
}
}
// PrintInfo prints package info like pacman -Si.
2020-07-08 03:22:01 +02:00
func PrintInfo(a *rpc.Pkg, extendedInfo bool) {
text.PrintInfoValue(gotext.Get("Repository"), "aur")
text.PrintInfoValue(gotext.Get("Name"), a.Name)
text.PrintInfoValue(gotext.Get("Keywords"), strings.Join(a.Keywords, " "))
text.PrintInfoValue(gotext.Get("Version"), a.Version)
text.PrintInfoValue(gotext.Get("Description"), a.Description)
text.PrintInfoValue(gotext.Get("URL"), a.URL)
text.PrintInfoValue(gotext.Get("AUR URL"), config.AURURL+"/packages/"+a.Name)
text.PrintInfoValue(gotext.Get("Groups"), strings.Join(a.Groups, " "))
text.PrintInfoValue(gotext.Get("Licenses"), strings.Join(a.License, " "))
text.PrintInfoValue(gotext.Get("Provides"), strings.Join(a.Provides, " "))
text.PrintInfoValue(gotext.Get("Depends On"), strings.Join(a.Depends, " "))
text.PrintInfoValue(gotext.Get("Make Deps"), strings.Join(a.MakeDepends, " "))
text.PrintInfoValue(gotext.Get("Check Deps"), strings.Join(a.CheckDepends, " "))
text.PrintInfoValue(gotext.Get("Optional Deps"), strings.Join(a.OptDepends, " "))
text.PrintInfoValue(gotext.Get("Conflicts With"), strings.Join(a.Conflicts, " "))
text.PrintInfoValue(gotext.Get("Maintainer"), a.Maintainer)
text.PrintInfoValue(gotext.Get("Votes"), fmt.Sprintf("%d", a.NumVotes))
text.PrintInfoValue(gotext.Get("Popularity"), fmt.Sprintf("%f", a.Popularity))
2020-06-26 09:15:36 +02:00
text.PrintInfoValue(gotext.Get("First Submitted"), text.FormatTimeQuery(a.FirstSubmitted))
text.PrintInfoValue(gotext.Get("Last Modified"), text.FormatTimeQuery(a.LastModified))
2018-07-24 02:48:36 +02:00
if a.OutOfDate != 0 {
2020-06-26 09:15:36 +02:00
text.PrintInfoValue(gotext.Get("Out-of-date"), text.FormatTimeQuery(a.OutOfDate))
} else {
text.PrintInfoValue(gotext.Get("Out-of-date"), "No")
}
2020-07-08 03:22:01 +02:00
if extendedInfo {
text.PrintInfoValue("ID", fmt.Sprintf("%d", a.ID))
text.PrintInfoValue(gotext.Get("Package Base ID"), fmt.Sprintf("%d", a.PackageBaseID))
text.PrintInfoValue(gotext.Get("Package Base"), a.PackageBase)
text.PrintInfoValue(gotext.Get("Snapshot URL"), config.AURURL+a.URLPath)
2018-07-24 02:48:36 +02:00
}
fmt.Println()
}
// BiggestPackages prints the name of the ten biggest packages in the system.
func biggestPackages(alpmHandle *alpm.Handle) {
2019-02-04 17:56:02 +01:00
localDB, err := alpmHandle.LocalDB()
if err != nil {
return
}
2019-02-04 17:56:02 +01:00
pkgCache := localDB.PkgCache()
pkgS := pkgCache.SortBySize().Slice()
if len(pkgS) < 10 {
return
}
for i := 0; i < 10; i++ {
fmt.Printf("%s: %s\n", bold(pkgS[i].Name()), cyan(text.Human(pkgS[i].ISize())))
}
// Could implement size here as well, but we just want the general idea
}
2017-10-19 07:59:26 +02:00
// localStatistics prints installed packages statistics.
func localStatistics(alpmHandle *alpm.Handle) error {
info, err := statistics(alpmHandle)
2017-10-19 07:59:26 +02:00
if err != nil {
return err
}
_, remoteNames, err := query.GetPackageNamesBySource(alpmHandle)
2017-10-19 07:59:26 +02:00
if err != nil {
return err
}
text.Infoln(gotext.Get("Yay version v%s", yayVersion))
fmt.Println(bold(cyan("===========================================")))
text.Infoln(gotext.Get("Total installed packages: %s", cyan(strconv.Itoa(info.Totaln))))
text.Infoln(gotext.Get("Total foreign installed packages: %s", cyan(strconv.Itoa(len(remoteNames)))))
text.Infoln(gotext.Get("Explicitly installed packages: %s", cyan(strconv.Itoa(info.Expln))))
text.Infoln(gotext.Get("Total Size occupied by packages: %s", cyan(text.Human(info.TotalSize))))
fmt.Println(bold(cyan("===========================================")))
text.Infoln(gotext.Get("Ten biggest packages:"))
biggestPackages(alpmHandle)
fmt.Println(bold(cyan("===========================================")))
2017-10-19 07:59:26 +02:00
2020-07-10 02:36:45 +02:00
query.AURInfoPrint(remoteNames, config.RequestSplitN)
2017-10-19 07:59:26 +02:00
return nil
}
New install algorithm I have replaced the old install and dependancy algorithms with a new design that attemps to be more pacaur like. Mostly in minimizing user input. Ask every thing first then do everything with no need for more user input. It is not yet fully complete but is finished enough so that it works, should not fail in most cases and provides a base for more contributors to help address the existing problems. The new install chain is as follows: Source info about the provided targets Fetch a list of all dependancies needed to install targets I put alot of effort into fetching the dependancy tree while making the least amount of aur requests as possible. I'm actually very happy with how it turned out and yay wil now resolve dependancies noticably faster than pacaur when there are many aur dependancies. Install repo targets by passing to pacman Print dependancy tree and ask to confirm Ask to clean build if directory already exists Download all pkgbuilds Ask to edit all pkgbuilds Ask to continue with the install Download the sources for each packagebuild Build and install every package using -s to get repo deps and -i to install Ask to remove make dependancies There are still a lot of things that need to be done for a fully working system. Here are the problems I found with this system, either new or existing: Formating I am not so good at formatting myself, I thought best to leave it until last so I could get feedback on how it should look and help implementing it. Dependancy tree The dependancy tree is usually correct although I have noticed times where it doesnt detect all the dependancies that it should. I have only noticed this when there are circular dependancies so i think this might be the cause. It's not a big deal currently because makepkg -i installed repo deps for us which handles the repo deps for us and will get the correct ones. So yay might not list all the dependancies. but they will get installed so I consider this a visual bug. I have yet to see any circular dependancies in the AUR so I can not say what will happend but I#m guessing that it will break. Versioned packages/dependencies Targets and dependancies with version constriants such as 'linux>=4.1' will not be checked on the aur side of things but will be checked on the repo side. Ignorepkg/Ignoregroup Currently I do not handle this in any way but it shouldn't be too hard to implement. Conflict checking This is not currently implemented either Split Paclages Split packages are not Handles properly. If we only specify one package so install from a split package makepkg -i ends up installing them all anyway. If we specify more than one (n) package it will actually build the package base n times and reinstall every split package n times. Makepkg To get things working I decided to keep using the makepkg -i method. I plan to eventually replace this with a pacman -U based method. This should allow passing args such as --dbpath and --config to aur packages aswell as help solve some problems such as the split packages. Clean build I plan to improve the clean build choice to be a little more smart and instead of check if the directory exists, check if the package is already build and if so skip the build all together.
2018-01-17 22:48:23 +01:00
2020-07-08 03:40:50 +02:00
// TODO: Make it less hacky
2020-07-08 03:22:01 +02:00
func printNumberOfUpdates(alpmHandle *alpm.Handle, enableDowngrade bool) error {
2020-07-10 02:36:45 +02:00
warnings := query.NewWarnings()
2018-02-19 18:36:33 +01:00
old := os.Stdout // keep backup of the real stdout
os.Stdout = nil
2020-07-08 03:22:01 +02:00
aurUp, repoUp, err := upList(warnings, alpmHandle, enableDowngrade)
os.Stdout = old // restoring the real stdout
if err != nil {
return err
}
fmt.Println(len(aurUp) + len(repoUp))
2018-02-21 09:41:25 +01:00
return nil
}
2020-07-08 03:40:50 +02:00
// TODO: Make it less hacky
2020-07-08 03:22:01 +02:00
func printUpdateList(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle, enableDowngrade bool) error {
targets := stringset.FromSlice(cmdArgs.Targets)
2020-07-10 02:36:45 +02:00
warnings := query.NewWarnings()
old := os.Stdout // keep backup of the real stdout
os.Stdout = nil
localNames, remoteNames, err := query.GetPackageNamesBySource(alpmHandle)
if err != nil {
return err
}
2020-07-08 03:22:01 +02:00
aurUp, repoUp, err := upList(warnings, alpmHandle, enableDowngrade)
os.Stdout = old // restoring the real stdout
if err != nil {
return err
}
noTargets := len(targets) == 0
2020-07-08 03:22:01 +02:00
if !cmdArgs.ExistsArg("m", "foreign") {
for _, pkg := range repoUp {
if noTargets || targets.Get(pkg.Name) {
2020-07-08 03:22:01 +02:00
if cmdArgs.ExistsArg("q", "quiet") {
2018-09-24 11:52:17 +02:00
fmt.Printf("%s\n", pkg.Name)
} else {
fmt.Printf("%s %s -> %s\n", bold(pkg.Name), green(pkg.LocalVersion), green(pkg.RemoteVersion))
}
delete(targets, pkg.Name)
}
}
}
2020-07-08 03:22:01 +02:00
if !cmdArgs.ExistsArg("n", "native") {
for _, pkg := range aurUp {
if noTargets || targets.Get(pkg.Name) {
2020-07-08 03:22:01 +02:00
if cmdArgs.ExistsArg("q", "quiet") {
2018-09-24 11:52:17 +02:00
fmt.Printf("%s\n", pkg.Name)
} else {
fmt.Printf("%s %s -> %s\n", bold(pkg.Name), green(pkg.LocalVersion), green(pkg.RemoteVersion))
}
delete(targets, pkg.Name)
}
}
}
missing := false
outer:
for pkg := range targets {
for _, name := range localNames {
if name == pkg {
continue outer
}
}
for _, name := range remoteNames {
if name == pkg {
continue outer
}
}
text.Errorln(gotext.Get("package '%s' was not found", pkg))
missing = true
}
if missing {
return fmt.Errorf("")
}
2018-02-19 18:36:33 +01:00
return nil
}
const (
redCode = "\x1b[31m"
greenCode = "\x1b[32m"
blueCode = "\x1b[34m"
magentaCode = "\x1b[35m"
cyanCode = "\x1b[36m"
boldCode = "\x1b[1m"
resetCode = "\x1b[0m"
)
func stylize(startCode, in string) string {
if text.UseColor {
return startCode + in + resetCode
}
return in
}
func red(in string) string {
return stylize(redCode, in)
}
func green(in string) string {
return stylize(greenCode, in)
}
func blue(in string) string {
return stylize(blueCode, in)
}
func cyan(in string) string {
return stylize(cyanCode, in)
}
func magenta(in string) string {
return stylize(magentaCode, in)
}
func bold(in string) string {
return stylize(boldCode, in)
}