print.go 10 KB

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