print.go 6.0 KB

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