print.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "github.com/leonelquinteros/gotext"
  9. rpc "github.com/mikkeloscar/aur"
  10. "github.com/Jguer/go-alpm"
  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. const (
  18. arrow = "==>"
  19. smallArrow = " ->"
  20. )
  21. func (warnings *aurWarnings) print() {
  22. if len(warnings.Missing) > 0 {
  23. text.Warn(gotext.Get("Missing AUR Packages:"))
  24. for _, name := range warnings.Missing {
  25. fmt.Print(" " + cyan(name))
  26. }
  27. fmt.Println()
  28. }
  29. if len(warnings.Orphans) > 0 {
  30. text.Warn(gotext.Get("Orphaned AUR Packages:"))
  31. for _, name := range warnings.Orphans {
  32. fmt.Print(" " + cyan(name))
  33. }
  34. fmt.Println()
  35. }
  36. if len(warnings.OutOfDate) > 0 {
  37. text.Warn(gotext.Get("Flagged Out Of Date AUR Packages:"))
  38. for _, name := range warnings.OutOfDate {
  39. fmt.Print(" " + cyan(name))
  40. }
  41. fmt.Println()
  42. }
  43. }
  44. // PrintSearch handles printing search results in a given format
  45. func (q aurQuery) printSearch(start int, alpmHandle *alpm.Handle) {
  46. localDB, _ := alpmHandle.LocalDB()
  47. for i := range q {
  48. var toprint string
  49. if config.SearchMode == numberMenu {
  50. switch config.SortMode {
  51. case settings.TopDown:
  52. toprint += magenta(strconv.Itoa(start+i) + " ")
  53. case settings.BottomUp:
  54. toprint += magenta(strconv.Itoa(len(q)+start-i-1) + " ")
  55. default:
  56. text.Warnln(gotext.Get("invalid sort mode. Fix with yay -Y --bottomup --save"))
  57. }
  58. } else if config.SearchMode == minimal {
  59. fmt.Println(q[i].Name)
  60. continue
  61. }
  62. toprint += bold(text.ColorHash("aur")) + "/" + bold(q[i].Name) +
  63. " " + cyan(q[i].Version) +
  64. bold(" (+"+strconv.Itoa(q[i].NumVotes)) +
  65. " " + bold(strconv.FormatFloat(q[i].Popularity, 'f', 2, 64)+") ")
  66. if q[i].Maintainer == "" {
  67. toprint += bold(red(gotext.Get("(Orphaned)"))) + " "
  68. }
  69. if q[i].OutOfDate != 0 {
  70. toprint += bold(red(gotext.Get("(Out-of-date: %s)", text.FormatTime(q[i].OutOfDate)))) + " "
  71. }
  72. if pkg := localDB.Pkg(q[i].Name); pkg != nil {
  73. if pkg.Version() != q[i].Version {
  74. toprint += bold(green(gotext.Get("(Installed: %s)", pkg.Version())))
  75. } else {
  76. toprint += bold(green(gotext.Get("(Installed)")))
  77. }
  78. }
  79. toprint += "\n " + q[i].Description
  80. fmt.Println(toprint)
  81. }
  82. }
  83. // PrintSearch receives a RepoSearch type and outputs pretty text.
  84. func (s repoQuery) printSearch(alpmHandle *alpm.Handle) {
  85. for i, res := range s {
  86. var toprint string
  87. if config.SearchMode == numberMenu {
  88. switch config.SortMode {
  89. case settings.TopDown:
  90. toprint += magenta(strconv.Itoa(i+1) + " ")
  91. case settings.BottomUp:
  92. toprint += magenta(strconv.Itoa(len(s)-i) + " ")
  93. default:
  94. text.Warnln(gotext.Get("invalid sort mode. Fix with yay -Y --bottomup --save"))
  95. }
  96. } else if config.SearchMode == minimal {
  97. fmt.Println(res.Name())
  98. continue
  99. }
  100. toprint += bold(text.ColorHash(res.DB().Name())) + "/" + bold(res.Name()) +
  101. " " + cyan(res.Version()) +
  102. bold(" ("+text.Human(res.Size())+
  103. " "+text.Human(res.ISize())+") ")
  104. if len(res.Groups().Slice()) != 0 {
  105. toprint += fmt.Sprint(res.Groups().Slice(), " ")
  106. }
  107. localDB, err := alpmHandle.LocalDB()
  108. if err == nil {
  109. if pkg := localDB.Pkg(res.Name()); pkg != nil {
  110. if pkg.Version() != res.Version() {
  111. toprint += bold(green(gotext.Get("(Installed: %s)", pkg.Version())))
  112. } else {
  113. toprint += bold(green(gotext.Get("(Installed)")))
  114. }
  115. }
  116. }
  117. toprint += "\n " + res.Description()
  118. fmt.Println(toprint)
  119. }
  120. }
  121. // Pretty print a set of packages from the same package base.
  122. // Packages foo and bar from a pkgbase named base would print like so:
  123. // base (foo bar)
  124. func (b Base) String() string {
  125. pkg := b[0]
  126. str := pkg.PackageBase
  127. if len(b) > 1 || pkg.PackageBase != pkg.Name {
  128. str2 := " ("
  129. for _, split := range b {
  130. str2 += split.Name + " "
  131. }
  132. str2 = str2[:len(str2)-1] + ")"
  133. str += str2
  134. }
  135. return str
  136. }
  137. func (u *upgrade) StylizedNameWithRepository() string {
  138. return bold(text.ColorHash(u.Repository)) + "/" + bold(u.Name)
  139. }
  140. // Print prints the details of the packages to upgrade.
  141. func (u upSlice) print() {
  142. longestName, longestVersion := 0, 0
  143. for _, pack := range u {
  144. packNameLen := len(pack.StylizedNameWithRepository())
  145. packVersion, _ := getVersionDiff(pack.LocalVersion, pack.RemoteVersion)
  146. packVersionLen := len(packVersion)
  147. longestName = intrange.Max(packNameLen, longestName)
  148. longestVersion = intrange.Max(packVersionLen, longestVersion)
  149. }
  150. namePadding := fmt.Sprintf("%%-%ds ", longestName)
  151. versionPadding := fmt.Sprintf("%%-%ds", longestVersion)
  152. numberPadding := fmt.Sprintf("%%%dd ", len(fmt.Sprintf("%v", len(u))))
  153. for k, i := range u {
  154. left, right := getVersionDiff(i.LocalVersion, i.RemoteVersion)
  155. fmt.Print(magenta(fmt.Sprintf(numberPadding, len(u)-k)))
  156. fmt.Printf(namePadding, i.StylizedNameWithRepository())
  157. fmt.Printf("%s -> %s\n", fmt.Sprintf(versionPadding, left), right)
  158. }
  159. }
  160. // Print prints repository packages to be downloaded
  161. func (do *depOrder) Print() {
  162. repo := ""
  163. repoMake := ""
  164. aur := ""
  165. aurMake := ""
  166. repoLen := 0
  167. repoMakeLen := 0
  168. aurLen := 0
  169. aurMakeLen := 0
  170. for _, pkg := range do.Repo {
  171. if do.Runtime.Get(pkg.Name()) {
  172. repo += " " + pkg.Name() + "-" + pkg.Version()
  173. repoLen++
  174. } else {
  175. repoMake += " " + pkg.Name() + "-" + pkg.Version()
  176. repoMakeLen++
  177. }
  178. }
  179. for _, base := range do.Aur {
  180. pkg := base.Pkgbase()
  181. pkgStr := " " + pkg + "-" + base[0].Version
  182. pkgStrMake := pkgStr
  183. push := false
  184. pushMake := false
  185. switch {
  186. case len(base) > 1, pkg != base[0].Name:
  187. pkgStr += " ("
  188. pkgStrMake += " ("
  189. for _, split := range base {
  190. if do.Runtime.Get(split.Name) {
  191. pkgStr += split.Name + " "
  192. aurLen++
  193. push = true
  194. } else {
  195. pkgStrMake += split.Name + " "
  196. aurMakeLen++
  197. pushMake = true
  198. }
  199. }
  200. pkgStr = pkgStr[:len(pkgStr)-1] + ")"
  201. pkgStrMake = pkgStrMake[:len(pkgStrMake)-1] + ")"
  202. case do.Runtime.Get(base[0].Name):
  203. aurLen++
  204. push = true
  205. default:
  206. aurMakeLen++
  207. pushMake = true
  208. }
  209. if push {
  210. aur += pkgStr
  211. }
  212. if pushMake {
  213. aurMake += pkgStrMake
  214. }
  215. }
  216. printDownloads("Repo", repoLen, repo)
  217. printDownloads("Repo Make", repoMakeLen, repoMake)
  218. printDownloads("Aur", aurLen, aur)
  219. printDownloads("Aur Make", aurMakeLen, aurMake)
  220. }
  221. func printDownloads(repoName string, length int, packages string) {
  222. if length < 1 {
  223. return
  224. }
  225. repoInfo := bold(blue(
  226. "[" + repoName + ": " + strconv.Itoa(length) + "]"))
  227. fmt.Println(repoInfo + cyan(packages))
  228. }
  229. // PrintInfo prints package info like pacman -Si.
  230. func PrintInfo(a *rpc.Pkg, extendedInfo bool) {
  231. text.PrintInfoValue(gotext.Get("Repository"), "aur")
  232. text.PrintInfoValue(gotext.Get("Name"), a.Name)
  233. text.PrintInfoValue(gotext.Get("Keywords"), strings.Join(a.Keywords, " "))
  234. text.PrintInfoValue(gotext.Get("Version"), a.Version)
  235. text.PrintInfoValue(gotext.Get("Description"), a.Description)
  236. text.PrintInfoValue(gotext.Get("URL"), a.URL)
  237. text.PrintInfoValue(gotext.Get("AUR URL"), config.AURURL+"/packages/"+a.Name)
  238. text.PrintInfoValue(gotext.Get("Groups"), strings.Join(a.Groups, " "))
  239. text.PrintInfoValue(gotext.Get("Licenses"), strings.Join(a.License, " "))
  240. text.PrintInfoValue(gotext.Get("Provides"), strings.Join(a.Provides, " "))
  241. text.PrintInfoValue(gotext.Get("Depends On"), strings.Join(a.Depends, " "))
  242. text.PrintInfoValue(gotext.Get("Make Deps"), strings.Join(a.MakeDepends, " "))
  243. text.PrintInfoValue(gotext.Get("Check Deps"), strings.Join(a.CheckDepends, " "))
  244. text.PrintInfoValue(gotext.Get("Optional Deps"), strings.Join(a.OptDepends, " "))
  245. text.PrintInfoValue(gotext.Get("Conflicts With"), strings.Join(a.Conflicts, " "))
  246. text.PrintInfoValue(gotext.Get("Maintainer"), a.Maintainer)
  247. text.PrintInfoValue(gotext.Get("Votes"), fmt.Sprintf("%d", a.NumVotes))
  248. text.PrintInfoValue(gotext.Get("Popularity"), fmt.Sprintf("%f", a.Popularity))
  249. text.PrintInfoValue(gotext.Get("First Submitted"), text.FormatTimeQuery(a.FirstSubmitted))
  250. text.PrintInfoValue(gotext.Get("Last Modified"), text.FormatTimeQuery(a.LastModified))
  251. if a.OutOfDate != 0 {
  252. text.PrintInfoValue(gotext.Get("Out-of-date"), text.FormatTimeQuery(a.OutOfDate))
  253. } else {
  254. text.PrintInfoValue(gotext.Get("Out-of-date"), "No")
  255. }
  256. if extendedInfo {
  257. text.PrintInfoValue("ID", fmt.Sprintf("%d", a.ID))
  258. text.PrintInfoValue(gotext.Get("Package Base ID"), fmt.Sprintf("%d", a.PackageBaseID))
  259. text.PrintInfoValue(gotext.Get("Package Base"), a.PackageBase)
  260. text.PrintInfoValue(gotext.Get("Snapshot URL"), config.AURURL+a.URLPath)
  261. }
  262. fmt.Println()
  263. }
  264. // BiggestPackages prints the name of the ten biggest packages in the system.
  265. func biggestPackages(alpmHandle *alpm.Handle) {
  266. localDB, err := alpmHandle.LocalDB()
  267. if err != nil {
  268. return
  269. }
  270. pkgCache := localDB.PkgCache()
  271. pkgS := pkgCache.SortBySize().Slice()
  272. if len(pkgS) < 10 {
  273. return
  274. }
  275. for i := 0; i < 10; i++ {
  276. fmt.Printf("%s: %s\n", bold(pkgS[i].Name()), cyan(text.Human(pkgS[i].ISize())))
  277. }
  278. // Could implement size here as well, but we just want the general idea
  279. }
  280. // localStatistics prints installed packages statistics.
  281. func localStatistics(alpmHandle *alpm.Handle) error {
  282. info, err := statistics(alpmHandle)
  283. if err != nil {
  284. return err
  285. }
  286. _, _, _, remoteNames, err := query.FilterPackages(alpmHandle)
  287. if err != nil {
  288. return err
  289. }
  290. text.Infoln(gotext.Get("Yay version v%s", yayVersion))
  291. fmt.Println(bold(cyan("===========================================")))
  292. text.Infoln(gotext.Get("Total installed packages: %s", cyan(strconv.Itoa(info.Totaln))))
  293. text.Infoln(gotext.Get("Total foreign installed packages: %s", cyan(strconv.Itoa(len(remoteNames)))))
  294. text.Infoln(gotext.Get("Explicitly installed packages: %s", cyan(strconv.Itoa(info.Expln))))
  295. text.Infoln(gotext.Get("Total Size occupied by packages: %s", cyan(text.Human(info.TotalSize))))
  296. fmt.Println(bold(cyan("===========================================")))
  297. text.Infoln(gotext.Get("Ten biggest packages:"))
  298. biggestPackages(alpmHandle)
  299. fmt.Println(bold(cyan("===========================================")))
  300. aurInfoPrint(remoteNames)
  301. return nil
  302. }
  303. // TODO: Make it less hacky
  304. func printNumberOfUpdates(alpmHandle *alpm.Handle, enableDowngrade bool) error {
  305. warnings := makeWarnings()
  306. old := os.Stdout // keep backup of the real stdout
  307. os.Stdout = nil
  308. aurUp, repoUp, err := upList(warnings, alpmHandle, enableDowngrade)
  309. os.Stdout = old // restoring the real stdout
  310. if err != nil {
  311. return err
  312. }
  313. fmt.Println(len(aurUp) + len(repoUp))
  314. return nil
  315. }
  316. // TODO: Make it less hacky
  317. func printUpdateList(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle, enableDowngrade bool) error {
  318. targets := stringset.FromSlice(cmdArgs.Targets)
  319. warnings := makeWarnings()
  320. old := os.Stdout // keep backup of the real stdout
  321. os.Stdout = nil
  322. _, _, localNames, remoteNames, err := query.FilterPackages(alpmHandle)
  323. if err != nil {
  324. return err
  325. }
  326. aurUp, repoUp, err := upList(warnings, alpmHandle, enableDowngrade)
  327. os.Stdout = old // restoring the real stdout
  328. if err != nil {
  329. return err
  330. }
  331. noTargets := len(targets) == 0
  332. if !cmdArgs.ExistsArg("m", "foreign") {
  333. for _, pkg := range repoUp {
  334. if noTargets || targets.Get(pkg.Name) {
  335. if cmdArgs.ExistsArg("q", "quiet") {
  336. fmt.Printf("%s\n", pkg.Name)
  337. } else {
  338. fmt.Printf("%s %s -> %s\n", bold(pkg.Name), green(pkg.LocalVersion), green(pkg.RemoteVersion))
  339. }
  340. delete(targets, pkg.Name)
  341. }
  342. }
  343. }
  344. if !cmdArgs.ExistsArg("n", "native") {
  345. for _, pkg := range aurUp {
  346. if noTargets || targets.Get(pkg.Name) {
  347. if cmdArgs.ExistsArg("q", "quiet") {
  348. fmt.Printf("%s\n", pkg.Name)
  349. } else {
  350. fmt.Printf("%s %s -> %s\n", bold(pkg.Name), green(pkg.LocalVersion), green(pkg.RemoteVersion))
  351. }
  352. delete(targets, pkg.Name)
  353. }
  354. }
  355. }
  356. missing := false
  357. outer:
  358. for pkg := range targets {
  359. for _, name := range localNames {
  360. if name == pkg {
  361. continue outer
  362. }
  363. }
  364. for _, name := range remoteNames {
  365. if name == pkg {
  366. continue outer
  367. }
  368. }
  369. text.Errorln(gotext.Get("package '%s' was not found", pkg))
  370. missing = true
  371. }
  372. if missing {
  373. return fmt.Errorf("")
  374. }
  375. return nil
  376. }
  377. const (
  378. redCode = "\x1b[31m"
  379. greenCode = "\x1b[32m"
  380. yellowCode = "\x1b[33m"
  381. blueCode = "\x1b[34m"
  382. magentaCode = "\x1b[35m"
  383. cyanCode = "\x1b[36m"
  384. boldCode = "\x1b[1m"
  385. resetCode = "\x1b[0m"
  386. )
  387. func stylize(startCode, in string) string {
  388. if text.UseColor {
  389. return startCode + in + resetCode
  390. }
  391. return in
  392. }
  393. func red(in string) string {
  394. return stylize(redCode, in)
  395. }
  396. func green(in string) string {
  397. return stylize(greenCode, in)
  398. }
  399. func yellow(in string) string {
  400. return stylize(yellowCode, in)
  401. }
  402. func blue(in string) string {
  403. return stylize(blueCode, in)
  404. }
  405. func cyan(in string) string {
  406. return stylize(cyanCode, in)
  407. }
  408. func magenta(in string) string {
  409. return stylize(magentaCode, in)
  410. }
  411. func bold(in string) string {
  412. return stylize(boldCode, in)
  413. }
  414. func providerMenu(dep string, providers providers) *rpc.Pkg {
  415. size := providers.Len()
  416. str := bold(gotext.Get("There are %d providers available for %s:", size, dep))
  417. size = 1
  418. str += bold(cyan("\n:: ")) + bold(gotext.Get("Repository AUR")) + "\n "
  419. for _, pkg := range providers.Pkgs {
  420. str += fmt.Sprintf("%d) %s ", size, pkg.Name)
  421. size++
  422. }
  423. text.OperationInfoln(str)
  424. for {
  425. fmt.Print(gotext.Get("\nEnter a number (default=1): "))
  426. if config.NoConfirm {
  427. fmt.Println("1")
  428. return providers.Pkgs[0]
  429. }
  430. reader := bufio.NewReader(os.Stdin)
  431. numberBuf, overflow, err := reader.ReadLine()
  432. if err != nil {
  433. fmt.Fprintln(os.Stderr, err)
  434. break
  435. }
  436. if overflow {
  437. text.Errorln(gotext.Get("input too long"))
  438. continue
  439. }
  440. if string(numberBuf) == "" {
  441. return providers.Pkgs[0]
  442. }
  443. num, err := strconv.Atoi(string(numberBuf))
  444. if err != nil {
  445. text.Errorln(gotext.Get("invalid number: %s", string(numberBuf)))
  446. continue
  447. }
  448. if num < 1 || num >= size {
  449. text.Errorln(gotext.Get("invalid value: %d is not between %d and %d", num, 1, size-1))
  450. continue
  451. }
  452. return providers.Pkgs[num-1]
  453. }
  454. return nil
  455. }