2017-08-02 17:24:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-02-19 17:36:33 +00:00
|
|
|
"os"
|
2018-01-26 01:18:49 +00:00
|
|
|
"strconv"
|
2018-01-07 17:36:31 +00:00
|
|
|
"strings"
|
2018-03-02 04:54:38 +00:00
|
|
|
"time"
|
2017-08-02 17:24:03 +00:00
|
|
|
|
2018-01-26 01:18:49 +00:00
|
|
|
alpm "github.com/jguer/go-alpm"
|
2017-08-02 17:24:03 +00:00
|
|
|
rpc "github.com/mikkeloscar/aur"
|
|
|
|
)
|
|
|
|
|
2018-01-26 01:18:49 +00:00
|
|
|
const arrow = "==>"
|
|
|
|
|
2018-03-03 02:45:16 +00:00
|
|
|
// human method returns results in human readable format.
|
2017-08-02 17:24:03 +00:00
|
|
|
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) {
|
2017-10-18 02:38:19 +00:00
|
|
|
localDb, _ := alpmHandle.LocalDb()
|
2017-08-02 17:24:03 +00:00
|
|
|
|
|
|
|
for i, res := range q {
|
|
|
|
var toprint string
|
2017-08-04 09:26:53 +00:00
|
|
|
if config.SearchMode == NumberMenu {
|
|
|
|
if config.SortMode == BottomUp {
|
2018-03-02 04:54:38 +00:00
|
|
|
toprint += magenta(strconv.Itoa(len(q)+start-i-1) + " ")
|
2017-08-02 17:24:03 +00:00
|
|
|
} else {
|
2018-03-02 04:54:38 +00:00
|
|
|
toprint += magenta(strconv.Itoa(start+i) + " ")
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
2017-08-04 09:26:53 +00:00
|
|
|
} else if config.SearchMode == Minimal {
|
2017-08-02 17:24:03 +00:00
|
|
|
fmt.Println(res.Name)
|
|
|
|
continue
|
|
|
|
}
|
2018-03-02 04:54:38 +00:00
|
|
|
|
|
|
|
toprint += bold(colourHash("aur")) + "/" + bold(res.Name) +
|
|
|
|
" " + cyan(res.Version) +
|
|
|
|
bold(" (+"+strconv.Itoa(res.NumVotes)) +
|
|
|
|
" " + bold(strconv.FormatFloat(res.Popularity, 'f', 2, 64)+"%) ")
|
2018-01-26 01:18:49 +00:00
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
if res.Maintainer == "" {
|
2018-03-02 04:54:38 +00:00
|
|
|
toprint += bold(red("(Orphaned)")) + " "
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if res.OutOfDate != 0 {
|
2018-03-02 04:54:38 +00:00
|
|
|
toprint += bold(red("(Out-of-date "+formatTime(res.OutOfDate)+")")) + " "
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := localDb.PkgByName(res.Name); err == nil {
|
2018-03-02 04:54:38 +00:00
|
|
|
toprint += bold(green("(Installed)"))
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
toprint += "\n " + res.Description
|
|
|
|
fmt.Println(toprint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 02:45:16 +00:00
|
|
|
// PrintSearch receives a RepoSearch type and outputs pretty text.
|
2017-08-02 17:24:03 +00:00
|
|
|
func (s repoQuery) printSearch() {
|
|
|
|
for i, res := range s {
|
|
|
|
var toprint string
|
|
|
|
if config.SearchMode == NumberMenu {
|
|
|
|
if config.SortMode == BottomUp {
|
2018-03-02 04:54:38 +00:00
|
|
|
toprint += magenta(strconv.Itoa(len(s)-i) + " ")
|
2017-08-02 17:24:03 +00:00
|
|
|
} else {
|
2018-03-02 04:54:38 +00:00
|
|
|
toprint += magenta(strconv.Itoa(i+1) + " ")
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
} else if config.SearchMode == Minimal {
|
|
|
|
fmt.Println(res.Name())
|
|
|
|
continue
|
|
|
|
}
|
2018-03-02 04:54:38 +00:00
|
|
|
|
|
|
|
toprint += bold(colourHash(res.DB().Name())) + "/" + bold(res.Name()) +
|
|
|
|
" " + cyan(res.Version()) +
|
|
|
|
bold(" ("+human(res.Size())+
|
|
|
|
" "+human(res.ISize())+") ")
|
2017-08-02 17:24:03 +00:00
|
|
|
|
|
|
|
if len(res.Groups().Slice()) != 0 {
|
|
|
|
toprint += fmt.Sprint(res.Groups().Slice(), " ")
|
|
|
|
}
|
|
|
|
|
2017-10-18 02:38:19 +00:00
|
|
|
localDb, err := alpmHandle.LocalDb()
|
2017-08-02 17:24:03 +00:00
|
|
|
if err == nil {
|
|
|
|
if _, err = localDb.PkgByName(res.Name()); err == nil {
|
2018-03-02 04:54:38 +00:00
|
|
|
toprint += bold(green("(Installed)"))
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toprint += "\n " + res.Description()
|
|
|
|
fmt.Println(toprint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 02:45:16 +00:00
|
|
|
// Pretty print a set of packages from the same package base.
|
|
|
|
// Packages foo and bar from a pkgbase named base would print like so:
|
|
|
|
// base (foo bar)
|
2018-02-20 01:23:16 +00:00
|
|
|
func formatPkgbase(pkg *rpc.Pkg, bases map[string][]*rpc.Pkg) string {
|
|
|
|
str := pkg.PackageBase
|
|
|
|
if len(bases[pkg.PackageBase]) > 1 || pkg.PackageBase != pkg.Name {
|
|
|
|
str2 := " ("
|
|
|
|
for _, split := range bases[pkg.PackageBase] {
|
|
|
|
str2 += split.Name + " "
|
|
|
|
}
|
|
|
|
str2 = str2[:len(str2)-1] + ")"
|
|
|
|
|
|
|
|
str += str2
|
|
|
|
}
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2018-02-05 20:05:58 +00:00
|
|
|
// printDownloadsFromRepo prints repository packages to be downloaded
|
2018-02-15 21:23:34 +00:00
|
|
|
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 20:05:58 +00:00
|
|
|
}
|
2018-02-15 21:23:34 +00: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 20:05:58 +00:00
|
|
|
}
|
2018-02-15 21:23:34 +00:00
|
|
|
|
2018-02-16 17:18:59 +00:00
|
|
|
printDownloads("Repo", repoLen, repo)
|
|
|
|
printDownloads("Repo Make", repoMakeLen, repoMake)
|
|
|
|
printDownloads("Aur", aurLen, aur)
|
|
|
|
printDownloads("Aur Make", aurMakeLen, aurMake)
|
2018-02-05 20:05:58 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 21:23:34 +00:00
|
|
|
func printDownloads(repoName string, length int, packages string) {
|
|
|
|
if length < 1 {
|
|
|
|
return
|
2018-02-05 20:05:58 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 04:03:09 +00:00
|
|
|
repoInfo := bold(blue(
|
|
|
|
"[" + repoName + ": " + strconv.Itoa(length) + "]"))
|
2018-03-02 04:54:38 +00:00
|
|
|
fmt.Println(repoInfo + magenta(packages))
|
2018-02-05 20:05:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
// PrintInfo prints package info like pacman -Si.
|
|
|
|
func PrintInfo(a *rpc.Pkg) {
|
2018-03-02 04:54:38 +00:00
|
|
|
fmt.Println(bold("Repository :"), "aur")
|
|
|
|
fmt.Println(bold("Name :"), a.Name)
|
|
|
|
fmt.Println(bold("Version :"), a.Version)
|
|
|
|
fmt.Println(bold("Description :"), a.Description)
|
|
|
|
fmt.Println(bold("URL :"), a.URL)
|
|
|
|
fmt.Println(bold("Licenses :"), strings.Join(a.License, " "))
|
|
|
|
fmt.Println(bold("Depends On :"), strings.Join(a.Depends, " "))
|
|
|
|
fmt.Println(bold("Make Deps :"), strings.Join(a.MakeDepends, " "))
|
|
|
|
fmt.Println(bold("Check Deps :"), strings.Join(a.CheckDepends, " "))
|
|
|
|
fmt.Println(bold("Optional Deps :"), strings.Join(a.OptDepends, " "))
|
|
|
|
fmt.Println(bold("Conflicts With :"), strings.Join(a.Conflicts, " "))
|
|
|
|
fmt.Println(bold("Maintainer :"), a.Maintainer)
|
|
|
|
fmt.Println(bold("Votes :"), a.NumVotes)
|
|
|
|
fmt.Println(bold("Popularity :"), a.Popularity)
|
2017-08-02 17:24:03 +00:00
|
|
|
if a.OutOfDate != 0 {
|
2018-03-02 04:54:38 +00:00
|
|
|
fmt.Println(bold("Out-of-date :"), "Yes", "["+formatTime(a.OutOfDate)+"]")
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
2018-01-07 17:36:31 +00:00
|
|
|
|
|
|
|
fmt.Println()
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BiggestPackages prints the name of the ten biggest packages in the system.
|
|
|
|
func biggestPackages() {
|
2017-10-18 02:38:19 +00:00
|
|
|
localDb, err := alpmHandle.LocalDb()
|
2017-08-02 17:24:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pkgCache := localDb.PkgCache()
|
|
|
|
pkgS := pkgCache.SortBySize().Slice()
|
|
|
|
|
|
|
|
if len(pkgS) < 10 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
2018-03-02 04:54:38 +00:00
|
|
|
fmt.Println(bold(pkgS[i].Name()) + ": " + cyan(human(pkgS[i].ISize())))
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
// Could implement size here as well, but we just want the general idea
|
|
|
|
}
|
2017-10-19 05:59:26 +00: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
|
|
|
|
}
|
|
|
|
|
2018-03-02 04:54:38 +00:00
|
|
|
fmt.Printf(bold("Yay version v%s\n"), version)
|
2018-03-02 04:03:09 +00:00
|
|
|
fmt.Println(bold(cyan("===========================================")))
|
2018-03-02 04:54:38 +00:00
|
|
|
fmt.Println(bold(green("Total installed packages: ")) + magenta(strconv.Itoa(info.Totaln)))
|
|
|
|
fmt.Println(bold(green("Total foreign installed packages: ")) + magenta(strconv.Itoa(len(remoteNames))))
|
|
|
|
fmt.Println(bold(green("Explicitly installed packages: ")) + magenta(strconv.Itoa(info.Expln)))
|
|
|
|
fmt.Println(bold(green("Total Size occupied by packages: ")) + magenta(human(info.TotalSize)))
|
2018-03-02 04:03:09 +00:00
|
|
|
fmt.Println(bold(cyan("===========================================")))
|
2018-03-02 04:54:38 +00:00
|
|
|
fmt.Println(bold(green("Ten biggest packages:")))
|
2017-10-19 05:59:26 +00:00
|
|
|
biggestPackages()
|
2018-03-02 04:03:09 +00:00
|
|
|
fmt.Println(bold(cyan("===========================================")))
|
2017-10-19 05:59:26 +00:00
|
|
|
|
2018-02-17 18:08:00 +00:00
|
|
|
aurInfo(remoteNames)
|
2017-10-19 05:59:26 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-17 21:48:23 +00:00
|
|
|
|
2018-03-03 02:45:16 +00:00
|
|
|
//TODO: Make it less hacky
|
2018-01-25 20:39:26 +00:00
|
|
|
func printNumberOfUpdates() error {
|
2018-02-18 23:46:25 +00:00
|
|
|
//todo
|
2018-02-19 17:36:33 +00:00
|
|
|
old := os.Stdout // keep backup of the real stdout
|
2018-01-25 20:39:26 +00:00
|
|
|
os.Stdout = nil
|
2018-02-19 17:36:33 +00:00
|
|
|
_, _, localNames, remoteNames, err := filterPackages()
|
|
|
|
dt, _ := getDepTree(append(localNames, remoteNames...))
|
|
|
|
aurUp, repoUp, err := upList(dt)
|
2018-01-25 20:39:26 +00:00
|
|
|
os.Stdout = old // restoring the real stdout
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(len(aurUp) + len(repoUp))
|
2018-02-21 08:41:25 +00:00
|
|
|
|
2018-01-25 20:39:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-03 02:45:16 +00:00
|
|
|
//TODO: Make it less hacky
|
2018-01-25 20:39:26 +00:00
|
|
|
func printUpdateList() error {
|
2018-03-03 02:45:16 +00:00
|
|
|
old := os.Stdout // Keep backup of the real stdout
|
2018-01-25 20:39:26 +00:00
|
|
|
os.Stdout = nil
|
2018-02-19 17:36:33 +00:00
|
|
|
_, _, localNames, remoteNames, err := filterPackages()
|
|
|
|
dt, _ := getDepTree(append(localNames, remoteNames...))
|
|
|
|
aurUp, repoUp, err := upList(dt)
|
|
|
|
|
2018-03-03 02:45:16 +00:00
|
|
|
os.Stdout = old // Restoring the real stdout
|
2018-01-25 20:39:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, pkg := range repoUp {
|
|
|
|
fmt.Println(pkg.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pkg := range aurUp {
|
|
|
|
fmt.Println(pkg.Name)
|
|
|
|
}
|
2018-02-19 17:36:33 +00:00
|
|
|
|
2018-01-25 20:39:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-01-26 01:18:49 +00:00
|
|
|
|
2018-03-03 02:45:16 +00:00
|
|
|
// Formats a unix timestamp to yyyy/mm/dd
|
2018-03-02 04:54:38 +00:00
|
|
|
func formatTime(i int) string {
|
|
|
|
t := time.Unix(int64(i), 0)
|
2018-03-03 02:45:16 +00:00
|
|
|
return fmt.Sprintf("%d/%02d/%02d", t.Year(), int(t.Month()), t.Day())
|
2018-03-02 04:54:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 04:03:09 +00:00
|
|
|
func red(in string) string {
|
2018-01-26 01:18:49 +00:00
|
|
|
if alpmConf.Options&alpm.ConfColor > 0 {
|
2018-03-02 04:03:09 +00:00
|
|
|
return "\x1b[31m" + in + "\x1b[0m"
|
2018-01-26 01:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
|
2018-03-02 04:03:09 +00:00
|
|
|
func green(in string) string {
|
2018-01-26 01:18:49 +00:00
|
|
|
if alpmConf.Options&alpm.ConfColor > 0 {
|
2018-03-02 04:03:09 +00:00
|
|
|
return "\x1b[32m" + in + "\x1b[0m"
|
2018-01-26 01:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
|
2018-03-02 04:03:09 +00:00
|
|
|
func yellow(in string) string {
|
2018-01-26 11:30:33 +00:00
|
|
|
if alpmConf.Options&alpm.ConfColor > 0 {
|
2018-03-02 04:03:09 +00:00
|
|
|
return "\x1b[33m" + in + "\x1b[0m"
|
2018-01-26 11:30:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
|
2018-03-02 04:03:09 +00:00
|
|
|
func blue(in string) string {
|
2018-01-26 01:18:49 +00:00
|
|
|
if alpmConf.Options&alpm.ConfColor > 0 {
|
2018-03-02 04:03:09 +00:00
|
|
|
return "\x1b[34m" + in + "\x1b[0m"
|
2018-01-26 01:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
|
2018-03-02 04:03:09 +00:00
|
|
|
func cyan(in string) string {
|
2018-01-26 01:18:49 +00:00
|
|
|
if alpmConf.Options&alpm.ConfColor > 0 {
|
2018-03-02 04:03:09 +00:00
|
|
|
return "\x1b[36m" + in + "\x1b[0m"
|
2018-01-26 01:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
|
2018-03-02 04:54:38 +00:00
|
|
|
func magenta(in string) string {
|
2018-01-26 01:18:49 +00:00
|
|
|
if alpmConf.Options&alpm.ConfColor > 0 {
|
2018-03-02 04:54:38 +00:00
|
|
|
return "\x1b[35m" + in + "\x1b[0m"
|
2018-01-26 01:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
|
2018-03-02 04:03:09 +00:00
|
|
|
func bold(in string) string {
|
2018-01-26 01:18:49 +00:00
|
|
|
if alpmConf.Options&alpm.ConfColor > 0 {
|
2018-03-02 04:03:09 +00:00
|
|
|
return "\x1b[1m" + in + "\x1b[0m"
|
2018-01-26 01:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return in
|
|
|
|
}
|
2018-03-02 01:02:56 +00:00
|
|
|
|
2018-03-03 02:45:16 +00:00
|
|
|
// Colours text using a hashing algorithm. The same text will always produce the
|
|
|
|
// same colour while different text will produce a different colour.
|
2018-03-02 01:02:56 +00:00
|
|
|
func colourHash(name string) (output string) {
|
|
|
|
if alpmConf.Options&alpm.ConfColor == 0 {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
var hash = 5381
|
|
|
|
for i := 0; i < len(name); i++ {
|
|
|
|
hash = int(name[i]) + ((hash << 5) + (hash))
|
|
|
|
}
|
2018-03-02 04:03:09 +00:00
|
|
|
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", hash%6+31, name)
|
2018-03-02 01:02:56 +00:00
|
|
|
}
|