print.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. return
  47. }
  48. //PrintSearch receives a RepoSearch type and outputs pretty text.
  49. func (s repoQuery) printSearch() {
  50. for i, res := range s {
  51. var toprint string
  52. if config.SearchMode == NumberMenu {
  53. if config.SortMode == BottomUp {
  54. toprint += fmt.Sprintf("\x1b[33m%d\x1b[0m ", len(s)-i-1)
  55. } else {
  56. toprint += fmt.Sprintf("\x1b[33m%d\x1b[0m ", i)
  57. }
  58. } else if config.SearchMode == Minimal {
  59. fmt.Println(res.Name())
  60. continue
  61. }
  62. toprint += fmt.Sprintf("\x1b[1m%s/\x1b[33m%s \x1b[36m%s \x1b[0m",
  63. res.DB().Name(), res.Name(), res.Version())
  64. if len(res.Groups().Slice()) != 0 {
  65. toprint += fmt.Sprint(res.Groups().Slice(), " ")
  66. }
  67. localDb, err := AlpmHandle.LocalDb()
  68. if err == nil {
  69. if _, err = localDb.PkgByName(res.Name()); err == nil {
  70. toprint += fmt.Sprintf("\x1b[32;40mInstalled\x1b[0m")
  71. }
  72. }
  73. toprint += "\n " + res.Description()
  74. fmt.Println(toprint)
  75. }
  76. }
  77. func printDeps(repoDeps []string, aurDeps []string) {
  78. if len(repoDeps) != 0 {
  79. fmt.Print("\x1b[1;32m==> Repository dependencies: \x1b[0m")
  80. for _, repoD := range repoDeps {
  81. fmt.Print("\x1b[33m", repoD, " \x1b[0m")
  82. }
  83. fmt.Print("\n")
  84. }
  85. if len(aurDeps) != 0 {
  86. fmt.Print("\x1b[1;32m==> AUR dependencies: \x1b[0m")
  87. for _, aurD := range aurDeps {
  88. fmt.Print("\x1b[33m", aurD, " \x1b[0m")
  89. }
  90. fmt.Print("\n")
  91. }
  92. }
  93. // PrintInfo prints package info like pacman -Si.
  94. func PrintInfo(a *rpc.Pkg) {
  95. fmt.Println("\x1b[1;37mRepository :\x1b[0m", "aur")
  96. fmt.Println("\x1b[1;37mName :\x1b[0m", a.Name)
  97. fmt.Println("\x1b[1;37mVersion :\x1b[0m", a.Version)
  98. fmt.Println("\x1b[1;37mDescription :\x1b[0m", a.Description)
  99. if a.URL != "" {
  100. fmt.Println("\x1b[1;37mURL :\x1b[0m", a.URL)
  101. } else {
  102. fmt.Println("\x1b[1;37mURL :\x1b[0m", "None")
  103. }
  104. fmt.Println("\x1b[1;37mLicenses :\x1b[0m", a.License)
  105. // if len(a.Provides) != 0 {
  106. // fmt.Println("\x1b[1;37mProvides :\x1b[0m", a.Provides)
  107. // } else {
  108. // fmt.Println("\x1b[1;37mProvides :\x1b[0m", "None")
  109. // }
  110. if len(a.Depends) != 0 {
  111. fmt.Println("\x1b[1;37mDepends On :\x1b[0m", a.Depends)
  112. } else {
  113. fmt.Println("\x1b[1;37mDepends On :\x1b[0m", "None")
  114. }
  115. if len(a.MakeDepends) != 0 {
  116. fmt.Println("\x1b[1;37mMake depends On :\x1b[0m", a.MakeDepends)
  117. } else {
  118. fmt.Println("\x1b[1;37mMake depends On :\x1b[0m", "None")
  119. }
  120. if len(a.OptDepends) != 0 {
  121. fmt.Println("\x1b[1;37mOptional Deps :\x1b[0m", a.OptDepends)
  122. } else {
  123. fmt.Println("\x1b[1;37mOptional Deps :\x1b[0m", "None")
  124. }
  125. if len(a.Conflicts) != 0 {
  126. fmt.Println("\x1b[1;37mConflicts With :\x1b[0m", a.Conflicts)
  127. } else {
  128. fmt.Println("\x1b[1;37mConflicts With :\x1b[0m", "None")
  129. }
  130. if a.Maintainer != "" {
  131. fmt.Println("\x1b[1;37mMaintainer :\x1b[0m", a.Maintainer)
  132. } else {
  133. fmt.Println("\x1b[1;37mMaintainer :\x1b[0m", "None")
  134. }
  135. fmt.Println("\x1b[1;37mVotes :\x1b[0m", a.NumVotes)
  136. fmt.Println("\x1b[1;37mPopularity :\x1b[0m", a.Popularity)
  137. if a.OutOfDate != 0 {
  138. fmt.Println("\x1b[1;37mOut-of-date :\x1b[0m", "Yes")
  139. }
  140. }
  141. // BiggestPackages prints the name of the ten biggest packages in the system.
  142. func biggestPackages() {
  143. localDb, err := AlpmHandle.LocalDb()
  144. if err != nil {
  145. return
  146. }
  147. pkgCache := localDb.PkgCache()
  148. pkgS := pkgCache.SortBySize().Slice()
  149. if len(pkgS) < 10 {
  150. return
  151. }
  152. for i := 0; i < 10; i++ {
  153. fmt.Printf("%s: \x1B[0;33m%s\x1B[0m\n", pkgS[i].Name(), human(pkgS[i].ISize()))
  154. }
  155. // Could implement size here as well, but we just want the general idea
  156. }