print.go 8.3 KB

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