yay/print.go

470 lines
10 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"os"
"strconv"
"strings"
alpm "github.com/jguer/go-alpm"
rpc "github.com/mikkeloscar/aur"
)
const arrow = "==>"
// Human returns results in Human readable format.
func human(size int64) string {
floatsize := float32(size)
units := [...]string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"}
for _, unit := range units {
if floatsize < 1024 {
return fmt.Sprintf("%.1f %sB", floatsize, unit)
}
floatsize /= 1024
}
return fmt.Sprintf("%d%s", size, "B")
}
// PrintSearch handles printing search results in a given format
func (q aurQuery) printSearch(start int) {
localDb, _ := alpmHandle.LocalDb()
for i, res := range q {
var toprint string
2017-08-04 11:26:53 +02:00
if config.SearchMode == NumberMenu {
if config.SortMode == BottomUp {
toprint += yellowFg(strconv.Itoa(len(q)+start-i-1) + " ")
} else {
toprint += yellowFg(strconv.Itoa(start+i) + " ")
}
2017-08-04 11:26:53 +02:00
} else if config.SearchMode == Minimal {
fmt.Println(res.Name)
continue
}
toprint += boldWhiteFg("aur/") + boldYellowFg(res.Name) +
" " + boldCyanFg(res.Version) +
" (" + strconv.Itoa(res.NumVotes) + ") "
if res.Maintainer == "" {
toprint += redFgBlackBg("(Orphaned)") + " "
}
if res.OutOfDate != 0 {
toprint += redFgBlackBg("(Out-of-date)") + " "
}
if _, err := localDb.PkgByName(res.Name); err == nil {
toprint += greenFgBlackBg("Installed")
}
toprint += "\n " + res.Description
fmt.Println(toprint)
}
}
//PrintSearch receives a RepoSearch type and outputs pretty text.
func (s repoQuery) printSearch() {
for i, res := range s {
var toprint string
if config.SearchMode == NumberMenu {
if config.SortMode == BottomUp {
toprint += yellowFg(strconv.Itoa(len(s)-i) + " ")
} else {
toprint += yellowFg(strconv.Itoa(i+1) + " ")
}
} else if config.SearchMode == Minimal {
fmt.Println(res.Name())
continue
}
toprint += boldWhiteFg(res.DB().Name()+"/") + boldYellowFg(res.Name()) +
" " + boldCyanFg(res.Version()) + " "
if len(res.Groups().Slice()) != 0 {
toprint += fmt.Sprint(res.Groups().Slice(), " ")
}
localDb, err := alpmHandle.LocalDb()
if err == nil {
if _, err = localDb.PkgByName(res.Name()); err == nil {
toprint += greenFgBlackBg("Installed")
}
}
toprint += "\n " + res.Description()
fmt.Println(toprint)
}
}
2018-02-05 21:05:58 +01:00
// printDownloadsFromRepo prints repository packages to be downloaded
func printDepCatagories(dc *depCatagories) {
repo := ""
repoMake := ""
aur := ""
aurMake := ""
repoLen := 0
repoMakeLen := 0
aurLen := 0
aurMakeLen := 0
for _, pkg := range dc.Repo {
if dc.MakeOnly.get(pkg.Name()) {
repoMake += " " + pkg.Name()
repoMakeLen++
} else {
repo += " " + pkg.Name()
repoLen++
}
2018-02-05 21:05:58 +01:00
}
for _, pkg := range dc.Aur {
pkgStr := " " + pkg.PackageBase
pkgStrMake := pkgStr
push := false
pushMake := false
if len(dc.Bases[pkg.PackageBase]) > 1 || pkg.PackageBase != pkg.Name {
pkgStr += " ("
pkgStrMake += " ("
for _, split := range dc.Bases[pkg.PackageBase] {
if dc.MakeOnly.get(split.Name) {
pkgStrMake += split.Name + " "
aurMakeLen++
pushMake = true
} else {
pkgStr += split.Name + " "
aurLen++
push = true
}
}
pkgStr = pkgStr[:len(pkgStr)-1] + ")"
pkgStrMake = pkgStrMake[:len(pkgStrMake)-1] + ")"
} else if dc.MakeOnly.get(pkg.Name) {
aurMakeLen++
pushMake = true
} else {
aurLen++
push = true
}
if push {
aur += pkgStr
}
if pushMake {
aurMake += pkgStrMake
}
2018-02-05 21:05:58 +01:00
}
2018-02-16 18:18:59 +01:00
printDownloads("Repo", repoLen, repo)
printDownloads("Repo Make", repoMakeLen, repoMake)
printDownloads("Aur", aurLen, aur)
printDownloads("Aur Make", aurMakeLen, aurMake)
2018-02-05 21:05:58 +01:00
}
func printDownloads(repoName string, length int, packages string) {
if length < 1 {
return
2018-02-05 21:05:58 +01:00
}
repoInfo := boldBlueFg(
"[" + repoName + ": " + strconv.Itoa(length) + "]")
2018-02-05 21:18:16 +01:00
fmt.Println(repoInfo + yellowFg(packages))
2018-02-05 21:05:58 +01:00
}
func printDeps(repoDeps []string, aurDeps []string) {
if len(repoDeps) != 0 {
fmt.Print(boldGreenFg(arrow + " Repository dependencies: "))
for _, repoD := range repoDeps {
fmt.Print(yellowFg(repoD) + " ")
}
fmt.Print("\n")
}
if len(aurDeps) != 0 {
fmt.Print(boldGreenFg(arrow + " AUR dependencies: "))
for _, aurD := range aurDeps {
fmt.Print(yellowFg(aurD) + " ")
}
fmt.Print("\n")
}
}
// PrintInfo prints package info like pacman -Si.
func PrintInfo(a *rpc.Pkg) {
fmt.Println(boldWhiteFg("Repository :"), "aur")
fmt.Println(boldWhiteFg("Name :"), a.Name)
fmt.Println(boldWhiteFg("Version :"), a.Version)
fmt.Println(boldWhiteFg("Description :"), a.Description)
fmt.Println(boldWhiteFg("URL :"), a.URL)
fmt.Println(boldWhiteFg("Licenses :"), strings.Join(a.License, " "))
fmt.Println(boldWhiteFg("Depends On :"), strings.Join(a.Depends, " "))
fmt.Println(boldWhiteFg("Make Deps :"), strings.Join(a.MakeDepends, " "))
fmt.Println(boldWhiteFg("Optional Deps :"), strings.Join(a.OptDepends, " "))
fmt.Println(boldWhiteFg("Conflicts With :"), strings.Join(a.Conflicts, " "))
fmt.Println(boldWhiteFg("Maintainer :"), a.Maintainer)
fmt.Println(boldWhiteFg("Votes :"), a.NumVotes)
fmt.Println(boldWhiteFg("Popularity :"), a.Popularity)
if a.OutOfDate != 0 {
fmt.Println(boldWhiteFg("Out-of-date :"), "Yes")
}
fmt.Println()
}
// BiggestPackages prints the name of the ten biggest packages in the system.
func biggestPackages() {
localDb, err := alpmHandle.LocalDb()
if err != nil {
return
}
pkgCache := localDb.PkgCache()
pkgS := pkgCache.SortBySize().Slice()
if len(pkgS) < 10 {
return
}
for i := 0; i < 10; i++ {
fmt.Println(pkgS[i].Name() + ": " + yellowFg(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() error {
info, err := statistics()
if err != nil {
return err
}
_, _, _, remoteNames, err := filterPackages()
if err != nil {
return err
}
fmt.Printf("\n Yay version r%s\n", version)
fmt.Println(boldCyanFg("==========================================="))
fmt.Println(boldGreenFg("Total installed packages: ") + yellowFg(strconv.Itoa(info.Totaln)))
fmt.Println(boldGreenFg("Total foreign installed packages: ") + yellowFg(strconv.Itoa(len(remoteNames))))
fmt.Println(boldGreenFg("Explicitly installed packages: ") + yellowFg(strconv.Itoa(info.Expln)))
fmt.Println(boldGreenFg("Total Size occupied by packages: ") + yellowFg(human(info.TotalSize)))
fmt.Println(boldCyanFg("==========================================="))
fmt.Println(boldGreenFg("Ten biggest packages"))
2017-10-19 07:59:26 +02:00
biggestPackages()
fmt.Println(boldCyanFg("==========================================="))
2017-10-19 07:59:26 +02:00
var q aurQuery
var j int
for i := len(remoteNames); i != 0; i = j {
j = i - config.RequestSplitN
if j < 0 {
j = 0
}
qtemp, err := rpc.Info(remoteNames[j:i])
q = append(q, qtemp...)
if err != nil {
return err
}
}
var outcast []string
for _, s := range remoteNames {
found := false
for _, i := range q {
if s == i.Name {
found = true
break
}
}
if !found {
outcast = append(outcast, s)
}
}
if err != nil {
return err
}
for _, res := range q {
if res.Maintainer == "" {
fmt.Println(boldRedFgBlackBg(arrow+"Warning:"),
boldYellowFgBlackBg(res.Name), whiteFgBlackBg("is orphaned"))
2017-10-19 07:59:26 +02:00
}
if res.OutOfDate != 0 {
fmt.Println(boldRedFgBlackBg(arrow+"Warning:"),
boldYellowFgBlackBg(res.Name), whiteFgBlackBg("is out-of-date in AUR"))
2017-10-19 07:59:26 +02:00
}
}
for _, res := range outcast {
fmt.Println(boldRedFgBlackBg(arrow+"Warning:"),
boldYellowFgBlackBg(res), whiteFgBlackBg("is not available in AUR"))
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
//todo make pretty
func printMissing(missing stringSet) {
fmt.Print("Packages not found in repos or aur:")
for pkg := range missing {
fmt.Print(" ", pkg)
}
fmt.Println()
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
}
//todo make it less hacky
func printNumberOfUpdates() error {
old := os.Stdout // keep backup of the real stdout
os.Stdout = nil
aurUp, repoUp, err := upList()
os.Stdout = old // restoring the real stdout
if err != nil {
return err
}
fmt.Println(len(aurUp) + len(repoUp))
return nil
}
//todo make it less hacky
func printUpdateList() error {
old := os.Stdout // keep backup of the real stdout
os.Stdout = nil
aurUp, repoUp, err := upList()
os.Stdout = old // restoring the real stdout
if err != nil {
return err
}
for _, pkg := range repoUp {
fmt.Println(pkg.Name)
}
for _, pkg := range aurUp {
fmt.Println(pkg.Name)
}
return nil
}
func blackBg(in string) string {
if alpmConf.Options&alpm.ConfColor > 0 {
return "\x1b[0;;40m" + in + "\x1b[0m"
}
return in
}
func redFg(in string) string {
if alpmConf.Options&alpm.ConfColor > 0 {
return "\x1b[0;31m" + in + "\x1b[0m"
}
return in
}
2018-01-26 12:30:33 +01:00
func greenFg(in string) string {
if alpmConf.Options&alpm.ConfColor > 0 {
return "\x1b[0;32m" + in + "\x1b[0m"
}
return in
}
func yellowFg(in string) string {
if alpmConf.Options&alpm.ConfColor > 0 {
return "\x1b[0;33m" + in + "\x1b[0m"
}
return in
}
func boldFg(in string) string {
if alpmConf.Options&alpm.ConfColor > 0 {
return "\x1b[1m" + in + "\x1b[0m"
}
return in
}
func boldGreenFg(in string) string {
if alpmConf.Options&alpm.ConfColor > 0 {
return "\x1b[1;32m" + in + "\x1b[0m"
}
return in
}
func boldYellowFg(in string) string {
if alpmConf.Options&alpm.ConfColor > 0 {
return "\x1b[1;33m" + in + "\x1b[0m"
}
return in
}
2018-01-26 12:30:33 +01:00
func boldBlueFg(in string) string {
if alpmConf.Options&alpm.ConfColor > 0 {
return "\x1b[1;34m" + in + "\x1b[0m"
}
return in
}
func boldCyanFg(in string) string {
if alpmConf.Options&alpm.ConfColor > 0 {
return "\x1b[1;36m" + in + "\x1b[0m"
}
return in
}
func boldWhiteFg(in string) string {
if alpmConf.Options&alpm.ConfColor > 0 {
return "\x1b[1;37m" + in + "\x1b[0m"
}
return in
}
func redFgBlackBg(in string) string {
if alpmConf.Options&alpm.ConfColor > 0 {
2018-01-26 12:30:33 +01:00
return "\x1b[0;31;40m" + in + "\x1b[0m"
}
return in
}
func greenFgBlackBg(in string) string {
if alpmConf.Options&alpm.ConfColor > 0 {
2018-01-26 12:30:33 +01:00
return "\x1b[0;32;40m" + in + "\x1b[0m"
}
return in
}
func whiteFgBlackBg(in string) string {
if alpmConf.Options&alpm.ConfColor > 0 {
return "\x1b[0;37;40m" + in + "\x1b[0m"
}
return in
}
func boldRedFgBlackBg(in string) string {
if alpmConf.Options&alpm.ConfColor > 0 {
return "\x1b[1;31;40m" + in + "\x1b[0m"
}
return in
}
func boldYellowFgBlackBg(in string) string {
if alpmConf.Options&alpm.ConfColor > 0 {
return "\x1b[1;33;40m" + in + "\x1b[0m"
}
return in
}