print.go 14 KB

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