print.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. func printDeps(repoDeps []string, aurDeps []string) {
  163. if len(repoDeps) != 0 {
  164. fmt.Print(boldGreenFg(arrow + " Repository dependencies: "))
  165. for _, repoD := range repoDeps {
  166. fmt.Print(yellowFg(repoD) + " ")
  167. }
  168. fmt.Print("\n")
  169. }
  170. if len(aurDeps) != 0 {
  171. fmt.Print(boldGreenFg(arrow + " AUR dependencies: "))
  172. for _, aurD := range aurDeps {
  173. fmt.Print(yellowFg(aurD) + " ")
  174. }
  175. fmt.Print("\n")
  176. }
  177. }
  178. // PrintInfo prints package info like pacman -Si.
  179. func PrintInfo(a *rpc.Pkg) {
  180. fmt.Println(boldWhiteFg("Repository :"), "aur")
  181. fmt.Println(boldWhiteFg("Name :"), a.Name)
  182. fmt.Println(boldWhiteFg("Version :"), a.Version)
  183. fmt.Println(boldWhiteFg("Description :"), a.Description)
  184. fmt.Println(boldWhiteFg("URL :"), a.URL)
  185. fmt.Println(boldWhiteFg("Licenses :"), strings.Join(a.License, " "))
  186. fmt.Println(boldWhiteFg("Depends On :"), strings.Join(a.Depends, " "))
  187. fmt.Println(boldWhiteFg("Make Deps :"), strings.Join(a.MakeDepends, " "))
  188. fmt.Println(boldWhiteFg("Check Deps :"), strings.Join(a.CheckDepends, " "))
  189. fmt.Println(boldWhiteFg("Optional Deps :"), strings.Join(a.OptDepends, " "))
  190. fmt.Println(boldWhiteFg("Conflicts With :"), strings.Join(a.Conflicts, " "))
  191. fmt.Println(boldWhiteFg("Maintainer :"), a.Maintainer)
  192. fmt.Println(boldWhiteFg("Votes :"), a.NumVotes)
  193. fmt.Println(boldWhiteFg("Popularity :"), a.Popularity)
  194. if a.OutOfDate != 0 {
  195. fmt.Println(boldWhiteFg("Out-of-date :"), "Yes")
  196. }
  197. fmt.Println()
  198. }
  199. // BiggestPackages prints the name of the ten biggest packages in the system.
  200. func biggestPackages() {
  201. localDb, err := alpmHandle.LocalDb()
  202. if err != nil {
  203. return
  204. }
  205. pkgCache := localDb.PkgCache()
  206. pkgS := pkgCache.SortBySize().Slice()
  207. if len(pkgS) < 10 {
  208. return
  209. }
  210. for i := 0; i < 10; i++ {
  211. fmt.Println(pkgS[i].Name() + ": " + yellowFg(human(pkgS[i].ISize())))
  212. }
  213. // Could implement size here as well, but we just want the general idea
  214. }
  215. // localStatistics prints installed packages statistics.
  216. func localStatistics() error {
  217. info, err := statistics()
  218. if err != nil {
  219. return err
  220. }
  221. _, _, _, remoteNames, err := filterPackages()
  222. if err != nil {
  223. return err
  224. }
  225. fmt.Printf("\n Yay version r%s\n", version)
  226. fmt.Println(boldCyanFg("==========================================="))
  227. fmt.Println(boldGreenFg("Total installed packages: ") + yellowFg(strconv.Itoa(info.Totaln)))
  228. fmt.Println(boldGreenFg("Total foreign installed packages: ") + yellowFg(strconv.Itoa(len(remoteNames))))
  229. fmt.Println(boldGreenFg("Explicitly installed packages: ") + yellowFg(strconv.Itoa(info.Expln)))
  230. fmt.Println(boldGreenFg("Total Size occupied by packages: ") + yellowFg(human(info.TotalSize)))
  231. fmt.Println(boldCyanFg("==========================================="))
  232. fmt.Println(boldGreenFg("Ten biggest packages"))
  233. biggestPackages()
  234. fmt.Println(boldCyanFg("==========================================="))
  235. aurInfo(remoteNames)
  236. return nil
  237. }
  238. //todo make pretty
  239. func printMissing(missing stringSet) {
  240. fmt.Print("Packages not found in repos or aur:")
  241. for pkg := range missing {
  242. fmt.Print(" ", pkg)
  243. }
  244. fmt.Println()
  245. }
  246. //todo make it less hacky
  247. func printNumberOfUpdates() error {
  248. //todo
  249. old := os.Stdout // keep backup of the real stdout
  250. os.Stdout = nil
  251. _, _, localNames, remoteNames, err := filterPackages()
  252. dt, _ := getDepTree(append(localNames, remoteNames...))
  253. aurUp, repoUp, err := upList(dt)
  254. os.Stdout = old // restoring the real stdout
  255. if err != nil {
  256. return err
  257. }
  258. fmt.Println(len(aurUp) + len(repoUp))
  259. return nil
  260. }
  261. //todo make it less hacky
  262. func printUpdateList() error {
  263. old := os.Stdout // keep backup of the real stdout
  264. os.Stdout = nil
  265. _, _, localNames, remoteNames, err := filterPackages()
  266. dt, _ := getDepTree(append(localNames, remoteNames...))
  267. aurUp, repoUp, err := upList(dt)
  268. os.Stdout = old // restoring the real stdout
  269. if err != nil {
  270. return err
  271. }
  272. for _, pkg := range repoUp {
  273. fmt.Println(pkg.Name)
  274. }
  275. for _, pkg := range aurUp {
  276. fmt.Println(pkg.Name)
  277. }
  278. return nil
  279. }
  280. func blackBg(in string) string {
  281. if alpmConf.Options&alpm.ConfColor > 0 {
  282. return "\x1b[0;;40m" + in + "\x1b[0m"
  283. }
  284. return in
  285. }
  286. func redFg(in string) string {
  287. if alpmConf.Options&alpm.ConfColor > 0 {
  288. return "\x1b[0;31m" + in + "\x1b[0m"
  289. }
  290. return in
  291. }
  292. func greenFg(in string) string {
  293. if alpmConf.Options&alpm.ConfColor > 0 {
  294. return "\x1b[0;32m" + in + "\x1b[0m"
  295. }
  296. return in
  297. }
  298. func yellowFg(in string) string {
  299. if alpmConf.Options&alpm.ConfColor > 0 {
  300. return "\x1b[0;33m" + in + "\x1b[0m"
  301. }
  302. return in
  303. }
  304. func boldFg(in string) string {
  305. if alpmConf.Options&alpm.ConfColor > 0 {
  306. return "\x1b[1m" + in + "\x1b[0m"
  307. }
  308. return in
  309. }
  310. func boldGreenFg(in string) string {
  311. if alpmConf.Options&alpm.ConfColor > 0 {
  312. return "\x1b[1;32m" + in + "\x1b[0m"
  313. }
  314. return in
  315. }
  316. func boldYellowFg(in string) string {
  317. if alpmConf.Options&alpm.ConfColor > 0 {
  318. return "\x1b[1;33m" + in + "\x1b[0m"
  319. }
  320. return in
  321. }
  322. func boldBlueFg(in string) string {
  323. if alpmConf.Options&alpm.ConfColor > 0 {
  324. return "\x1b[1;34m" + in + "\x1b[0m"
  325. }
  326. return in
  327. }
  328. func boldCyanFg(in string) string {
  329. if alpmConf.Options&alpm.ConfColor > 0 {
  330. return "\x1b[1;36m" + in + "\x1b[0m"
  331. }
  332. return in
  333. }
  334. func boldWhiteFg(in string) string {
  335. if alpmConf.Options&alpm.ConfColor > 0 {
  336. return "\x1b[1;37m" + in + "\x1b[0m"
  337. }
  338. return in
  339. }
  340. func redFgBlackBg(in string) string {
  341. if alpmConf.Options&alpm.ConfColor > 0 {
  342. return "\x1b[0;31;40m" + in + "\x1b[0m"
  343. }
  344. return in
  345. }
  346. func greenFgBlackBg(in string) string {
  347. if alpmConf.Options&alpm.ConfColor > 0 {
  348. return "\x1b[0;32;40m" + in + "\x1b[0m"
  349. }
  350. return in
  351. }
  352. func whiteFgBlackBg(in string) string {
  353. if alpmConf.Options&alpm.ConfColor > 0 {
  354. return "\x1b[0;37;40m" + in + "\x1b[0m"
  355. }
  356. return in
  357. }
  358. func boldRedFgBlackBg(in string) string {
  359. if alpmConf.Options&alpm.ConfColor > 0 {
  360. return "\x1b[1;31;40m" + in + "\x1b[0m"
  361. }
  362. return in
  363. }
  364. func boldYellowFgBlackBg(in string) string {
  365. if alpmConf.Options&alpm.ConfColor > 0 {
  366. return "\x1b[1;33;40m" + in + "\x1b[0m"
  367. }
  368. return in
  369. }