print.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package main
  2. import (
  3. "fmt"
  4. rpc "github.com/mikkeloscar/aur"
  5. )
  6. // Human returns results in Human readable format.
  7. func human(size int64) string {
  8. floatsize := float32(size)
  9. units := [...]string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"}
  10. for _, unit := range units {
  11. if floatsize < 1024 {
  12. return fmt.Sprintf("%.1f %sB", floatsize, unit)
  13. }
  14. floatsize /= 1024
  15. }
  16. return fmt.Sprintf("%d%s", size, "B")
  17. }
  18. // PrintSearch handles printing search results in a given format
  19. func (q aurQuery) printSearch(start int) {
  20. localDb, _ := alpmHandle.LocalDb()
  21. for i, res := range q {
  22. var toprint string
  23. if config.SearchMode == NumberMenu {
  24. if config.SortMode == BottomUp {
  25. toprint += fmt.Sprintf("\x1b[33m%d\x1b[0m ", len(q)+start-i-1)
  26. } else {
  27. toprint += fmt.Sprintf("\x1b[33m%d\x1b[0m ", start+i)
  28. }
  29. } else if config.SearchMode == Minimal {
  30. fmt.Println(res.Name)
  31. continue
  32. }
  33. toprint += fmt.Sprintf("\x1b[1m%s/\x1b[33m%s \x1b[36m%s \x1b[0m(%d) ", "aur", res.Name, res.Version, res.NumVotes)
  34. if res.Maintainer == "" {
  35. toprint += fmt.Sprintf("\x1b[31;40m(Orphaned)\x1b[0m ")
  36. }
  37. if res.OutOfDate != 0 {
  38. toprint += fmt.Sprintf("\x1b[31;40m(Out-of-date)\x1b[0m ")
  39. }
  40. if _, err := localDb.PkgByName(res.Name); err == nil {
  41. toprint += fmt.Sprintf("\x1b[32;40mInstalled\x1b[0m")
  42. }
  43. toprint += "\n " + res.Description
  44. fmt.Println(toprint)
  45. }
  46. }
  47. //PrintSearch receives a RepoSearch type and outputs pretty text.
  48. func (s repoQuery) printSearch() {
  49. for i, res := range s {
  50. var toprint string
  51. if config.SearchMode == NumberMenu {
  52. if config.SortMode == BottomUp {
  53. toprint += fmt.Sprintf("\x1b[33m%d\x1b[0m ", len(s)-i-1)
  54. } else {
  55. toprint += fmt.Sprintf("\x1b[33m%d\x1b[0m ", i)
  56. }
  57. } else if config.SearchMode == Minimal {
  58. fmt.Println(res.Name())
  59. continue
  60. }
  61. toprint += fmt.Sprintf("\x1b[1m%s/\x1b[33m%s \x1b[36m%s \x1b[0m",
  62. res.DB().Name(), res.Name(), res.Version())
  63. if len(res.Groups().Slice()) != 0 {
  64. toprint += fmt.Sprint(res.Groups().Slice(), " ")
  65. }
  66. localDb, err := alpmHandle.LocalDb()
  67. if err == nil {
  68. if _, err = localDb.PkgByName(res.Name()); err == nil {
  69. toprint += fmt.Sprintf("\x1b[32;40mInstalled\x1b[0m")
  70. }
  71. }
  72. toprint += "\n " + res.Description()
  73. fmt.Println(toprint)
  74. }
  75. }
  76. func printDeps(repoDeps []string, aurDeps []string) {
  77. if len(repoDeps) != 0 {
  78. fmt.Print("\x1b[1;32m==> Repository dependencies: \x1b[0m")
  79. for _, repoD := range repoDeps {
  80. fmt.Print("\x1b[33m", repoD, " \x1b[0m")
  81. }
  82. fmt.Print("\n")
  83. }
  84. if len(aurDeps) != 0 {
  85. fmt.Print("\x1b[1;32m==> AUR dependencies: \x1b[0m")
  86. for _, aurD := range aurDeps {
  87. fmt.Print("\x1b[33m", aurD, " \x1b[0m")
  88. }
  89. fmt.Print("\n")
  90. }
  91. }
  92. // PrintInfo prints package info like pacman -Si.
  93. func PrintInfo(a *rpc.Pkg) {
  94. fmt.Println("\x1b[1;37mRepository :\x1b[0m", "aur")
  95. fmt.Println("\x1b[1;37mName :\x1b[0m", a.Name)
  96. fmt.Println("\x1b[1;37mVersion :\x1b[0m", a.Version)
  97. fmt.Println("\x1b[1;37mDescription :\x1b[0m", a.Description)
  98. if a.URL != "" {
  99. fmt.Println("\x1b[1;37mURL :\x1b[0m", a.URL)
  100. } else {
  101. fmt.Println("\x1b[1;37mURL :\x1b[0m", "None")
  102. }
  103. fmt.Println("\x1b[1;37mLicenses :\x1b[0m", a.License)
  104. // if len(a.Provides) != 0 {
  105. // fmt.Println("\x1b[1;37mProvides :\x1b[0m", a.Provides)
  106. // } else {
  107. // fmt.Println("\x1b[1;37mProvides :\x1b[0m", "None")
  108. // }
  109. if len(a.Depends) != 0 {
  110. fmt.Println("\x1b[1;37mDepends On :\x1b[0m", a.Depends)
  111. } else {
  112. fmt.Println("\x1b[1;37mDepends On :\x1b[0m", "None")
  113. }
  114. if len(a.MakeDepends) != 0 {
  115. fmt.Println("\x1b[1;37mMake depends On :\x1b[0m", a.MakeDepends)
  116. } else {
  117. fmt.Println("\x1b[1;37mMake depends On :\x1b[0m", "None")
  118. }
  119. if len(a.OptDepends) != 0 {
  120. fmt.Println("\x1b[1;37mOptional Deps :\x1b[0m", a.OptDepends)
  121. } else {
  122. fmt.Println("\x1b[1;37mOptional Deps :\x1b[0m", "None")
  123. }
  124. if len(a.Conflicts) != 0 {
  125. fmt.Println("\x1b[1;37mConflicts With :\x1b[0m", a.Conflicts)
  126. } else {
  127. fmt.Println("\x1b[1;37mConflicts With :\x1b[0m", "None")
  128. }
  129. if a.Maintainer != "" {
  130. fmt.Println("\x1b[1;37mMaintainer :\x1b[0m", a.Maintainer)
  131. } else {
  132. fmt.Println("\x1b[1;37mMaintainer :\x1b[0m", "None")
  133. }
  134. fmt.Println("\x1b[1;37mVotes :\x1b[0m", a.NumVotes)
  135. fmt.Println("\x1b[1;37mPopularity :\x1b[0m", a.Popularity)
  136. if a.OutOfDate != 0 {
  137. fmt.Println("\x1b[1;37mOut-of-date :\x1b[0m", "Yes")
  138. }
  139. }
  140. // BiggestPackages prints the name of the ten biggest packages in the system.
  141. func biggestPackages() {
  142. localDb, err := alpmHandle.LocalDb()
  143. if err != nil {
  144. return
  145. }
  146. pkgCache := localDb.PkgCache()
  147. pkgS := pkgCache.SortBySize().Slice()
  148. if len(pkgS) < 10 {
  149. return
  150. }
  151. for i := 0; i < 10; i++ {
  152. fmt.Printf("%s: \x1B[0;33m%s\x1B[0m\n", pkgS[i].Name(), human(pkgS[i].ISize()))
  153. }
  154. // Could implement size here as well, but we just want the general idea
  155. }