print.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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/intrange"
  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. )
  17. // PrintSearch handles printing search results in a given format
  18. func (q aurQuery) printSearch(start int, dbExecutor *db.AlpmExecutor) {
  19. for i := range q {
  20. var toprint string
  21. if config.SearchMode == numberMenu {
  22. switch config.SortMode {
  23. case settings.TopDown:
  24. toprint += magenta(strconv.Itoa(start+i) + " ")
  25. case settings.BottomUp:
  26. toprint += 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 += bold(text.ColorHash("aur")) + "/" + bold(q[i].Name) +
  35. " " + cyan(q[i].Version) +
  36. bold(" (+"+strconv.Itoa(q[i].NumVotes)) +
  37. " " + bold(strconv.FormatFloat(q[i].Popularity, 'f', 2, 64)+") ")
  38. if q[i].Maintainer == "" {
  39. toprint += bold(red(gotext.Get("(Orphaned)"))) + " "
  40. }
  41. if q[i].OutOfDate != 0 {
  42. toprint += bold(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 += bold(green(gotext.Get("(Installed: %s)", pkg.Version())))
  47. } else {
  48. toprint += bold(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.AlpmExecutor) {
  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 += magenta(strconv.Itoa(i+1) + " ")
  63. case settings.BottomUp:
  64. toprint += 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 += bold(text.ColorHash(res.DB().Name())) + "/" + bold(res.Name()) +
  73. " " + cyan(res.Version()) +
  74. 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 += bold(green(gotext.Get("(Installed: %s)", pkg.Version())))
  83. } else {
  84. toprint += bold(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. func (u *upgrade) StylizedNameWithRepository() string {
  93. return bold(text.ColorHash(u.Repository)) + "/" + bold(u.Name)
  94. }
  95. // Print prints the details of the packages to upgrade.
  96. func (u upSlice) print() {
  97. longestName, longestVersion := 0, 0
  98. for _, pack := range u {
  99. packNameLen := len(pack.StylizedNameWithRepository())
  100. packVersion, _ := getVersionDiff(pack.LocalVersion, pack.RemoteVersion)
  101. packVersionLen := len(packVersion)
  102. longestName = intrange.Max(packNameLen, longestName)
  103. longestVersion = intrange.Max(packVersionLen, longestVersion)
  104. }
  105. namePadding := fmt.Sprintf("%%-%ds ", longestName)
  106. versionPadding := fmt.Sprintf("%%-%ds", longestVersion)
  107. numberPadding := fmt.Sprintf("%%%dd ", len(fmt.Sprintf("%v", len(u))))
  108. for k, i := range u {
  109. left, right := getVersionDiff(i.LocalVersion, i.RemoteVersion)
  110. fmt.Print(magenta(fmt.Sprintf(numberPadding, len(u)-k)))
  111. fmt.Printf(namePadding, i.StylizedNameWithRepository())
  112. fmt.Printf("%s -> %s\n", fmt.Sprintf(versionPadding, left), right)
  113. }
  114. }
  115. // PrintInfo prints package info like pacman -Si.
  116. func PrintInfo(a *rpc.Pkg, extendedInfo bool) {
  117. text.PrintInfoValue(gotext.Get("Repository"), "aur")
  118. text.PrintInfoValue(gotext.Get("Name"), a.Name)
  119. text.PrintInfoValue(gotext.Get("Keywords"), strings.Join(a.Keywords, " "))
  120. text.PrintInfoValue(gotext.Get("Version"), a.Version)
  121. text.PrintInfoValue(gotext.Get("Description"), a.Description)
  122. text.PrintInfoValue(gotext.Get("URL"), a.URL)
  123. text.PrintInfoValue(gotext.Get("AUR URL"), config.AURURL+"/packages/"+a.Name)
  124. text.PrintInfoValue(gotext.Get("Groups"), strings.Join(a.Groups, " "))
  125. text.PrintInfoValue(gotext.Get("Licenses"), strings.Join(a.License, " "))
  126. text.PrintInfoValue(gotext.Get("Provides"), strings.Join(a.Provides, " "))
  127. text.PrintInfoValue(gotext.Get("Depends On"), strings.Join(a.Depends, " "))
  128. text.PrintInfoValue(gotext.Get("Make Deps"), strings.Join(a.MakeDepends, " "))
  129. text.PrintInfoValue(gotext.Get("Check Deps"), strings.Join(a.CheckDepends, " "))
  130. text.PrintInfoValue(gotext.Get("Optional Deps"), strings.Join(a.OptDepends, " "))
  131. text.PrintInfoValue(gotext.Get("Conflicts With"), strings.Join(a.Conflicts, " "))
  132. text.PrintInfoValue(gotext.Get("Maintainer"), a.Maintainer)
  133. text.PrintInfoValue(gotext.Get("Votes"), fmt.Sprintf("%d", a.NumVotes))
  134. text.PrintInfoValue(gotext.Get("Popularity"), fmt.Sprintf("%f", a.Popularity))
  135. text.PrintInfoValue(gotext.Get("First Submitted"), text.FormatTimeQuery(a.FirstSubmitted))
  136. text.PrintInfoValue(gotext.Get("Last Modified"), text.FormatTimeQuery(a.LastModified))
  137. if a.OutOfDate != 0 {
  138. text.PrintInfoValue(gotext.Get("Out-of-date"), text.FormatTimeQuery(a.OutOfDate))
  139. } else {
  140. text.PrintInfoValue(gotext.Get("Out-of-date"), "No")
  141. }
  142. if extendedInfo {
  143. text.PrintInfoValue("ID", fmt.Sprintf("%d", a.ID))
  144. text.PrintInfoValue(gotext.Get("Package Base ID"), fmt.Sprintf("%d", a.PackageBaseID))
  145. text.PrintInfoValue(gotext.Get("Package Base"), a.PackageBase)
  146. text.PrintInfoValue(gotext.Get("Snapshot URL"), config.AURURL+a.URLPath)
  147. }
  148. fmt.Println()
  149. }
  150. // BiggestPackages prints the name of the ten biggest packages in the system.
  151. func biggestPackages(alpmHandle *alpm.Handle) {
  152. localDB, err := alpmHandle.LocalDB()
  153. if err != nil {
  154. return
  155. }
  156. pkgCache := localDB.PkgCache()
  157. pkgS := pkgCache.SortBySize().Slice()
  158. if len(pkgS) < 10 {
  159. return
  160. }
  161. for i := 0; i < 10; i++ {
  162. fmt.Printf("%s: %s\n", bold(pkgS[i].Name()), cyan(text.Human(pkgS[i].ISize())))
  163. }
  164. // Could implement size here as well, but we just want the general idea
  165. }
  166. // localStatistics prints installed packages statistics.
  167. func localStatistics(alpmHandle *alpm.Handle) error {
  168. info, err := statistics(alpmHandle)
  169. if err != nil {
  170. return err
  171. }
  172. _, remoteNames, err := query.GetPackageNamesBySource(alpmHandle)
  173. if err != nil {
  174. return err
  175. }
  176. text.Infoln(gotext.Get("Yay version v%s", yayVersion))
  177. fmt.Println(bold(cyan("===========================================")))
  178. text.Infoln(gotext.Get("Total installed packages: %s", cyan(strconv.Itoa(info.Totaln))))
  179. text.Infoln(gotext.Get("Total foreign installed packages: %s", cyan(strconv.Itoa(len(remoteNames)))))
  180. text.Infoln(gotext.Get("Explicitly installed packages: %s", cyan(strconv.Itoa(info.Expln))))
  181. text.Infoln(gotext.Get("Total Size occupied by packages: %s", cyan(text.Human(info.TotalSize))))
  182. fmt.Println(bold(cyan("===========================================")))
  183. text.Infoln(gotext.Get("Ten biggest packages:"))
  184. biggestPackages(alpmHandle)
  185. fmt.Println(bold(cyan("===========================================")))
  186. query.AURInfoPrint(remoteNames, config.RequestSplitN)
  187. return nil
  188. }
  189. // TODO: Make it less hacky
  190. func printNumberOfUpdates(alpmHandle *alpm.Handle, enableDowngrade bool) error {
  191. warnings := query.NewWarnings()
  192. old := os.Stdout // keep backup of the real stdout
  193. os.Stdout = nil
  194. aurUp, repoUp, err := upList(warnings, alpmHandle, enableDowngrade)
  195. os.Stdout = old // restoring the real stdout
  196. if err != nil {
  197. return err
  198. }
  199. fmt.Println(len(aurUp) + len(repoUp))
  200. return nil
  201. }
  202. // TODO: Make it less hacky
  203. func printUpdateList(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle, enableDowngrade bool) error {
  204. targets := stringset.FromSlice(cmdArgs.Targets)
  205. warnings := query.NewWarnings()
  206. old := os.Stdout // keep backup of the real stdout
  207. os.Stdout = nil
  208. localNames, remoteNames, err := query.GetPackageNamesBySource(alpmHandle)
  209. if err != nil {
  210. return err
  211. }
  212. aurUp, repoUp, err := upList(warnings, alpmHandle, enableDowngrade)
  213. os.Stdout = old // restoring the real stdout
  214. if err != nil {
  215. return err
  216. }
  217. noTargets := len(targets) == 0
  218. if !cmdArgs.ExistsArg("m", "foreign") {
  219. for _, pkg := range repoUp {
  220. if noTargets || targets.Get(pkg.Name) {
  221. if cmdArgs.ExistsArg("q", "quiet") {
  222. fmt.Printf("%s\n", pkg.Name)
  223. } else {
  224. fmt.Printf("%s %s -> %s\n", bold(pkg.Name), green(pkg.LocalVersion), green(pkg.RemoteVersion))
  225. }
  226. delete(targets, pkg.Name)
  227. }
  228. }
  229. }
  230. if !cmdArgs.ExistsArg("n", "native") {
  231. for _, pkg := range aurUp {
  232. if noTargets || targets.Get(pkg.Name) {
  233. if cmdArgs.ExistsArg("q", "quiet") {
  234. fmt.Printf("%s\n", pkg.Name)
  235. } else {
  236. fmt.Printf("%s %s -> %s\n", bold(pkg.Name), green(pkg.LocalVersion), green(pkg.RemoteVersion))
  237. }
  238. delete(targets, pkg.Name)
  239. }
  240. }
  241. }
  242. missing := false
  243. outer:
  244. for pkg := range targets {
  245. for _, name := range localNames {
  246. if name == pkg {
  247. continue outer
  248. }
  249. }
  250. for _, name := range remoteNames {
  251. if name == pkg {
  252. continue outer
  253. }
  254. }
  255. text.Errorln(gotext.Get("package '%s' was not found", pkg))
  256. missing = true
  257. }
  258. if missing {
  259. return fmt.Errorf("")
  260. }
  261. return nil
  262. }
  263. const (
  264. redCode = "\x1b[31m"
  265. greenCode = "\x1b[32m"
  266. blueCode = "\x1b[34m"
  267. magentaCode = "\x1b[35m"
  268. cyanCode = "\x1b[36m"
  269. boldCode = "\x1b[1m"
  270. resetCode = "\x1b[0m"
  271. )
  272. func stylize(startCode, in string) string {
  273. if text.UseColor {
  274. return startCode + in + resetCode
  275. }
  276. return in
  277. }
  278. func red(in string) string {
  279. return stylize(redCode, in)
  280. }
  281. func green(in string) string {
  282. return stylize(greenCode, in)
  283. }
  284. func blue(in string) string {
  285. return stylize(blueCode, in)
  286. }
  287. func cyan(in string) string {
  288. return stylize(cyanCode, in)
  289. }
  290. func magenta(in string) string {
  291. return stylize(magentaCode, in)
  292. }
  293. func bold(in string) string {
  294. return stylize(boldCode, in)
  295. }