print.go 9.6 KB

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