print.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. "strings"
  7. alpm "github.com/jguer/go-alpm"
  8. rpc "github.com/mikkeloscar/aur"
  9. )
  10. const warning = "\x1b[33mWarning:\x1b[0m "
  11. const arrow = "==>"
  12. // Human returns results in Human readable format.
  13. func human(size int64) string {
  14. floatsize := float32(size)
  15. units := [...]string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"}
  16. for _, unit := range units {
  17. if floatsize < 1024 {
  18. return fmt.Sprintf("%.1f %sB", floatsize, unit)
  19. }
  20. floatsize /= 1024
  21. }
  22. return fmt.Sprintf("%d%s", size, "B")
  23. }
  24. // PrintSearch handles printing search results in a given format
  25. func (q aurQuery) printSearch(start int) {
  26. localDb, _ := alpmHandle.LocalDb()
  27. for i, res := range q {
  28. var toprint string
  29. if config.SearchMode == NumberMenu {
  30. if config.SortMode == BottomUp {
  31. toprint += yellowFg(strconv.Itoa(len(q)+start-i-1) + " ")
  32. } else {
  33. toprint += yellowFg(strconv.Itoa(start+i) + " ")
  34. }
  35. } else if config.SearchMode == Minimal {
  36. fmt.Println(res.Name)
  37. continue
  38. }
  39. toprint += boldWhiteFg("aur/") + boldYellowFg(res.Name) +
  40. " " + boldCyanFg(res.Version) +
  41. " (" + strconv.Itoa(res.NumVotes) + ") "
  42. if res.Maintainer == "" {
  43. toprint += redFgBlackBg("(Orphaned)") + " "
  44. }
  45. if res.OutOfDate != 0 {
  46. toprint += redFgBlackBg("(Out-of-date)") + " "
  47. }
  48. if _, err := localDb.PkgByName(res.Name); err == nil {
  49. toprint += greenFgBlackBg("Installed")
  50. }
  51. toprint += "\n " + res.Description
  52. fmt.Println(toprint)
  53. }
  54. }
  55. //PrintSearch receives a RepoSearch type and outputs pretty text.
  56. func (s repoQuery) printSearch() {
  57. for i, res := range s {
  58. var toprint string
  59. if config.SearchMode == NumberMenu {
  60. if config.SortMode == BottomUp {
  61. toprint += yellowFg(strconv.Itoa(len(s)-i) + " ")
  62. } else {
  63. toprint += yellowFg(strconv.Itoa(i+1) + " ")
  64. }
  65. } else if config.SearchMode == Minimal {
  66. fmt.Println(res.Name())
  67. continue
  68. }
  69. toprint += boldWhiteFg(res.DB().Name()+"/") + boldYellowFg(res.Name()) +
  70. " " + boldCyanFg(res.Version()) + " "
  71. if len(res.Groups().Slice()) != 0 {
  72. toprint += fmt.Sprint(res.Groups().Slice(), " ")
  73. }
  74. localDb, err := alpmHandle.LocalDb()
  75. if err == nil {
  76. if _, err = localDb.PkgByName(res.Name()); err == nil {
  77. toprint += greenFgBlackBg("Installed")
  78. }
  79. }
  80. toprint += "\n " + res.Description()
  81. fmt.Println(toprint)
  82. }
  83. }
  84. func printDeps(repoDeps []string, aurDeps []string) {
  85. if len(repoDeps) != 0 {
  86. fmt.Print(boldGreenFg(arrow + " Repository dependencies: "))
  87. for _, repoD := range repoDeps {
  88. fmt.Print(yellowFg(repoD) + " ")
  89. }
  90. fmt.Print("\n")
  91. }
  92. if len(aurDeps) != 0 {
  93. fmt.Print(boldGreenFg(arrow + " AUR dependencies: "))
  94. for _, aurD := range aurDeps {
  95. fmt.Print(yellowFg(aurD) + " ")
  96. }
  97. fmt.Print("\n")
  98. }
  99. }
  100. // PrintInfo prints package info like pacman -Si.
  101. func PrintInfo(a *rpc.Pkg) {
  102. fmt.Println(boldWhiteFg("Repository :"), "aur")
  103. fmt.Println(boldWhiteFg("Name :"), a.Name)
  104. fmt.Println(boldWhiteFg("Version :"), a.Version)
  105. fmt.Println(boldWhiteFg("Description :"), a.Description)
  106. fmt.Println(boldWhiteFg("URL :"), a.URL)
  107. fmt.Println(boldWhiteFg("Licenses :"), strings.Join(a.License, " "))
  108. fmt.Println(boldWhiteFg("Depends On :"), strings.Join(a.Depends, " "))
  109. fmt.Println(boldWhiteFg("Make Deps :"), strings.Join(a.MakeDepends, " "))
  110. fmt.Println(boldWhiteFg("Optional Deps :"), strings.Join(a.OptDepends, " "))
  111. fmt.Println(boldWhiteFg("Conflicts With :"), strings.Join(a.Conflicts, " "))
  112. fmt.Println(boldWhiteFg("Maintainer :"), a.Maintainer)
  113. fmt.Println(boldWhiteFg("Votes :"), a.NumVotes)
  114. fmt.Println(boldWhiteFg("Popularity :"), a.Popularity)
  115. if a.OutOfDate != 0 {
  116. fmt.Println(boldWhiteFg("Out-of-date :"), "Yes")
  117. }
  118. fmt.Println()
  119. }
  120. // BiggestPackages prints the name of the ten biggest packages in the system.
  121. func biggestPackages() {
  122. localDb, err := alpmHandle.LocalDb()
  123. if err != nil {
  124. return
  125. }
  126. pkgCache := localDb.PkgCache()
  127. pkgS := pkgCache.SortBySize().Slice()
  128. if len(pkgS) < 10 {
  129. return
  130. }
  131. for i := 0; i < 10; i++ {
  132. fmt.Println(pkgS[i].Name() + ": " + yellowFg(human(pkgS[i].ISize())))
  133. }
  134. // Could implement size here as well, but we just want the general idea
  135. }
  136. // localStatistics prints installed packages statistics.
  137. func localStatistics() error {
  138. info, err := statistics()
  139. if err != nil {
  140. return err
  141. }
  142. _, _, _, remoteNames, err := filterPackages()
  143. if err != nil {
  144. return err
  145. }
  146. fmt.Printf("\n Yay version r%s\n", version)
  147. fmt.Println(boldCyanFg("==========================================="))
  148. fmt.Println(boldGreenFg("Total installed packages: ") + yellowFg(strconv.Itoa(info.Totaln)))
  149. fmt.Println(boldGreenFg("Total foreign installed packages: ") + yellowFg(strconv.Itoa(len(remoteNames))))
  150. fmt.Println(boldGreenFg("Explicitly installed packages: ") + yellowFg(strconv.Itoa(info.Expln)))
  151. fmt.Println(boldGreenFg("Total Size occupied by packages: ") + yellowFg(human(info.TotalSize)))
  152. fmt.Println(boldCyanFg("==========================================="))
  153. fmt.Println(boldGreenFg("Ten biggest packages"))
  154. biggestPackages()
  155. fmt.Println(boldCyanFg("==========================================="))
  156. var q aurQuery
  157. var j int
  158. for i := len(remoteNames); i != 0; i = j {
  159. j = i - config.RequestSplitN
  160. if j < 0 {
  161. j = 0
  162. }
  163. qtemp, err := rpc.Info(remoteNames[j:i])
  164. q = append(q, qtemp...)
  165. if err != nil {
  166. return err
  167. }
  168. }
  169. var outcast []string
  170. for _, s := range remoteNames {
  171. found := false
  172. for _, i := range q {
  173. if s == i.Name {
  174. found = true
  175. break
  176. }
  177. }
  178. if !found {
  179. outcast = append(outcast, s)
  180. }
  181. }
  182. if err != nil {
  183. return err
  184. }
  185. for _, res := range q {
  186. if res.Maintainer == "" {
  187. fmt.Println(boldRedFgBlackBg(arrow+"Warning:"),
  188. boldYellowFgBlackBg(res.Name), whiteFgBlackBg("is orphaned"))
  189. }
  190. if res.OutOfDate != 0 {
  191. fmt.Println(boldRedFgBlackBg(arrow+"Warning:"),
  192. boldYellowFgBlackBg(res.Name), whiteFgBlackBg("is out-of-date in AUR"))
  193. }
  194. }
  195. for _, res := range outcast {
  196. fmt.Println(boldRedFgBlackBg(arrow+"Warning:"),
  197. boldYellowFgBlackBg(res), whiteFgBlackBg("is not available in AUR"))
  198. }
  199. return nil
  200. }
  201. //todo make pretty
  202. func printMissing(missing stringSet) {
  203. fmt.Print("Packages not found in repos or aur:")
  204. for pkg := range missing {
  205. fmt.Print(" ", pkg)
  206. }
  207. fmt.Println()
  208. }
  209. //todo make it less hacky
  210. func printNumberOfUpdates() error {
  211. old := os.Stdout // keep backup of the real stdout
  212. os.Stdout = nil
  213. aurUp, repoUp, err := upList()
  214. os.Stdout = old // restoring the real stdout
  215. if err != nil {
  216. return err
  217. }
  218. fmt.Println(len(aurUp) + len(repoUp))
  219. return nil
  220. }
  221. //todo make it less hacky
  222. func printUpdateList() error {
  223. old := os.Stdout // keep backup of the real stdout
  224. os.Stdout = nil
  225. aurUp, repoUp, err := upList()
  226. os.Stdout = old // restoring the real stdout
  227. if err != nil {
  228. return err
  229. }
  230. for _, pkg := range repoUp {
  231. fmt.Println(pkg.Name)
  232. }
  233. for _, pkg := range aurUp {
  234. fmt.Println(pkg.Name)
  235. }
  236. return nil
  237. }
  238. func blackBg(in string) string {
  239. if alpmConf.Options&alpm.ConfColor > 0 {
  240. return "\x1b[0;;40m" + in + "\x1b[0m"
  241. }
  242. return in
  243. }
  244. func redFg(in string) string {
  245. if alpmConf.Options&alpm.ConfColor > 0 {
  246. return "\x1b[0;31m" + in + "\x1b[0m"
  247. }
  248. return in
  249. }
  250. func yellowFg(in string) string {
  251. if alpmConf.Options&alpm.ConfColor > 0 {
  252. return "\x1b[0;33m" + in + "\x1b[0m"
  253. }
  254. return in
  255. }
  256. func boldGreenFg(in string) string {
  257. if alpmConf.Options&alpm.ConfColor > 0 {
  258. return "\x1b[1;32m" + in + "\x1b[0m"
  259. }
  260. return in
  261. }
  262. func boldYellowFg(in string) string {
  263. if alpmConf.Options&alpm.ConfColor > 0 {
  264. return "\x1b[1;33m" + in + "\x1b[0m"
  265. }
  266. return in
  267. }
  268. func boldCyanFg(in string) string {
  269. if alpmConf.Options&alpm.ConfColor > 0 {
  270. return "\x1b[1;36m" + in + "\x1b[0m"
  271. }
  272. return in
  273. }
  274. func boldWhiteFg(in string) string {
  275. if alpmConf.Options&alpm.ConfColor > 0 {
  276. return "\x1b[1;37m" + in + "\x1b[0m"
  277. }
  278. return in
  279. }
  280. func redFgBlackBg(in string) string {
  281. if alpmConf.Options&alpm.ConfColor > 0 {
  282. return "\x1b[31;40m" + in + "\x1b[0m"
  283. }
  284. return in
  285. }
  286. func greenFgBlackBg(in string) string {
  287. if alpmConf.Options&alpm.ConfColor > 0 {
  288. return "\x1b[32;40m" + in + "\x1b[0m"
  289. }
  290. return in
  291. }
  292. func whiteFgBlackBg(in string) string {
  293. if alpmConf.Options&alpm.ConfColor > 0 {
  294. return "\x1b[0;37;40m" + in + "\x1b[0m"
  295. }
  296. return in
  297. }
  298. func boldRedFgBlackBg(in string) string {
  299. if alpmConf.Options&alpm.ConfColor > 0 {
  300. return "\x1b[1;31;40m" + in + "\x1b[0m"
  301. }
  302. return in
  303. }
  304. func boldYellowFgBlackBg(in string) string {
  305. if alpmConf.Options&alpm.ConfColor > 0 {
  306. return "\x1b[1;33;40m" + in + "\x1b[0m"
  307. }
  308. return in
  309. }