print.go 14 KB

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