print.go 16 KB

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