123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667 |
- package main
- import (
- "bufio"
- "bytes"
- "encoding/xml"
- "fmt"
- "io/ioutil"
- "net/http"
- "os"
- "strconv"
- "strings"
- "time"
- "github.com/leonelquinteros/gotext"
- rpc "github.com/mikkeloscar/aur"
- "github.com/Jguer/yay/v9/pkg/intrange"
- "github.com/Jguer/yay/v9/pkg/stringset"
- "github.com/Jguer/yay/v9/pkg/text"
- )
- const arrow = "==>"
- const smallArrow = " ->"
- func (warnings *aurWarnings) print() {
- if len(warnings.Missing) > 0 {
- text.Warn(gotext.Get("Missing AUR Packages:"))
- for _, name := range warnings.Missing {
- fmt.Print(" " + cyan(name))
- }
- fmt.Println()
- }
- if len(warnings.Orphans) > 0 {
- text.Warn(gotext.Get("Orphaned AUR Packages:"))
- for _, name := range warnings.Orphans {
- fmt.Print(" " + cyan(name))
- }
- fmt.Println()
- }
- if len(warnings.OutOfDate) > 0 {
- text.Warn(gotext.Get("Flagged Out Of Date AUR Packages:"))
- for _, name := range warnings.OutOfDate {
- fmt.Print(" " + cyan(name))
- }
- fmt.Println()
- }
- }
- // human method 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 := range q {
- var toprint string
- if config.SearchMode == numberMenu {
- switch config.SortMode {
- case topDown:
- toprint += magenta(strconv.Itoa(start+i) + " ")
- case bottomUp:
- toprint += magenta(strconv.Itoa(len(q)+start-i-1) + " ")
- default:
- 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(colorHash("aur")) + "/" + bold(q[i].Name) +
- " " + cyan(q[i].Version) +
- bold(" (+"+strconv.Itoa(q[i].NumVotes)) +
- " " + bold(strconv.FormatFloat(q[i].Popularity, 'f', 2, 64)+"%) ")
- if q[i].Maintainer == "" {
- toprint += bold(red(gotext.Get("(Orphaned)"))) + " "
- }
- if q[i].OutOfDate != 0 {
- toprint += bold(red(gotext.Get("(Out-of-date: %s)", formatTime(q[i].OutOfDate)))) + " "
- }
- if pkg := localDB.Pkg(q[i].Name); pkg != nil {
- if pkg.Version() != q[i].Version {
- toprint += bold(green(gotext.Get("(Installed: %)", 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() {
- for i, res := range s {
- var toprint string
- if config.SearchMode == numberMenu {
- switch config.SortMode {
- case topDown:
- toprint += magenta(strconv.Itoa(i+1) + " ")
- case bottomUp:
- toprint += magenta(strconv.Itoa(len(s)-i) + " ")
- default:
- 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(colorHash(res.DB().Name())) + "/" + bold(res.Name()) +
- " " + cyan(res.Version()) +
- bold(" ("+human(res.Size())+
- " "+human(res.ISize())+") ")
- if len(res.Groups().Slice()) != 0 {
- toprint += fmt.Sprint(res.Groups().Slice(), " ")
- }
- localDB, err := alpmHandle.LocalDB()
- if err == nil {
- if pkg := localDB.Pkg(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.
- // Packages foo and bar from a pkgbase named base would print like so:
- // base (foo bar)
- func (b Base) String() string {
- pkg := b[0]
- str := pkg.PackageBase
- if len(b) > 1 || pkg.PackageBase != pkg.Name {
- str2 := " ("
- for _, split := range b {
- str2 += split.Name + " "
- }
- str2 = str2[:len(str2)-1] + ")"
- str += str2
- }
- return str
- }
- func (u upgrade) StylizedNameWithRepository() string {
- return bold(colorHash(u.Repository)) + "/" + bold(u.Name)
- }
- // Print prints the details of the packages to upgrade.
- func (u upSlice) print() {
- 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)
- }
- 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)
- fmt.Print(magenta(fmt.Sprintf(numberPadding, len(u)-k)))
- fmt.Printf(namePadding, i.StylizedNameWithRepository())
- fmt.Printf("%s -> %s\n", fmt.Sprintf(versionPadding, left), right)
- }
- }
- // Print prints repository packages to be downloaded
- func (do *depOrder) Print() {
- repo := ""
- repoMake := ""
- aur := ""
- aurMake := ""
- repoLen := 0
- repoMakeLen := 0
- aurLen := 0
- aurMakeLen := 0
- for _, pkg := range do.Repo {
- if do.Runtime.Get(pkg.Name()) {
- repo += " " + pkg.Name() + "-" + pkg.Version()
- repoLen++
- } else {
- repoMake += " " + pkg.Name() + "-" + pkg.Version()
- repoMakeLen++
- }
- }
- for _, base := range do.Aur {
- pkg := base.Pkgbase()
- pkgStr := " " + pkg + "-" + base[0].Version
- pkgStrMake := pkgStr
- push := false
- pushMake := false
- switch {
- case len(base) > 1, pkg != base[0].Name:
- pkgStr += " ("
- pkgStrMake += " ("
- for _, split := range base {
- if do.Runtime.Get(split.Name) {
- pkgStr += split.Name + " "
- aurLen++
- push = true
- } else {
- pkgStrMake += split.Name + " "
- aurMakeLen++
- pushMake = true
- }
- }
- pkgStr = pkgStr[:len(pkgStr)-1] + ")"
- pkgStrMake = pkgStrMake[:len(pkgStrMake)-1] + ")"
- case do.Runtime.Get(base[0].Name):
- aurLen++
- push = true
- default:
- aurMakeLen++
- pushMake = true
- }
- if push {
- aur += pkgStr
- }
- if pushMake {
- aurMake += pkgStrMake
- }
- }
- printDownloads("Repo", repoLen, repo)
- printDownloads("Repo Make", repoMakeLen, repoMake)
- printDownloads("Aur", aurLen, aur)
- printDownloads("Aur Make", aurMakeLen, aurMake)
- }
- func printDownloads(repoName string, length int, packages string) {
- if length < 1 {
- return
- }
- repoInfo := bold(blue(
- "[" + repoName + ": " + strconv.Itoa(length) + "]"))
- fmt.Println(repoInfo + cyan(packages))
- }
- // PrintInfo prints package info like pacman -Si.
- func PrintInfo(a *rpc.Pkg) {
- 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))
- text.PrintInfoValue(gotext.Get("First Submitted"), formatTimeQuery(a.FirstSubmitted))
- text.PrintInfoValue(gotext.Get("Last Modified"), formatTimeQuery(a.LastModified))
- if a.OutOfDate != 0 {
- text.PrintInfoValue(gotext.Get("Out-of-date"), formatTimeQuery(a.OutOfDate))
- } else {
- text.PrintInfoValue(gotext.Get("Out-of-date"), "No")
- }
- if cmdArgs.existsDouble("i") {
- 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)
- }
- 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.Printf("%s: %s\n", bold(pkgS[i].Name()), cyan(human(pkgS[i].ISize())))
- }
- // Could implement size here as well, but we just want the general idea
- }
- // localStatistics prints installed packages statistics.
- func localStatistics() error {
- info, err := statistics()
- if err != nil {
- return err
- }
- _, _, _, remoteNames, err := filterPackages()
- 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(human(info.TotalSize))))
- fmt.Println(bold(cyan("===========================================")))
- text.Infoln(gotext.Get("Ten biggest packages:"))
- biggestPackages()
- fmt.Println(bold(cyan("===========================================")))
- aurInfoPrint(remoteNames)
- return nil
- }
- //TODO: Make it less hacky
- func printNumberOfUpdates() error {
- warnings := makeWarnings()
- old := os.Stdout // keep backup of the real stdout
- os.Stdout = nil
- aurUp, repoUp, err := upList(warnings)
- 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(parser *arguments) error {
- targets := stringset.FromSlice(parser.targets)
- warnings := makeWarnings()
- old := os.Stdout // keep backup of the real stdout
- os.Stdout = nil
- _, _, localNames, remoteNames, err := filterPackages()
- if err != nil {
- return err
- }
- aurUp, repoUp, err := upList(warnings)
- os.Stdout = old // restoring the real stdout
- if err != nil {
- return err
- }
- noTargets := len(targets) == 0
- if !parser.existsArg("m", "foreign") {
- for _, pkg := range repoUp {
- if noTargets || targets.Get(pkg.Name) {
- if parser.existsArg("q", "quiet") {
- 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)
- }
- }
- }
- if !parser.existsArg("n", "native") {
- for _, pkg := range aurUp {
- if noTargets || targets.Get(pkg.Name) {
- if parser.existsArg("q", "quiet") {
- 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("")
- }
- return nil
- }
- type item struct {
- Title string `xml:"title"`
- Link string `xml:"link"`
- Description string `xml:"description"`
- PubDate string `xml:"pubDate"`
- Creator string `xml:"dc:creator"`
- }
- func (item *item) print(buildTime time.Time) {
- var fd string
- date, err := time.Parse(time.RFC1123Z, item.PubDate)
- if err != nil {
- fmt.Fprintln(os.Stderr, err)
- } else {
- fd = formatTime(int(date.Unix()))
- if _, double, _ := cmdArgs.getArg("news", "w"); !double && !buildTime.IsZero() {
- if buildTime.After(date) {
- return
- }
- }
- }
- fmt.Println(bold(magenta(fd)), bold(strings.TrimSpace(item.Title)))
- if !cmdArgs.existsArg("q", "quiet") {
- desc := strings.TrimSpace(parseNews(item.Description))
- fmt.Println(desc)
- }
- }
- type channel struct {
- Title string `xml:"title"`
- Link string `xml:"link"`
- Description string `xml:"description"`
- Language string `xml:"language"`
- Lastbuilddate string `xml:"lastbuilddate"`
- Items []item `xml:"item"`
- }
- type rss struct {
- Channel channel `xml:"channel"`
- }
- func printNewsFeed() error {
- resp, err := http.Get("https://archlinux.org/feeds/news")
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return err
- }
- rssGot := rss{}
- d := xml.NewDecoder(bytes.NewReader(body))
- err = d.Decode(&rssGot)
- if err != nil {
- return err
- }
- buildTime, err := lastBuildTime()
- if err != nil {
- return err
- }
- if config.SortMode == bottomUp {
- for i := len(rssGot.Channel.Items) - 1; i >= 0; i-- {
- rssGot.Channel.Items[i].print(buildTime)
- }
- } else {
- for i := 0; i < len(rssGot.Channel.Items); i++ {
- rssGot.Channel.Items[i].print(buildTime)
- }
- }
- return nil
- }
- // Formats a unix timestamp to ISO 8601 date (yyyy-mm-dd)
- func formatTime(i int) string {
- t := time.Unix(int64(i), 0)
- return t.Format("2006-01-02")
- }
- // Formats a unix timestamp to ISO 8601 date (Mon 02 Jan 2006 03:04:05 PM MST)
- func formatTimeQuery(i int) string {
- t := time.Unix(int64(i), 0)
- return t.Format("Mon 02 Jan 2006 03:04:05 PM MST")
- }
- const (
- redCode = "\x1b[31m"
- greenCode = "\x1b[32m"
- yellowCode = "\x1b[33m"
- blueCode = "\x1b[34m"
- magentaCode = "\x1b[35m"
- cyanCode = "\x1b[36m"
- boldCode = "\x1b[1m"
- resetCode = "\x1b[0m"
- )
- func stylize(startCode, in string) string {
- if 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 yellow(in string) string {
- return stylize(yellowCode, 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)
- }
- // Colors text using a hashing algorithm. The same text will always produce the
- // same color while different text will produce a different color.
- func colorHash(name string) (output string) {
- if !useColor {
- return name
- }
- var hash uint = 5381
- for i := 0; i < len(name); i++ {
- hash = uint(name[i]) + ((hash << 5) + (hash))
- }
- return fmt.Sprintf("\x1b[%dm%s\x1b[0m", hash%6+31, name)
- }
- func providerMenu(dep string, providers providers) *rpc.Pkg {
- size := providers.Len()
- str := bold(gotext.Get("There are %d providers available for %s:", size, dep))
- size = 1
- str += bold(cyan("\n:: ")) + bold(gotext.Get("Repository AUR")) + "\n "
- for _, pkg := range providers.Pkgs {
- str += fmt.Sprintf("%d) %s ", size, pkg.Name)
- size++
- }
- text.OperationInfoln(str)
- for {
- fmt.Print(gotext.Get("\nEnter a number (default=1): "))
- if config.NoConfirm {
- fmt.Println("1")
- return providers.Pkgs[0]
- }
- reader := bufio.NewReader(os.Stdin)
- numberBuf, overflow, err := reader.ReadLine()
- if err != nil {
- fmt.Fprintln(os.Stderr, err)
- break
- }
- if overflow {
- text.Errorln(gotext.Get("input too long"))
- continue
- }
- if string(numberBuf) == "" {
- return providers.Pkgs[0]
- }
- num, err := strconv.Atoi(string(numberBuf))
- if err != nil {
- text.Errorln(gotext.Get("invalid number: %s", string(numberBuf)))
- continue
- }
- if num < 1 || num >= size {
- text.Errorln(gotext.Get("invalid value: %d is not between %d and %d", num, 1, size-1))
- continue
- }
- return providers.Pkgs[num-1]
- }
- return nil
- }
|