print.go 5.8 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, err := query.GetPackageNamesBySource(dbExecutor)
  65. if err != nil {
  66. return err
  67. }
  68. text.Infoln(gotext.Get("Yay version v%s", yayVersion))
  69. fmt.Println(text.Bold(text.Cyan("===========================================")))
  70. text.Infoln(gotext.Get("Total installed packages: %s", text.Cyan(strconv.Itoa(info.Totaln))))
  71. text.Infoln(gotext.Get("Total foreign installed packages: %s", text.Cyan(strconv.Itoa(len(remoteNames)))))
  72. text.Infoln(gotext.Get("Explicitly installed packages: %s", text.Cyan(strconv.Itoa(info.Expln))))
  73. text.Infoln(gotext.Get("Total Size occupied by packages: %s", text.Cyan(text.Human(info.TotalSize))))
  74. fmt.Println(text.Bold(text.Cyan("===========================================")))
  75. text.Infoln(gotext.Get("Ten biggest packages:"))
  76. biggestPackages(dbExecutor)
  77. fmt.Println(text.Bold(text.Cyan("===========================================")))
  78. query.AURInfoPrint(ctx, config.Runtime.AURClient, remoteNames, config.RequestSplitN)
  79. return nil
  80. }
  81. func printNumberOfUpdates(ctx context.Context, dbExecutor db.Executor, enableDowngrade bool, filter upgrade.Filter) error {
  82. warnings := query.NewWarnings()
  83. old := os.Stdout // keep backup of the real stdout
  84. os.Stdout = nil
  85. aurUp, repoUp, err := upList(ctx, warnings, dbExecutor, enableDowngrade, filter)
  86. os.Stdout = old // restoring the real stdout
  87. if err != nil {
  88. return err
  89. }
  90. fmt.Println(len(aurUp.Up) + len(repoUp.Up))
  91. return nil
  92. }
  93. func printUpdateList(ctx context.Context, cmdArgs *parser.Arguments,
  94. dbExecutor db.Executor, enableDowngrade bool, filter upgrade.Filter) error {
  95. targets := stringset.FromSlice(cmdArgs.Targets)
  96. warnings := query.NewWarnings()
  97. old := os.Stdout // keep backup of the real stdout
  98. os.Stdout = nil
  99. localNames, remoteNames, err := query.GetPackageNamesBySource(dbExecutor)
  100. if err != nil {
  101. os.Stdout = old
  102. return err
  103. }
  104. aurUp, repoUp, err := upList(ctx, warnings, dbExecutor, enableDowngrade, filter)
  105. os.Stdout = old // restoring the real stdout
  106. if err != nil {
  107. return err
  108. }
  109. noTargets := len(targets) == 0
  110. if !cmdArgs.ExistsArg("m", "foreign") {
  111. for _, pkg := range repoUp.Up {
  112. if noTargets || targets.Get(pkg.Name) {
  113. if cmdArgs.ExistsArg("q", "quiet") {
  114. fmt.Printf("%s\n", pkg.Name)
  115. } else {
  116. fmt.Printf("%s %s -> %s\n", text.Bold(pkg.Name), text.Green(pkg.LocalVersion), text.Green(pkg.RemoteVersion))
  117. }
  118. delete(targets, pkg.Name)
  119. }
  120. }
  121. }
  122. if !cmdArgs.ExistsArg("n", "native") {
  123. for _, pkg := range aurUp.Up {
  124. if noTargets || targets.Get(pkg.Name) {
  125. if cmdArgs.ExistsArg("q", "quiet") {
  126. fmt.Printf("%s\n", pkg.Name)
  127. } else {
  128. fmt.Printf("%s %s -> %s\n", text.Bold(pkg.Name), text.Green(pkg.LocalVersion), text.Green(pkg.RemoteVersion))
  129. }
  130. delete(targets, pkg.Name)
  131. }
  132. }
  133. }
  134. missing := false
  135. outer:
  136. for pkg := range targets {
  137. for _, name := range localNames {
  138. if name == pkg {
  139. continue outer
  140. }
  141. }
  142. for _, name := range remoteNames {
  143. if name == pkg {
  144. continue outer
  145. }
  146. }
  147. text.Errorln(gotext.Get("package '%s' was not found", pkg))
  148. missing = true
  149. }
  150. if missing {
  151. return fmt.Errorf("")
  152. }
  153. return nil
  154. }