print.go 9.7 KB

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