print.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. package main
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/xml"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "os"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/leonelquinteros/gotext"
  14. rpc "github.com/mikkeloscar/aur"
  15. "github.com/Jguer/yay/v10/pkg/intrange"
  16. "github.com/Jguer/yay/v10/pkg/stringset"
  17. "github.com/Jguer/yay/v10/pkg/text"
  18. )
  19. const arrow = "==>"
  20. const smallArrow = " ->"
  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. // human method returns results in human readable format.
  45. func human(size int64) string {
  46. floatsize := float32(size)
  47. units := [...]string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"}
  48. for _, unit := range units {
  49. if floatsize < 1024 {
  50. return fmt.Sprintf("%.1f %sB", floatsize, unit)
  51. }
  52. floatsize /= 1024
  53. }
  54. return fmt.Sprintf("%d%s", size, "B")
  55. }
  56. // PrintSearch handles printing search results in a given format
  57. func (q aurQuery) printSearch(start int) {
  58. localDB, _ := alpmHandle.LocalDB()
  59. for i := range q {
  60. var toprint string
  61. if config.SearchMode == numberMenu {
  62. switch config.SortMode {
  63. case topDown:
  64. toprint += magenta(strconv.Itoa(start+i) + " ")
  65. case bottomUp:
  66. toprint += magenta(strconv.Itoa(len(q)+start-i-1) + " ")
  67. default:
  68. text.Warnln(gotext.Get("invalid sort mode. Fix with yay -Y --bottomup --save"))
  69. }
  70. } else if config.SearchMode == minimal {
  71. fmt.Println(q[i].Name)
  72. continue
  73. }
  74. toprint += bold(colorHash("aur")) + "/" + bold(q[i].Name) +
  75. " " + cyan(q[i].Version) +
  76. bold(" (+"+strconv.Itoa(q[i].NumVotes)) +
  77. " " + bold(strconv.FormatFloat(q[i].Popularity, 'f', 2, 64)+"%) ")
  78. if q[i].Maintainer == "" {
  79. toprint += bold(red(gotext.Get("(Orphaned)"))) + " "
  80. }
  81. if q[i].OutOfDate != 0 {
  82. toprint += bold(red(gotext.Get("(Out-of-date: %s)", formatTime(q[i].OutOfDate)))) + " "
  83. }
  84. if pkg := localDB.Pkg(q[i].Name); pkg != nil {
  85. if pkg.Version() != q[i].Version {
  86. toprint += bold(green(gotext.Get("(Installed: %s)", pkg.Version())))
  87. } else {
  88. toprint += bold(green(gotext.Get("(Installed)")))
  89. }
  90. }
  91. toprint += "\n " + q[i].Description
  92. fmt.Println(toprint)
  93. }
  94. }
  95. // PrintSearch receives a RepoSearch type and outputs pretty text.
  96. func (s repoQuery) printSearch() {
  97. for i, res := range s {
  98. var toprint string
  99. if config.SearchMode == numberMenu {
  100. switch config.SortMode {
  101. case topDown:
  102. toprint += magenta(strconv.Itoa(i+1) + " ")
  103. case bottomUp:
  104. toprint += magenta(strconv.Itoa(len(s)-i) + " ")
  105. default:
  106. text.Warnln(gotext.Get("invalid sort mode. Fix with yay -Y --bottomup --save"))
  107. }
  108. } else if config.SearchMode == minimal {
  109. fmt.Println(res.Name())
  110. continue
  111. }
  112. toprint += bold(colorHash(res.DB().Name())) + "/" + bold(res.Name()) +
  113. " " + cyan(res.Version()) +
  114. bold(" ("+human(res.Size())+
  115. " "+human(res.ISize())+") ")
  116. if len(res.Groups().Slice()) != 0 {
  117. toprint += fmt.Sprint(res.Groups().Slice(), " ")
  118. }
  119. localDB, err := alpmHandle.LocalDB()
  120. if err == nil {
  121. if pkg := localDB.Pkg(res.Name()); pkg != nil {
  122. if pkg.Version() != res.Version() {
  123. toprint += bold(green(gotext.Get("(Installed: %s)", pkg.Version())))
  124. } else {
  125. toprint += bold(green(gotext.Get("(Installed)")))
  126. }
  127. }
  128. }
  129. toprint += "\n " + res.Description()
  130. fmt.Println(toprint)
  131. }
  132. }
  133. // Pretty print a set of packages from the same package base.
  134. // Packages foo and bar from a pkgbase named base would print like so:
  135. // base (foo bar)
  136. func (b Base) String() string {
  137. pkg := b[0]
  138. str := pkg.PackageBase
  139. if len(b) > 1 || pkg.PackageBase != pkg.Name {
  140. str2 := " ("
  141. for _, split := range b {
  142. str2 += split.Name + " "
  143. }
  144. str2 = str2[:len(str2)-1] + ")"
  145. str += str2
  146. }
  147. return str
  148. }
  149. func (u upgrade) StylizedNameWithRepository() string {
  150. return bold(colorHash(u.Repository)) + "/" + bold(u.Name)
  151. }
  152. // Print prints the details of the packages to upgrade.
  153. func (u upSlice) print() {
  154. longestName, longestVersion := 0, 0
  155. for _, pack := range u {
  156. packNameLen := len(pack.StylizedNameWithRepository())
  157. packVersion, _ := getVersionDiff(pack.LocalVersion, pack.RemoteVersion)
  158. packVersionLen := len(packVersion)
  159. longestName = intrange.Max(packNameLen, longestName)
  160. longestVersion = intrange.Max(packVersionLen, longestVersion)
  161. }
  162. namePadding := fmt.Sprintf("%%-%ds ", longestName)
  163. versionPadding := fmt.Sprintf("%%-%ds", longestVersion)
  164. numberPadding := fmt.Sprintf("%%%dd ", len(fmt.Sprintf("%v", len(u))))
  165. for k, i := range u {
  166. left, right := getVersionDiff(i.LocalVersion, i.RemoteVersion)
  167. fmt.Print(magenta(fmt.Sprintf(numberPadding, len(u)-k)))
  168. fmt.Printf(namePadding, i.StylizedNameWithRepository())
  169. fmt.Printf("%s -> %s\n", fmt.Sprintf(versionPadding, left), right)
  170. }
  171. }
  172. // Print prints repository packages to be downloaded
  173. func (do *depOrder) Print() {
  174. repo := ""
  175. repoMake := ""
  176. aur := ""
  177. aurMake := ""
  178. repoLen := 0
  179. repoMakeLen := 0
  180. aurLen := 0
  181. aurMakeLen := 0
  182. for _, pkg := range do.Repo {
  183. if do.Runtime.Get(pkg.Name()) {
  184. repo += " " + pkg.Name() + "-" + pkg.Version()
  185. repoLen++
  186. } else {
  187. repoMake += " " + pkg.Name() + "-" + pkg.Version()
  188. repoMakeLen++
  189. }
  190. }
  191. for _, base := range do.Aur {
  192. pkg := base.Pkgbase()
  193. pkgStr := " " + pkg + "-" + base[0].Version
  194. pkgStrMake := pkgStr
  195. push := false
  196. pushMake := false
  197. switch {
  198. case len(base) > 1, pkg != base[0].Name:
  199. pkgStr += " ("
  200. pkgStrMake += " ("
  201. for _, split := range base {
  202. if do.Runtime.Get(split.Name) {
  203. pkgStr += split.Name + " "
  204. aurLen++
  205. push = true
  206. } else {
  207. pkgStrMake += split.Name + " "
  208. aurMakeLen++
  209. pushMake = true
  210. }
  211. }
  212. pkgStr = pkgStr[:len(pkgStr)-1] + ")"
  213. pkgStrMake = pkgStrMake[:len(pkgStrMake)-1] + ")"
  214. case do.Runtime.Get(base[0].Name):
  215. aurLen++
  216. push = true
  217. default:
  218. aurMakeLen++
  219. pushMake = true
  220. }
  221. if push {
  222. aur += pkgStr
  223. }
  224. if pushMake {
  225. aurMake += pkgStrMake
  226. }
  227. }
  228. printDownloads("Repo", repoLen, repo)
  229. printDownloads("Repo Make", repoMakeLen, repoMake)
  230. printDownloads("Aur", aurLen, aur)
  231. printDownloads("Aur Make", aurMakeLen, aurMake)
  232. }
  233. func printDownloads(repoName string, length int, packages string) {
  234. if length < 1 {
  235. return
  236. }
  237. repoInfo := bold(blue(
  238. "[" + repoName + ": " + strconv.Itoa(length) + "]"))
  239. fmt.Println(repoInfo + cyan(packages))
  240. }
  241. // PrintInfo prints package info like pacman -Si.
  242. func PrintInfo(a *rpc.Pkg) {
  243. text.PrintInfoValue(gotext.Get("Repository"), "aur")
  244. text.PrintInfoValue(gotext.Get("Name"), a.Name)
  245. text.PrintInfoValue(gotext.Get("Keywords"), strings.Join(a.Keywords, " "))
  246. text.PrintInfoValue(gotext.Get("Version"), a.Version)
  247. text.PrintInfoValue(gotext.Get("Description"), a.Description)
  248. text.PrintInfoValue(gotext.Get("URL"), a.URL)
  249. text.PrintInfoValue(gotext.Get("AUR URL"), config.AURURL+"/packages/"+a.Name)
  250. text.PrintInfoValue(gotext.Get("Groups"), strings.Join(a.Groups, " "))
  251. text.PrintInfoValue(gotext.Get("Licenses"), strings.Join(a.License, " "))
  252. text.PrintInfoValue(gotext.Get("Provides"), strings.Join(a.Provides, " "))
  253. text.PrintInfoValue(gotext.Get("Depends On"), strings.Join(a.Depends, " "))
  254. text.PrintInfoValue(gotext.Get("Make Deps"), strings.Join(a.MakeDepends, " "))
  255. text.PrintInfoValue(gotext.Get("Check Deps"), strings.Join(a.CheckDepends, " "))
  256. text.PrintInfoValue(gotext.Get("Optional Deps"), strings.Join(a.OptDepends, " "))
  257. text.PrintInfoValue(gotext.Get("Conflicts With"), strings.Join(a.Conflicts, " "))
  258. text.PrintInfoValue(gotext.Get("Maintainer"), a.Maintainer)
  259. text.PrintInfoValue(gotext.Get("Votes"), fmt.Sprintf("%d", a.NumVotes))
  260. text.PrintInfoValue(gotext.Get("Popularity"), fmt.Sprintf("%f", a.Popularity))
  261. text.PrintInfoValue(gotext.Get("First Submitted"), formatTimeQuery(a.FirstSubmitted))
  262. text.PrintInfoValue(gotext.Get("Last Modified"), formatTimeQuery(a.LastModified))
  263. if a.OutOfDate != 0 {
  264. text.PrintInfoValue(gotext.Get("Out-of-date"), formatTimeQuery(a.OutOfDate))
  265. } else {
  266. text.PrintInfoValue(gotext.Get("Out-of-date"), "No")
  267. }
  268. if cmdArgs.existsDouble("i") {
  269. text.PrintInfoValue("ID", fmt.Sprintf("%d", a.ID))
  270. text.PrintInfoValue(gotext.Get("Package Base ID"), fmt.Sprintf("%d", a.PackageBaseID))
  271. text.PrintInfoValue(gotext.Get("Package Base"), a.PackageBase)
  272. text.PrintInfoValue(gotext.Get("Snapshot URL"), config.AURURL+a.URLPath)
  273. }
  274. fmt.Println()
  275. }
  276. // BiggestPackages prints the name of the ten biggest packages in the system.
  277. func biggestPackages() {
  278. localDB, err := alpmHandle.LocalDB()
  279. if err != nil {
  280. return
  281. }
  282. pkgCache := localDB.PkgCache()
  283. pkgS := pkgCache.SortBySize().Slice()
  284. if len(pkgS) < 10 {
  285. return
  286. }
  287. for i := 0; i < 10; i++ {
  288. fmt.Printf("%s: %s\n", bold(pkgS[i].Name()), cyan(human(pkgS[i].ISize())))
  289. }
  290. // Could implement size here as well, but we just want the general idea
  291. }
  292. // localStatistics prints installed packages statistics.
  293. func localStatistics() error {
  294. info, err := statistics()
  295. if err != nil {
  296. return err
  297. }
  298. _, _, _, remoteNames, err := filterPackages()
  299. if err != nil {
  300. return err
  301. }
  302. text.Infoln(gotext.Get("Yay version v%s", yayVersion))
  303. fmt.Println(bold(cyan("===========================================")))
  304. text.Infoln(gotext.Get("Total installed packages: %s", cyan(strconv.Itoa(info.Totaln))))
  305. text.Infoln(gotext.Get("Total foreign installed packages: %s", cyan(strconv.Itoa(len(remoteNames)))))
  306. text.Infoln(gotext.Get("Explicitly installed packages: %s", cyan(strconv.Itoa(info.Expln))))
  307. text.Infoln(gotext.Get("Total Size occupied by packages: %s", cyan(human(info.TotalSize))))
  308. fmt.Println(bold(cyan("===========================================")))
  309. text.Infoln(gotext.Get("Ten biggest packages:"))
  310. biggestPackages()
  311. fmt.Println(bold(cyan("===========================================")))
  312. aurInfoPrint(remoteNames)
  313. return nil
  314. }
  315. //TODO: Make it less hacky
  316. func printNumberOfUpdates() error {
  317. warnings := makeWarnings()
  318. old := os.Stdout // keep backup of the real stdout
  319. os.Stdout = nil
  320. aurUp, repoUp, err := upList(warnings)
  321. os.Stdout = old // restoring the real stdout
  322. if err != nil {
  323. return err
  324. }
  325. fmt.Println(len(aurUp) + len(repoUp))
  326. return nil
  327. }
  328. //TODO: Make it less hacky
  329. func printUpdateList(parser *arguments) error {
  330. targets := stringset.FromSlice(parser.targets)
  331. warnings := makeWarnings()
  332. old := os.Stdout // keep backup of the real stdout
  333. os.Stdout = nil
  334. _, _, localNames, remoteNames, err := filterPackages()
  335. if err != nil {
  336. return err
  337. }
  338. aurUp, repoUp, err := upList(warnings)
  339. os.Stdout = old // restoring the real stdout
  340. if err != nil {
  341. return err
  342. }
  343. noTargets := len(targets) == 0
  344. if !parser.existsArg("m", "foreign") {
  345. for _, pkg := range repoUp {
  346. if noTargets || targets.Get(pkg.Name) {
  347. if parser.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. if !parser.existsArg("n", "native") {
  357. for _, pkg := range aurUp {
  358. if noTargets || targets.Get(pkg.Name) {
  359. if parser.existsArg("q", "quiet") {
  360. fmt.Printf("%s\n", pkg.Name)
  361. } else {
  362. fmt.Printf("%s %s -> %s\n", bold(pkg.Name), green(pkg.LocalVersion), green(pkg.RemoteVersion))
  363. }
  364. delete(targets, pkg.Name)
  365. }
  366. }
  367. }
  368. missing := false
  369. outer:
  370. for pkg := range targets {
  371. for _, name := range localNames {
  372. if name == pkg {
  373. continue outer
  374. }
  375. }
  376. for _, name := range remoteNames {
  377. if name == pkg {
  378. continue outer
  379. }
  380. }
  381. text.Errorln(gotext.Get("package '%s' was not found", pkg))
  382. missing = true
  383. }
  384. if missing {
  385. return fmt.Errorf("")
  386. }
  387. return nil
  388. }
  389. type item struct {
  390. Title string `xml:"title"`
  391. Link string `xml:"link"`
  392. Description string `xml:"description"`
  393. PubDate string `xml:"pubDate"`
  394. Creator string `xml:"dc:creator"`
  395. }
  396. func (item *item) print(buildTime time.Time) {
  397. var fd string
  398. date, err := time.Parse(time.RFC1123Z, item.PubDate)
  399. if err != nil {
  400. fmt.Fprintln(os.Stderr, err)
  401. } else {
  402. fd = formatTime(int(date.Unix()))
  403. if _, double, _ := cmdArgs.getArg("news", "w"); !double && !buildTime.IsZero() {
  404. if buildTime.After(date) {
  405. return
  406. }
  407. }
  408. }
  409. fmt.Println(bold(magenta(fd)), bold(strings.TrimSpace(item.Title)))
  410. if !cmdArgs.existsArg("q", "quiet") {
  411. desc := strings.TrimSpace(parseNews(item.Description))
  412. fmt.Println(desc)
  413. }
  414. }
  415. type channel struct {
  416. Title string `xml:"title"`
  417. Link string `xml:"link"`
  418. Description string `xml:"description"`
  419. Language string `xml:"language"`
  420. Lastbuilddate string `xml:"lastbuilddate"`
  421. Items []item `xml:"item"`
  422. }
  423. type rss struct {
  424. Channel channel `xml:"channel"`
  425. }
  426. func printNewsFeed() error {
  427. resp, err := http.Get("https://archlinux.org/feeds/news")
  428. if err != nil {
  429. return err
  430. }
  431. defer resp.Body.Close()
  432. body, err := ioutil.ReadAll(resp.Body)
  433. if err != nil {
  434. return err
  435. }
  436. rssGot := rss{}
  437. d := xml.NewDecoder(bytes.NewReader(body))
  438. err = d.Decode(&rssGot)
  439. if err != nil {
  440. return err
  441. }
  442. buildTime, err := lastBuildTime()
  443. if err != nil {
  444. return err
  445. }
  446. if config.SortMode == bottomUp {
  447. for i := len(rssGot.Channel.Items) - 1; i >= 0; i-- {
  448. rssGot.Channel.Items[i].print(buildTime)
  449. }
  450. } else {
  451. for i := 0; i < len(rssGot.Channel.Items); i++ {
  452. rssGot.Channel.Items[i].print(buildTime)
  453. }
  454. }
  455. return nil
  456. }
  457. // Formats a unix timestamp to ISO 8601 date (yyyy-mm-dd)
  458. func formatTime(i int) string {
  459. t := time.Unix(int64(i), 0)
  460. return t.Format("2006-01-02")
  461. }
  462. // Formats a unix timestamp to ISO 8601 date (Mon 02 Jan 2006 03:04:05 PM MST)
  463. func formatTimeQuery(i int) string {
  464. t := time.Unix(int64(i), 0)
  465. return t.Format("Mon 02 Jan 2006 03:04:05 PM MST")
  466. }
  467. const (
  468. redCode = "\x1b[31m"
  469. greenCode = "\x1b[32m"
  470. yellowCode = "\x1b[33m"
  471. blueCode = "\x1b[34m"
  472. magentaCode = "\x1b[35m"
  473. cyanCode = "\x1b[36m"
  474. boldCode = "\x1b[1m"
  475. resetCode = "\x1b[0m"
  476. )
  477. func stylize(startCode, in string) string {
  478. if useColor {
  479. return startCode + in + resetCode
  480. }
  481. return in
  482. }
  483. func red(in string) string {
  484. return stylize(redCode, in)
  485. }
  486. func green(in string) string {
  487. return stylize(greenCode, in)
  488. }
  489. func yellow(in string) string {
  490. return stylize(yellowCode, in)
  491. }
  492. func blue(in string) string {
  493. return stylize(blueCode, in)
  494. }
  495. func cyan(in string) string {
  496. return stylize(cyanCode, in)
  497. }
  498. func magenta(in string) string {
  499. return stylize(magentaCode, in)
  500. }
  501. func bold(in string) string {
  502. return stylize(boldCode, in)
  503. }
  504. // Colors text using a hashing algorithm. The same text will always produce the
  505. // same color while different text will produce a different color.
  506. func colorHash(name string) (output string) {
  507. if !useColor {
  508. return name
  509. }
  510. var hash uint = 5381
  511. for i := 0; i < len(name); i++ {
  512. hash = uint(name[i]) + ((hash << 5) + (hash))
  513. }
  514. return fmt.Sprintf("\x1b[%dm%s\x1b[0m", hash%6+31, name)
  515. }
  516. func providerMenu(dep string, providers providers) *rpc.Pkg {
  517. size := providers.Len()
  518. str := bold(gotext.Get("There are %d providers available for %s:", size, dep))
  519. size = 1
  520. str += bold(cyan("\n:: ")) + bold(gotext.Get("Repository AUR")) + "\n "
  521. for _, pkg := range providers.Pkgs {
  522. str += fmt.Sprintf("%d) %s ", size, pkg.Name)
  523. size++
  524. }
  525. text.OperationInfoln(str)
  526. for {
  527. fmt.Print(gotext.Get("\nEnter a number (default=1): "))
  528. if config.NoConfirm {
  529. fmt.Println("1")
  530. return providers.Pkgs[0]
  531. }
  532. reader := bufio.NewReader(os.Stdin)
  533. numberBuf, overflow, err := reader.ReadLine()
  534. if err != nil {
  535. fmt.Fprintln(os.Stderr, err)
  536. break
  537. }
  538. if overflow {
  539. text.Errorln(gotext.Get("input too long"))
  540. continue
  541. }
  542. if string(numberBuf) == "" {
  543. return providers.Pkgs[0]
  544. }
  545. num, err := strconv.Atoi(string(numberBuf))
  546. if err != nil {
  547. text.Errorln(gotext.Get("invalid number: %s", string(numberBuf)))
  548. continue
  549. }
  550. if num < 1 || num >= size {
  551. text.Errorln(gotext.Get("invalid value: %d is not between %d and %d", num, 1, size-1))
  552. continue
  553. }
  554. return providers.Pkgs[num-1]
  555. }
  556. return nil
  557. }