print.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 printNumberOfUpdates(ctx context.Context, cfg *settings.Configuration,
  84. dbExecutor db.Executor, enableDowngrade bool, filter upgrade.Filter,
  85. ) error {
  86. warnings := query.NewWarnings()
  87. old := os.Stdout // keep backup of the real stdout
  88. os.Stdout = nil
  89. aurUp, repoUp, err := upList(ctx, cfg, warnings, dbExecutor, enableDowngrade, filter)
  90. os.Stdout = old // restoring the real stdout
  91. if err != nil {
  92. return err
  93. }
  94. fmt.Println(len(aurUp.Up) + len(repoUp.Up))
  95. return nil
  96. }
  97. func printUpdateList(ctx context.Context, cfg *settings.Configuration, cmdArgs *parser.Arguments,
  98. dbExecutor db.Executor, enableDowngrade bool, filter upgrade.Filter,
  99. ) error {
  100. targets := stringset.FromSlice(cmdArgs.Targets)
  101. warnings := query.NewWarnings()
  102. old := os.Stdout // keep backup of the real stdout
  103. os.Stdout = nil
  104. remoteNames := dbExecutor.InstalledRemotePackageNames()
  105. localNames := dbExecutor.InstalledSyncPackageNames()
  106. aurUp, repoUp, err := upList(ctx, cfg, warnings, dbExecutor, enableDowngrade, filter)
  107. os.Stdout = old // restoring the real stdout
  108. if err != nil {
  109. return err
  110. }
  111. if (len(aurUp.Up) + len(repoUp.Up)) == 0 {
  112. return fmt.Errorf("")
  113. }
  114. noTargets := len(targets) == 0
  115. if !cmdArgs.ExistsArg("m", "foreign") {
  116. for _, pkg := range repoUp.Up {
  117. if noTargets || targets.Get(pkg.Name) {
  118. if cmdArgs.ExistsArg("q", "quiet") {
  119. fmt.Printf("%s\n", pkg.Name)
  120. } else {
  121. fmt.Printf("%s %s -> %s\n", text.Bold(pkg.Name), text.Green(pkg.LocalVersion), text.Green(pkg.RemoteVersion))
  122. }
  123. delete(targets, pkg.Name)
  124. }
  125. }
  126. }
  127. if !cmdArgs.ExistsArg("n", "native") {
  128. for _, pkg := range aurUp.Up {
  129. if noTargets || targets.Get(pkg.Name) {
  130. if cmdArgs.ExistsArg("q", "quiet") {
  131. fmt.Printf("%s\n", pkg.Name)
  132. } else {
  133. fmt.Printf("%s %s -> %s\n", text.Bold(pkg.Name), text.Green(pkg.LocalVersion), text.Green(pkg.RemoteVersion))
  134. }
  135. delete(targets, pkg.Name)
  136. }
  137. }
  138. }
  139. missing := false
  140. outer:
  141. for pkg := range targets {
  142. for _, name := range localNames {
  143. if name == pkg {
  144. continue outer
  145. }
  146. }
  147. for _, name := range remoteNames {
  148. if name == pkg {
  149. continue outer
  150. }
  151. }
  152. text.Errorln(gotext.Get("package '%s' was not found", pkg))
  153. missing = true
  154. }
  155. if missing {
  156. return fmt.Errorf("")
  157. }
  158. return nil
  159. }