print.go 15 KB

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