print.go 9.2 KB

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