print.go 9.0 KB

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