print.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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"
  12. "github.com/Jguer/yay/v11/pkg/settings/parser"
  13. "github.com/Jguer/yay/v11/pkg/stringset"
  14. "github.com/Jguer/yay/v11/pkg/text"
  15. "github.com/Jguer/yay/v11/pkg/upgrade"
  16. )
  17. // PrintSearch handles printing search results in a given format.
  18. func (q aurQuery) printSearch(start int, dbExecutor db.Executor) {
  19. for i := range q {
  20. var toprint string
  21. if config.SearchMode == numberMenu {
  22. switch config.SortMode {
  23. case settings.TopDown:
  24. toprint += text.Magenta(strconv.Itoa(start+i) + " ")
  25. case settings.BottomUp:
  26. toprint += text.Magenta(strconv.Itoa(len(q)+start-i-1) + " ")
  27. default:
  28. text.Warnln(gotext.Get("invalid sort mode. Fix with yay -Y --bottomup --save"))
  29. }
  30. } else if config.SearchMode == minimal {
  31. fmt.Println(q[i].Name)
  32. continue
  33. }
  34. toprint += text.Bold(text.ColorHash("aur")) + "/" + text.Bold(q[i].Name) +
  35. " " + text.Cyan(q[i].Version) +
  36. text.Bold(" (+"+strconv.Itoa(q[i].NumVotes)) +
  37. " " + text.Bold(strconv.FormatFloat(q[i].Popularity, 'f', 2, 64)+") ")
  38. if q[i].Maintainer == "" {
  39. toprint += text.Bold(text.Red(gotext.Get("(Orphaned)"))) + " "
  40. }
  41. if q[i].OutOfDate != 0 {
  42. toprint += text.Bold(text.Red(gotext.Get("(Out-of-date: %s)", text.FormatTime(q[i].OutOfDate)))) + " "
  43. }
  44. if pkg := dbExecutor.LocalPackage(q[i].Name); pkg != nil {
  45. if pkg.Version() != q[i].Version {
  46. toprint += text.Bold(text.Green(gotext.Get("(Installed: %s)", pkg.Version())))
  47. } else {
  48. toprint += text.Bold(text.Green(gotext.Get("(Installed)")))
  49. }
  50. }
  51. toprint += "\n " + q[i].Description
  52. fmt.Println(toprint)
  53. }
  54. }
  55. // PrintSearch receives a RepoSearch type and outputs pretty text.
  56. func (s repoQuery) printSearch(dbExecutor db.Executor) {
  57. for i, res := range s {
  58. var toprint string
  59. if config.SearchMode == numberMenu {
  60. switch config.SortMode {
  61. case settings.TopDown:
  62. toprint += text.Magenta(strconv.Itoa(i+1) + " ")
  63. case settings.BottomUp:
  64. toprint += text.Magenta(strconv.Itoa(len(s)-i) + " ")
  65. default:
  66. text.Warnln(gotext.Get("invalid sort mode. Fix with yay -Y --bottomup --save"))
  67. }
  68. } else if config.SearchMode == minimal {
  69. fmt.Println(res.Name())
  70. continue
  71. }
  72. toprint += text.Bold(text.ColorHash(res.DB().Name())) + "/" + text.Bold(res.Name()) +
  73. " " + text.Cyan(res.Version()) +
  74. text.Bold(" ("+text.Human(res.Size())+
  75. " "+text.Human(res.ISize())+") ")
  76. packageGroups := dbExecutor.PackageGroups(res)
  77. if len(packageGroups) != 0 {
  78. toprint += fmt.Sprint(packageGroups, " ")
  79. }
  80. if pkg := dbExecutor.LocalPackage(res.Name()); pkg != nil {
  81. if pkg.Version() != res.Version() {
  82. toprint += text.Bold(text.Green(gotext.Get("(Installed: %s)", pkg.Version())))
  83. } else {
  84. toprint += text.Bold(text.Green(gotext.Get("(Installed)")))
  85. }
  86. }
  87. toprint += "\n " + res.Description()
  88. fmt.Println(toprint)
  89. }
  90. }
  91. // Pretty print a set of packages from the same package base.
  92. // PrintInfo prints package info like pacman -Si.
  93. func PrintInfo(a *aur.Pkg, extendedInfo bool) {
  94. text.PrintInfoValue(gotext.Get("Repository"), "aur")
  95. text.PrintInfoValue(gotext.Get("Name"), a.Name)
  96. text.PrintInfoValue(gotext.Get("Keywords"), a.Keywords...)
  97. text.PrintInfoValue(gotext.Get("Version"), a.Version)
  98. text.PrintInfoValue(gotext.Get("Description"), a.Description)
  99. text.PrintInfoValue(gotext.Get("URL"), a.URL)
  100. text.PrintInfoValue(gotext.Get("AUR URL"), config.AURURL+"/packages/"+a.Name)
  101. text.PrintInfoValue(gotext.Get("Groups"), a.Groups...)
  102. text.PrintInfoValue(gotext.Get("Licenses"), a.License...)
  103. text.PrintInfoValue(gotext.Get("Provides"), a.Provides...)
  104. text.PrintInfoValue(gotext.Get("Depends On"), a.Depends...)
  105. text.PrintInfoValue(gotext.Get("Make Deps"), a.MakeDepends...)
  106. text.PrintInfoValue(gotext.Get("Check Deps"), a.CheckDepends...)
  107. text.PrintInfoValue(gotext.Get("Optional Deps"), a.OptDepends...)
  108. text.PrintInfoValue(gotext.Get("Conflicts With"), a.Conflicts...)
  109. text.PrintInfoValue(gotext.Get("Maintainer"), a.Maintainer)
  110. text.PrintInfoValue(gotext.Get("Votes"), fmt.Sprintf("%d", a.NumVotes))
  111. text.PrintInfoValue(gotext.Get("Popularity"), fmt.Sprintf("%f", a.Popularity))
  112. text.PrintInfoValue(gotext.Get("First Submitted"), text.FormatTimeQuery(a.FirstSubmitted))
  113. text.PrintInfoValue(gotext.Get("Last Modified"), text.FormatTimeQuery(a.LastModified))
  114. if a.OutOfDate != 0 {
  115. text.PrintInfoValue(gotext.Get("Out-of-date"), text.FormatTimeQuery(a.OutOfDate))
  116. } else {
  117. text.PrintInfoValue(gotext.Get("Out-of-date"), "No")
  118. }
  119. if extendedInfo {
  120. text.PrintInfoValue("ID", fmt.Sprintf("%d", a.ID))
  121. text.PrintInfoValue(gotext.Get("Package Base ID"), fmt.Sprintf("%d", a.PackageBaseID))
  122. text.PrintInfoValue(gotext.Get("Package Base"), a.PackageBase)
  123. text.PrintInfoValue(gotext.Get("Snapshot URL"), config.AURURL+a.URLPath)
  124. }
  125. fmt.Println()
  126. }
  127. // BiggestPackages prints the name of the ten biggest packages in the system.
  128. func biggestPackages(dbExecutor db.Executor) {
  129. pkgS := dbExecutor.BiggestPackages()
  130. if len(pkgS) < 10 {
  131. return
  132. }
  133. for i := 0; i < 10; i++ {
  134. fmt.Printf("%s: %s\n", text.Bold(pkgS[i].Name()), text.Cyan(text.Human(pkgS[i].ISize())))
  135. }
  136. }
  137. // localStatistics prints installed packages statistics.
  138. func localStatistics(ctx context.Context, dbExecutor db.Executor) error {
  139. info := statistics(dbExecutor)
  140. _, remoteNames, err := query.GetPackageNamesBySource(dbExecutor)
  141. if err != nil {
  142. return err
  143. }
  144. text.Infoln(gotext.Get("Yay version v%s", yayVersion))
  145. fmt.Println(text.Bold(text.Cyan("===========================================")))
  146. text.Infoln(gotext.Get("Total installed packages: %s", text.Cyan(strconv.Itoa(info.Totaln))))
  147. text.Infoln(gotext.Get("Total foreign installed packages: %s", text.Cyan(strconv.Itoa(len(remoteNames)))))
  148. text.Infoln(gotext.Get("Explicitly installed packages: %s", text.Cyan(strconv.Itoa(info.Expln))))
  149. text.Infoln(gotext.Get("Total Size occupied by packages: %s", text.Cyan(text.Human(info.TotalSize))))
  150. fmt.Println(text.Bold(text.Cyan("===========================================")))
  151. text.Infoln(gotext.Get("Ten biggest packages:"))
  152. biggestPackages(dbExecutor)
  153. fmt.Println(text.Bold(text.Cyan("===========================================")))
  154. query.AURInfoPrint(ctx, config.Runtime.AURClient, remoteNames, config.RequestSplitN)
  155. return nil
  156. }
  157. func printNumberOfUpdates(ctx context.Context, dbExecutor db.Executor, enableDowngrade bool, filter upgrade.Filter) error {
  158. warnings := query.NewWarnings()
  159. old := os.Stdout // keep backup of the real stdout
  160. os.Stdout = nil
  161. aurUp, repoUp, err := upList(ctx, warnings, dbExecutor, enableDowngrade, filter)
  162. os.Stdout = old // restoring the real stdout
  163. if err != nil {
  164. return err
  165. }
  166. fmt.Println(len(aurUp.Up) + len(repoUp.Up))
  167. return nil
  168. }
  169. func printUpdateList(ctx context.Context, cmdArgs *parser.Arguments,
  170. dbExecutor db.Executor, enableDowngrade bool, filter upgrade.Filter) error {
  171. targets := stringset.FromSlice(cmdArgs.Targets)
  172. warnings := query.NewWarnings()
  173. old := os.Stdout // keep backup of the real stdout
  174. os.Stdout = nil
  175. localNames, remoteNames, err := query.GetPackageNamesBySource(dbExecutor)
  176. if err != nil {
  177. os.Stdout = old
  178. return err
  179. }
  180. aurUp, repoUp, err := upList(ctx, warnings, dbExecutor, enableDowngrade, filter)
  181. os.Stdout = old // restoring the real stdout
  182. if err != nil {
  183. return err
  184. }
  185. noTargets := len(targets) == 0
  186. if !cmdArgs.ExistsArg("m", "foreign") {
  187. for _, pkg := range repoUp.Up {
  188. if noTargets || targets.Get(pkg.Name) {
  189. if cmdArgs.ExistsArg("q", "quiet") {
  190. fmt.Printf("%s\n", pkg.Name)
  191. } else {
  192. fmt.Printf("%s %s -> %s\n", text.Bold(pkg.Name), text.Green(pkg.LocalVersion), text.Green(pkg.RemoteVersion))
  193. }
  194. delete(targets, pkg.Name)
  195. }
  196. }
  197. }
  198. if !cmdArgs.ExistsArg("n", "native") {
  199. for _, pkg := range aurUp.Up {
  200. if noTargets || targets.Get(pkg.Name) {
  201. if cmdArgs.ExistsArg("q", "quiet") {
  202. fmt.Printf("%s\n", pkg.Name)
  203. } else {
  204. fmt.Printf("%s %s -> %s\n", text.Bold(pkg.Name), text.Green(pkg.LocalVersion), text.Green(pkg.RemoteVersion))
  205. }
  206. delete(targets, pkg.Name)
  207. }
  208. }
  209. }
  210. missing := false
  211. outer:
  212. for pkg := range targets {
  213. for _, name := range localNames {
  214. if name == pkg {
  215. continue outer
  216. }
  217. }
  218. for _, name := range remoteNames {
  219. if name == pkg {
  220. continue outer
  221. }
  222. }
  223. text.Errorln(gotext.Get("package '%s' was not found", pkg))
  224. missing = true
  225. }
  226. if missing {
  227. return fmt.Errorf("")
  228. }
  229. return nil
  230. }