print.go 9.7 KB

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