print.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. // printDownloadsFromRepo prints repository packages to be downloaded
  84. func printDownloadsFromRepo(repoType string, repo []*alpm.Package) {
  85. var packages string
  86. for _, v := range repo {
  87. packages += v.Name() + " "
  88. }
  89. if len(repo) > 0 {
  90. printDownloads(repoType, packages, len(repo))
  91. }
  92. }
  93. // printDownloadsFromAur prints AUR packages to be downloaded
  94. func printDownloadsFromAur(repoType string, repo []*rpc.Pkg) {
  95. var packages string
  96. for _, v := range repo {
  97. packages += v.Name + " "
  98. }
  99. if len(repo) > 0 {
  100. printDownloads(repoType, packages, len(repo))
  101. }
  102. }
  103. func printDownloads(repoType, packages string, numPackages int) {
  104. fmt.Println(
  105. redFg("["+repoType+", "+strconv.Itoa(numPackages)+" packages] ") +
  106. yellowFg(packages))
  107. }
  108. func printDeps(repoDeps []string, aurDeps []string) {
  109. if len(repoDeps) != 0 {
  110. fmt.Print(boldGreenFg(arrow + " Repository dependencies: "))
  111. for _, repoD := range repoDeps {
  112. fmt.Print(yellowFg(repoD) + " ")
  113. }
  114. fmt.Print("\n")
  115. }
  116. if len(aurDeps) != 0 {
  117. fmt.Print(boldGreenFg(arrow + " AUR dependencies: "))
  118. for _, aurD := range aurDeps {
  119. fmt.Print(yellowFg(aurD) + " ")
  120. }
  121. fmt.Print("\n")
  122. }
  123. }
  124. // PrintInfo prints package info like pacman -Si.
  125. func PrintInfo(a *rpc.Pkg) {
  126. fmt.Println(boldWhiteFg("Repository :"), "aur")
  127. fmt.Println(boldWhiteFg("Name :"), a.Name)
  128. fmt.Println(boldWhiteFg("Version :"), a.Version)
  129. fmt.Println(boldWhiteFg("Description :"), a.Description)
  130. fmt.Println(boldWhiteFg("URL :"), a.URL)
  131. fmt.Println(boldWhiteFg("Licenses :"), strings.Join(a.License, " "))
  132. fmt.Println(boldWhiteFg("Depends On :"), strings.Join(a.Depends, " "))
  133. fmt.Println(boldWhiteFg("Make Deps :"), strings.Join(a.MakeDepends, " "))
  134. fmt.Println(boldWhiteFg("Optional Deps :"), strings.Join(a.OptDepends, " "))
  135. fmt.Println(boldWhiteFg("Conflicts With :"), strings.Join(a.Conflicts, " "))
  136. fmt.Println(boldWhiteFg("Maintainer :"), a.Maintainer)
  137. fmt.Println(boldWhiteFg("Votes :"), a.NumVotes)
  138. fmt.Println(boldWhiteFg("Popularity :"), a.Popularity)
  139. if a.OutOfDate != 0 {
  140. fmt.Println(boldWhiteFg("Out-of-date :"), "Yes")
  141. }
  142. fmt.Println()
  143. }
  144. // BiggestPackages prints the name of the ten biggest packages in the system.
  145. func biggestPackages() {
  146. localDb, err := alpmHandle.LocalDb()
  147. if err != nil {
  148. return
  149. }
  150. pkgCache := localDb.PkgCache()
  151. pkgS := pkgCache.SortBySize().Slice()
  152. if len(pkgS) < 10 {
  153. return
  154. }
  155. for i := 0; i < 10; i++ {
  156. fmt.Println(pkgS[i].Name() + ": " + yellowFg(human(pkgS[i].ISize())))
  157. }
  158. // Could implement size here as well, but we just want the general idea
  159. }
  160. // localStatistics prints installed packages statistics.
  161. func localStatistics() error {
  162. info, err := statistics()
  163. if err != nil {
  164. return err
  165. }
  166. _, _, _, remoteNames, err := filterPackages()
  167. if err != nil {
  168. return err
  169. }
  170. fmt.Printf("\n Yay version r%s\n", version)
  171. fmt.Println(boldCyanFg("==========================================="))
  172. fmt.Println(boldGreenFg("Total installed packages: ") + yellowFg(strconv.Itoa(info.Totaln)))
  173. fmt.Println(boldGreenFg("Total foreign installed packages: ") + yellowFg(strconv.Itoa(len(remoteNames))))
  174. fmt.Println(boldGreenFg("Explicitly installed packages: ") + yellowFg(strconv.Itoa(info.Expln)))
  175. fmt.Println(boldGreenFg("Total Size occupied by packages: ") + yellowFg(human(info.TotalSize)))
  176. fmt.Println(boldCyanFg("==========================================="))
  177. fmt.Println(boldGreenFg("Ten biggest packages"))
  178. biggestPackages()
  179. fmt.Println(boldCyanFg("==========================================="))
  180. var q aurQuery
  181. var j int
  182. for i := len(remoteNames); i != 0; i = j {
  183. j = i - config.RequestSplitN
  184. if j < 0 {
  185. j = 0
  186. }
  187. qtemp, err := rpc.Info(remoteNames[j:i])
  188. q = append(q, qtemp...)
  189. if err != nil {
  190. return err
  191. }
  192. }
  193. var outcast []string
  194. for _, s := range remoteNames {
  195. found := false
  196. for _, i := range q {
  197. if s == i.Name {
  198. found = true
  199. break
  200. }
  201. }
  202. if !found {
  203. outcast = append(outcast, s)
  204. }
  205. }
  206. if err != nil {
  207. return err
  208. }
  209. for _, res := range q {
  210. if res.Maintainer == "" {
  211. fmt.Println(boldRedFgBlackBg(arrow+"Warning:"),
  212. boldYellowFgBlackBg(res.Name), whiteFgBlackBg("is orphaned"))
  213. }
  214. if res.OutOfDate != 0 {
  215. fmt.Println(boldRedFgBlackBg(arrow+"Warning:"),
  216. boldYellowFgBlackBg(res.Name), whiteFgBlackBg("is out-of-date in AUR"))
  217. }
  218. }
  219. for _, res := range outcast {
  220. fmt.Println(boldRedFgBlackBg(arrow+"Warning:"),
  221. boldYellowFgBlackBg(res), whiteFgBlackBg("is not available in AUR"))
  222. }
  223. return nil
  224. }
  225. //todo make pretty
  226. func printMissing(missing stringSet) {
  227. fmt.Print("Packages not found in repos or aur:")
  228. for pkg := range missing {
  229. fmt.Print(" ", pkg)
  230. }
  231. fmt.Println()
  232. }
  233. //todo make it less hacky
  234. func printNumberOfUpdates() error {
  235. old := os.Stdout // keep backup of the real stdout
  236. os.Stdout = nil
  237. aurUp, repoUp, err := upList()
  238. os.Stdout = old // restoring the real stdout
  239. if err != nil {
  240. return err
  241. }
  242. fmt.Println(len(aurUp) + len(repoUp))
  243. return nil
  244. }
  245. //todo make it less hacky
  246. func printUpdateList() error {
  247. old := os.Stdout // keep backup of the real stdout
  248. os.Stdout = nil
  249. aurUp, repoUp, err := upList()
  250. os.Stdout = old // restoring the real stdout
  251. if err != nil {
  252. return err
  253. }
  254. for _, pkg := range repoUp {
  255. fmt.Println(pkg.Name)
  256. }
  257. for _, pkg := range aurUp {
  258. fmt.Println(pkg.Name)
  259. }
  260. return nil
  261. }
  262. func blackBg(in string) string {
  263. if alpmConf.Options&alpm.ConfColor > 0 {
  264. return "\x1b[0;;40m" + in + "\x1b[0m"
  265. }
  266. return in
  267. }
  268. func redFg(in string) string {
  269. if alpmConf.Options&alpm.ConfColor > 0 {
  270. return "\x1b[0;31m" + in + "\x1b[0m"
  271. }
  272. return in
  273. }
  274. func greenFg(in string) string {
  275. if alpmConf.Options&alpm.ConfColor > 0 {
  276. return "\x1b[0;32m" + in + "\x1b[0m"
  277. }
  278. return in
  279. }
  280. func yellowFg(in string) string {
  281. if alpmConf.Options&alpm.ConfColor > 0 {
  282. return "\x1b[0;33m" + in + "\x1b[0m"
  283. }
  284. return in
  285. }
  286. func boldFg(in string) string {
  287. if alpmConf.Options&alpm.ConfColor > 0 {
  288. return "\x1b[1m" + in + "\x1b[0m"
  289. }
  290. return in
  291. }
  292. func boldGreenFg(in string) string {
  293. if alpmConf.Options&alpm.ConfColor > 0 {
  294. return "\x1b[1;32m" + in + "\x1b[0m"
  295. }
  296. return in
  297. }
  298. func boldYellowFg(in string) string {
  299. if alpmConf.Options&alpm.ConfColor > 0 {
  300. return "\x1b[1;33m" + in + "\x1b[0m"
  301. }
  302. return in
  303. }
  304. func boldBlueFg(in string) string {
  305. if alpmConf.Options&alpm.ConfColor > 0 {
  306. return "\x1b[1;34m" + in + "\x1b[0m"
  307. }
  308. return in
  309. }
  310. func boldCyanFg(in string) string {
  311. if alpmConf.Options&alpm.ConfColor > 0 {
  312. return "\x1b[1;36m" + in + "\x1b[0m"
  313. }
  314. return in
  315. }
  316. func boldWhiteFg(in string) string {
  317. if alpmConf.Options&alpm.ConfColor > 0 {
  318. return "\x1b[1;37m" + in + "\x1b[0m"
  319. }
  320. return in
  321. }
  322. func redFgBlackBg(in string) string {
  323. if alpmConf.Options&alpm.ConfColor > 0 {
  324. return "\x1b[0;31;40m" + in + "\x1b[0m"
  325. }
  326. return in
  327. }
  328. func greenFgBlackBg(in string) string {
  329. if alpmConf.Options&alpm.ConfColor > 0 {
  330. return "\x1b[0;32;40m" + in + "\x1b[0m"
  331. }
  332. return in
  333. }
  334. func whiteFgBlackBg(in string) string {
  335. if alpmConf.Options&alpm.ConfColor > 0 {
  336. return "\x1b[0;37;40m" + in + "\x1b[0m"
  337. }
  338. return in
  339. }
  340. func boldRedFgBlackBg(in string) string {
  341. if alpmConf.Options&alpm.ConfColor > 0 {
  342. return "\x1b[1;31;40m" + in + "\x1b[0m"
  343. }
  344. return in
  345. }
  346. func boldYellowFgBlackBg(in string) string {
  347. if alpmConf.Options&alpm.ConfColor > 0 {
  348. return "\x1b[1;33;40m" + in + "\x1b[0m"
  349. }
  350. return in
  351. }