print.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. aur "github.com/Jguer/aur"
  8. "github.com/leonelquinteros/gotext"
  9. "github.com/Jguer/yay/v12/pkg/db"
  10. "github.com/Jguer/yay/v12/pkg/query"
  11. "github.com/Jguer/yay/v12/pkg/settings"
  12. "github.com/Jguer/yay/v12/pkg/settings/parser"
  13. "github.com/Jguer/yay/v12/pkg/stringset"
  14. "github.com/Jguer/yay/v12/pkg/text"
  15. "github.com/Jguer/yay/v12/pkg/upgrade"
  16. )
  17. // PrintInfo prints package info like pacman -Si.
  18. func PrintInfo(config *settings.Configuration, a *aur.Pkg, extendedInfo bool) {
  19. text.PrintInfoValue(gotext.Get("Repository"), "aur")
  20. text.PrintInfoValue(gotext.Get("Name"), a.Name)
  21. text.PrintInfoValue(gotext.Get("Keywords"), a.Keywords...)
  22. text.PrintInfoValue(gotext.Get("Version"), a.Version)
  23. text.PrintInfoValue(gotext.Get("Description"), a.Description)
  24. text.PrintInfoValue(gotext.Get("URL"), a.URL)
  25. text.PrintInfoValue(gotext.Get("AUR URL"), config.AURURL+"/packages/"+a.Name)
  26. text.PrintInfoValue(gotext.Get("Groups"), a.Groups...)
  27. text.PrintInfoValue(gotext.Get("Licenses"), a.License...)
  28. text.PrintInfoValue(gotext.Get("Provides"), a.Provides...)
  29. text.PrintInfoValue(gotext.Get("Depends On"), a.Depends...)
  30. text.PrintInfoValue(gotext.Get("Make Deps"), a.MakeDepends...)
  31. text.PrintInfoValue(gotext.Get("Check Deps"), a.CheckDepends...)
  32. text.PrintInfoValue(gotext.Get("Optional Deps"), a.OptDepends...)
  33. text.PrintInfoValue(gotext.Get("Conflicts With"), a.Conflicts...)
  34. text.PrintInfoValue(gotext.Get("Maintainer"), a.Maintainer)
  35. text.PrintInfoValue(gotext.Get("Votes"), fmt.Sprintf("%d", a.NumVotes))
  36. text.PrintInfoValue(gotext.Get("Popularity"), fmt.Sprintf("%f", a.Popularity))
  37. text.PrintInfoValue(gotext.Get("First Submitted"), text.FormatTimeQuery(a.FirstSubmitted))
  38. text.PrintInfoValue(gotext.Get("Last Modified"), text.FormatTimeQuery(a.LastModified))
  39. if a.OutOfDate != 0 {
  40. text.PrintInfoValue(gotext.Get("Out-of-date"), text.FormatTimeQuery(a.OutOfDate))
  41. } else {
  42. text.PrintInfoValue(gotext.Get("Out-of-date"), "No")
  43. }
  44. if extendedInfo {
  45. text.PrintInfoValue("ID", fmt.Sprintf("%d", a.ID))
  46. text.PrintInfoValue(gotext.Get("Package Base ID"), fmt.Sprintf("%d", a.PackageBaseID))
  47. text.PrintInfoValue(gotext.Get("Package Base"), a.PackageBase)
  48. text.PrintInfoValue(gotext.Get("Snapshot URL"), config.AURURL+a.URLPath)
  49. }
  50. fmt.Println()
  51. }
  52. // BiggestPackages prints the name of the ten biggest packages in the system.
  53. func biggestPackages(dbExecutor db.Executor) {
  54. pkgS := dbExecutor.BiggestPackages()
  55. if len(pkgS) < 10 {
  56. return
  57. }
  58. for i := 0; i < 10; i++ {
  59. fmt.Printf("%s: %s\n", text.Bold(pkgS[i].Name()), text.Cyan(text.Human(pkgS[i].ISize())))
  60. }
  61. }
  62. // localStatistics prints installed packages statistics.
  63. func localStatistics(ctx context.Context, cfg *settings.Configuration, dbExecutor db.Executor) error {
  64. info := statistics(cfg, dbExecutor)
  65. remoteNames := dbExecutor.InstalledRemotePackageNames()
  66. text.Infoln(gotext.Get("Yay version v%s", yayVersion))
  67. fmt.Println(text.Bold(text.Cyan("===========================================")))
  68. text.Infoln(gotext.Get("Total installed packages: %s", text.Cyan(strconv.Itoa(info.Totaln))))
  69. text.Infoln(gotext.Get("Foreign installed packages: %s", text.Cyan(strconv.Itoa(len(remoteNames)))))
  70. text.Infoln(gotext.Get("Explicitly installed packages: %s", text.Cyan(strconv.Itoa(info.Expln))))
  71. text.Infoln(gotext.Get("Total Size occupied by packages: %s", text.Cyan(text.Human(info.TotalSize))))
  72. for path, size := range info.pacmanCaches {
  73. text.Infoln(gotext.Get("Size of pacman cache %s: %s", path, text.Cyan(text.Human(size))))
  74. }
  75. text.Infoln(gotext.Get("Size of yay cache %s: %s", cfg.BuildDir, text.Cyan(text.Human(info.yayCache))))
  76. fmt.Println(text.Bold(text.Cyan("===========================================")))
  77. text.Infoln(gotext.Get("Ten biggest packages:"))
  78. biggestPackages(dbExecutor)
  79. fmt.Println(text.Bold(text.Cyan("===========================================")))
  80. query.AURInfoPrint(ctx, cfg.Runtime.AURClient, remoteNames, cfg.RequestSplitN)
  81. return nil
  82. }
  83. func printUpdateList(ctx context.Context, cfg *settings.Configuration, cmdArgs *parser.Arguments,
  84. dbExecutor db.Executor, enableDowngrade bool, filter upgrade.Filter,
  85. ) error {
  86. targets := stringset.FromSlice(cmdArgs.Targets)
  87. warnings := query.NewWarnings()
  88. old := os.Stdout // keep backup of the real stdout
  89. os.Stdout = nil
  90. remoteNames := dbExecutor.InstalledRemotePackageNames()
  91. localNames := dbExecutor.InstalledSyncPackageNames()
  92. aurUp, repoUp, err := upList(ctx, cfg, warnings, dbExecutor, enableDowngrade, filter)
  93. os.Stdout = old // restoring the real stdout
  94. if err != nil {
  95. return err
  96. }
  97. if (len(aurUp.Up) + len(repoUp.Up)) == 0 {
  98. return fmt.Errorf("")
  99. }
  100. noTargets := len(targets) == 0
  101. if !cmdArgs.ExistsArg("m", "foreign") {
  102. for _, pkg := range repoUp.Up {
  103. if noTargets || targets.Get(pkg.Name) {
  104. if cmdArgs.ExistsArg("q", "quiet") {
  105. fmt.Printf("%s\n", pkg.Name)
  106. } else {
  107. fmt.Printf("%s %s -> %s\n", text.Bold(pkg.Name), text.Green(pkg.LocalVersion), text.Green(pkg.RemoteVersion))
  108. }
  109. delete(targets, pkg.Name)
  110. }
  111. }
  112. }
  113. if !cmdArgs.ExistsArg("n", "native") {
  114. for _, pkg := range aurUp.Up {
  115. if noTargets || targets.Get(pkg.Name) {
  116. if cmdArgs.ExistsArg("q", "quiet") {
  117. fmt.Printf("%s\n", pkg.Name)
  118. } else {
  119. fmt.Printf("%s %s -> %s\n", text.Bold(pkg.Name), text.Green(pkg.LocalVersion), text.Green(pkg.RemoteVersion))
  120. }
  121. delete(targets, pkg.Name)
  122. }
  123. }
  124. }
  125. missing := false
  126. outer:
  127. for pkg := range targets {
  128. for _, name := range localNames {
  129. if name == pkg {
  130. continue outer
  131. }
  132. }
  133. for _, name := range remoteNames {
  134. if name == pkg {
  135. continue outer
  136. }
  137. }
  138. text.Errorln(gotext.Get("package '%s' was not found", pkg))
  139. missing = true
  140. }
  141. if missing {
  142. return fmt.Errorf("")
  143. }
  144. return nil
  145. }