print.go 8.9 KB

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