print.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. var q aurQuery
  223. var j int
  224. for i := len(remoteNames); i != 0; i = j {
  225. j = i - config.RequestSplitN
  226. if j < 0 {
  227. j = 0
  228. }
  229. qtemp, err := rpc.Info(remoteNames[j:i])
  230. q = append(q, qtemp...)
  231. if err != nil {
  232. return err
  233. }
  234. }
  235. var outcast []string
  236. for _, s := range remoteNames {
  237. found := false
  238. for _, i := range q {
  239. if s == i.Name {
  240. found = true
  241. break
  242. }
  243. }
  244. if !found {
  245. outcast = append(outcast, s)
  246. }
  247. }
  248. if err != nil {
  249. return err
  250. }
  251. for _, res := range q {
  252. if res.Maintainer == "" {
  253. fmt.Println(boldRedFgBlackBg(arrow+"Warning:"),
  254. boldYellowFgBlackBg(res.Name), whiteFgBlackBg("is orphaned"))
  255. }
  256. if res.OutOfDate != 0 {
  257. fmt.Println(boldRedFgBlackBg(arrow+"Warning:"),
  258. boldYellowFgBlackBg(res.Name), whiteFgBlackBg("is out-of-date in AUR"))
  259. }
  260. }
  261. for _, res := range outcast {
  262. fmt.Println(boldRedFgBlackBg(arrow+"Warning:"),
  263. boldYellowFgBlackBg(res), whiteFgBlackBg("is not available in AUR"))
  264. }
  265. return nil
  266. }
  267. //todo make pretty
  268. func printMissing(missing stringSet) {
  269. fmt.Print("Packages not found in repos or aur:")
  270. for pkg := range missing {
  271. fmt.Print(" ", pkg)
  272. }
  273. fmt.Println()
  274. }
  275. //todo make it less hacky
  276. func printNumberOfUpdates() error {
  277. old := os.Stdout // keep backup of the real stdout
  278. os.Stdout = nil
  279. aurUp, repoUp, err := upList()
  280. os.Stdout = old // restoring the real stdout
  281. if err != nil {
  282. return err
  283. }
  284. fmt.Println(len(aurUp) + len(repoUp))
  285. return nil
  286. }
  287. //todo make it less hacky
  288. func printUpdateList() error {
  289. old := os.Stdout // keep backup of the real stdout
  290. os.Stdout = nil
  291. aurUp, repoUp, err := upList()
  292. os.Stdout = old // restoring the real stdout
  293. if err != nil {
  294. return err
  295. }
  296. for _, pkg := range repoUp {
  297. fmt.Println(pkg.Name)
  298. }
  299. for _, pkg := range aurUp {
  300. fmt.Println(pkg.Name)
  301. }
  302. return nil
  303. }
  304. func blackBg(in string) string {
  305. if alpmConf.Options&alpm.ConfColor > 0 {
  306. return "\x1b[0;;40m" + in + "\x1b[0m"
  307. }
  308. return in
  309. }
  310. func redFg(in string) string {
  311. if alpmConf.Options&alpm.ConfColor > 0 {
  312. return "\x1b[0;31m" + in + "\x1b[0m"
  313. }
  314. return in
  315. }
  316. func greenFg(in string) string {
  317. if alpmConf.Options&alpm.ConfColor > 0 {
  318. return "\x1b[0;32m" + in + "\x1b[0m"
  319. }
  320. return in
  321. }
  322. func yellowFg(in string) string {
  323. if alpmConf.Options&alpm.ConfColor > 0 {
  324. return "\x1b[0;33m" + in + "\x1b[0m"
  325. }
  326. return in
  327. }
  328. func boldFg(in string) string {
  329. if alpmConf.Options&alpm.ConfColor > 0 {
  330. return "\x1b[1m" + in + "\x1b[0m"
  331. }
  332. return in
  333. }
  334. func boldGreenFg(in string) string {
  335. if alpmConf.Options&alpm.ConfColor > 0 {
  336. return "\x1b[1;32m" + in + "\x1b[0m"
  337. }
  338. return in
  339. }
  340. func boldYellowFg(in string) string {
  341. if alpmConf.Options&alpm.ConfColor > 0 {
  342. return "\x1b[1;33m" + in + "\x1b[0m"
  343. }
  344. return in
  345. }
  346. func boldBlueFg(in string) string {
  347. if alpmConf.Options&alpm.ConfColor > 0 {
  348. return "\x1b[1;34m" + in + "\x1b[0m"
  349. }
  350. return in
  351. }
  352. func boldCyanFg(in string) string {
  353. if alpmConf.Options&alpm.ConfColor > 0 {
  354. return "\x1b[1;36m" + in + "\x1b[0m"
  355. }
  356. return in
  357. }
  358. func boldWhiteFg(in string) string {
  359. if alpmConf.Options&alpm.ConfColor > 0 {
  360. return "\x1b[1;37m" + in + "\x1b[0m"
  361. }
  362. return in
  363. }
  364. func redFgBlackBg(in string) string {
  365. if alpmConf.Options&alpm.ConfColor > 0 {
  366. return "\x1b[0;31;40m" + in + "\x1b[0m"
  367. }
  368. return in
  369. }
  370. func greenFgBlackBg(in string) string {
  371. if alpmConf.Options&alpm.ConfColor > 0 {
  372. return "\x1b[0;32;40m" + in + "\x1b[0m"
  373. }
  374. return in
  375. }
  376. func whiteFgBlackBg(in string) string {
  377. if alpmConf.Options&alpm.ConfColor > 0 {
  378. return "\x1b[0;37;40m" + in + "\x1b[0m"
  379. }
  380. return in
  381. }
  382. func boldRedFgBlackBg(in string) string {
  383. if alpmConf.Options&alpm.ConfColor > 0 {
  384. return "\x1b[1;31;40m" + in + "\x1b[0m"
  385. }
  386. return in
  387. }
  388. func boldYellowFgBlackBg(in string) string {
  389. if alpmConf.Options&alpm.ConfColor > 0 {
  390. return "\x1b[1;33;40m" + in + "\x1b[0m"
  391. }
  392. return in
  393. }