print.go 9.2 KB

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