print.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. "strings"
  7. "time"
  8. alpm "github.com/jguer/go-alpm"
  9. rpc "github.com/mikkeloscar/aur"
  10. )
  11. const arrow = "==>"
  12. // Human returns results in Human readable format.
  13. func human(size int64) string {
  14. floatsize := float32(size)
  15. units := [...]string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"}
  16. for _, unit := range units {
  17. if floatsize < 1024 {
  18. return fmt.Sprintf("%.1f %sB", floatsize, unit)
  19. }
  20. floatsize /= 1024
  21. }
  22. return fmt.Sprintf("%d%s", size, "B")
  23. }
  24. // PrintSearch handles printing search results in a given format
  25. func (q aurQuery) printSearch(start int) {
  26. localDb, _ := alpmHandle.LocalDb()
  27. for i, res := range q {
  28. var toprint string
  29. if config.SearchMode == NumberMenu {
  30. if config.SortMode == BottomUp {
  31. toprint += magenta(strconv.Itoa(len(q)+start-i-1) + " ")
  32. } else {
  33. toprint += magenta(strconv.Itoa(start+i) + " ")
  34. }
  35. } else if config.SearchMode == Minimal {
  36. fmt.Println(res.Name)
  37. continue
  38. }
  39. toprint += bold(colourHash("aur")) + "/" + bold(res.Name) +
  40. " " + cyan(res.Version) +
  41. bold(" (+"+strconv.Itoa(res.NumVotes)) +
  42. " " + bold(strconv.FormatFloat(res.Popularity, 'f', 2, 64)+"%) ")
  43. if res.Maintainer == "" {
  44. toprint += bold(red("(Orphaned)")) + " "
  45. }
  46. if res.OutOfDate != 0 {
  47. toprint += bold(red("(Out-of-date "+formatTime(res.OutOfDate)+")")) + " "
  48. }
  49. if _, err := localDb.PkgByName(res.Name); err == nil {
  50. toprint += bold(green("(Installed)"))
  51. }
  52. toprint += "\n " + res.Description
  53. fmt.Println(toprint)
  54. }
  55. }
  56. //PrintSearch receives a RepoSearch type and outputs pretty text.
  57. func (s repoQuery) printSearch() {
  58. for i, res := range s {
  59. var toprint string
  60. if config.SearchMode == NumberMenu {
  61. if config.SortMode == BottomUp {
  62. toprint += magenta(strconv.Itoa(len(s)-i) + " ")
  63. } else {
  64. toprint += magenta(strconv.Itoa(i+1) + " ")
  65. }
  66. } else if config.SearchMode == Minimal {
  67. fmt.Println(res.Name())
  68. continue
  69. }
  70. toprint += bold(colourHash(res.DB().Name())) + "/" + bold(res.Name()) +
  71. " " + cyan(res.Version()) +
  72. bold(" ("+human(res.Size())+
  73. " "+human(res.ISize())+") ")
  74. if len(res.Groups().Slice()) != 0 {
  75. toprint += fmt.Sprint(res.Groups().Slice(), " ")
  76. }
  77. localDb, err := alpmHandle.LocalDb()
  78. if err == nil {
  79. if _, err = localDb.PkgByName(res.Name()); err == nil {
  80. toprint += bold(green("(Installed)"))
  81. }
  82. }
  83. toprint += "\n " + res.Description()
  84. fmt.Println(toprint)
  85. }
  86. }
  87. func formatPkgbase(pkg *rpc.Pkg, bases map[string][]*rpc.Pkg) string {
  88. str := pkg.PackageBase
  89. if len(bases[pkg.PackageBase]) > 1 || pkg.PackageBase != pkg.Name {
  90. str2 := " ("
  91. for _, split := range bases[pkg.PackageBase] {
  92. str2 += split.Name + " "
  93. }
  94. str2 = str2[:len(str2)-1] + ")"
  95. str += str2
  96. }
  97. return str
  98. }
  99. // printDownloadsFromRepo prints repository packages to be downloaded
  100. func printDepCatagories(dc *depCatagories) {
  101. repo := ""
  102. repoMake := ""
  103. aur := ""
  104. aurMake := ""
  105. repoLen := 0
  106. repoMakeLen := 0
  107. aurLen := 0
  108. aurMakeLen := 0
  109. for _, pkg := range dc.Repo {
  110. if dc.MakeOnly.get(pkg.Name()) {
  111. repoMake += " " + pkg.Name()
  112. repoMakeLen++
  113. } else {
  114. repo += " " + pkg.Name()
  115. repoLen++
  116. }
  117. }
  118. for _, pkg := range dc.Aur {
  119. pkgStr := " " + pkg.PackageBase
  120. pkgStrMake := pkgStr
  121. push := false
  122. pushMake := false
  123. if len(dc.Bases[pkg.PackageBase]) > 1 || pkg.PackageBase != pkg.Name {
  124. pkgStr += " ("
  125. pkgStrMake += " ("
  126. for _, split := range dc.Bases[pkg.PackageBase] {
  127. if dc.MakeOnly.get(split.Name) {
  128. pkgStrMake += split.Name + " "
  129. aurMakeLen++
  130. pushMake = true
  131. } else {
  132. pkgStr += split.Name + " "
  133. aurLen++
  134. push = true
  135. }
  136. }
  137. pkgStr = pkgStr[:len(pkgStr)-1] + ")"
  138. pkgStrMake = pkgStrMake[:len(pkgStrMake)-1] + ")"
  139. } else if dc.MakeOnly.get(pkg.Name) {
  140. aurMakeLen++
  141. pushMake = true
  142. } else {
  143. aurLen++
  144. push = true
  145. }
  146. if push {
  147. aur += pkgStr
  148. }
  149. if pushMake {
  150. aurMake += pkgStrMake
  151. }
  152. }
  153. printDownloads("Repo", repoLen, repo)
  154. printDownloads("Repo Make", repoMakeLen, repoMake)
  155. printDownloads("Aur", aurLen, aur)
  156. printDownloads("Aur Make", aurMakeLen, aurMake)
  157. }
  158. func printDownloads(repoName string, length int, packages string) {
  159. if length < 1 {
  160. return
  161. }
  162. repoInfo := bold(blue(
  163. "[" + repoName + ": " + strconv.Itoa(length) + "]"))
  164. fmt.Println(repoInfo + magenta(packages))
  165. }
  166. // PrintInfo prints package info like pacman -Si.
  167. func PrintInfo(a *rpc.Pkg) {
  168. fmt.Println(bold("Repository :"), "aur")
  169. fmt.Println(bold("Name :"), a.Name)
  170. fmt.Println(bold("Version :"), a.Version)
  171. fmt.Println(bold("Description :"), a.Description)
  172. fmt.Println(bold("URL :"), a.URL)
  173. fmt.Println(bold("Licenses :"), strings.Join(a.License, " "))
  174. fmt.Println(bold("Depends On :"), strings.Join(a.Depends, " "))
  175. fmt.Println(bold("Make Deps :"), strings.Join(a.MakeDepends, " "))
  176. fmt.Println(bold("Check Deps :"), strings.Join(a.CheckDepends, " "))
  177. fmt.Println(bold("Optional Deps :"), strings.Join(a.OptDepends, " "))
  178. fmt.Println(bold("Conflicts With :"), strings.Join(a.Conflicts, " "))
  179. fmt.Println(bold("Maintainer :"), a.Maintainer)
  180. fmt.Println(bold("Votes :"), a.NumVotes)
  181. fmt.Println(bold("Popularity :"), a.Popularity)
  182. if a.OutOfDate != 0 {
  183. fmt.Println(bold("Out-of-date :"), "Yes", "["+formatTime(a.OutOfDate)+"]")
  184. }
  185. fmt.Println()
  186. }
  187. // BiggestPackages prints the name of the ten biggest packages in the system.
  188. func biggestPackages() {
  189. localDb, err := alpmHandle.LocalDb()
  190. if err != nil {
  191. return
  192. }
  193. pkgCache := localDb.PkgCache()
  194. pkgS := pkgCache.SortBySize().Slice()
  195. if len(pkgS) < 10 {
  196. return
  197. }
  198. for i := 0; i < 10; i++ {
  199. fmt.Println(bold(pkgS[i].Name()) + ": " + cyan(human(pkgS[i].ISize())))
  200. }
  201. // Could implement size here as well, but we just want the general idea
  202. }
  203. // localStatistics prints installed packages statistics.
  204. func localStatistics() error {
  205. info, err := statistics()
  206. if err != nil {
  207. return err
  208. }
  209. _, _, _, remoteNames, err := filterPackages()
  210. if err != nil {
  211. return err
  212. }
  213. fmt.Printf(bold("Yay version v%s\n"), version)
  214. fmt.Println(bold(cyan("===========================================")))
  215. fmt.Println(bold(green("Total installed packages: ")) + magenta(strconv.Itoa(info.Totaln)))
  216. fmt.Println(bold(green("Total foreign installed packages: ")) + magenta(strconv.Itoa(len(remoteNames))))
  217. fmt.Println(bold(green("Explicitly installed packages: ")) + magenta(strconv.Itoa(info.Expln)))
  218. fmt.Println(bold(green("Total Size occupied by packages: ")) + magenta(human(info.TotalSize)))
  219. fmt.Println(bold(cyan("===========================================")))
  220. fmt.Println(bold(green("Ten biggest packages:")))
  221. biggestPackages()
  222. fmt.Println(bold(cyan("===========================================")))
  223. aurInfo(remoteNames)
  224. return nil
  225. }
  226. //todo make it less hacky
  227. func printNumberOfUpdates() error {
  228. //todo
  229. old := os.Stdout // keep backup of the real stdout
  230. os.Stdout = nil
  231. _, _, localNames, remoteNames, err := filterPackages()
  232. dt, _ := getDepTree(append(localNames, remoteNames...))
  233. aurUp, repoUp, err := upList(dt)
  234. os.Stdout = old // restoring the real stdout
  235. if err != nil {
  236. return err
  237. }
  238. fmt.Println(len(aurUp) + len(repoUp))
  239. return nil
  240. }
  241. //todo make it less hacky
  242. func printUpdateList() error {
  243. old := os.Stdout // keep backup of the real stdout
  244. os.Stdout = nil
  245. _, _, localNames, remoteNames, err := filterPackages()
  246. dt, _ := getDepTree(append(localNames, remoteNames...))
  247. aurUp, repoUp, err := upList(dt)
  248. os.Stdout = old // restoring the real stdout
  249. if err != nil {
  250. return err
  251. }
  252. for _, pkg := range repoUp {
  253. fmt.Println(pkg.Name)
  254. }
  255. for _, pkg := range aurUp {
  256. fmt.Println(pkg.Name)
  257. }
  258. return nil
  259. }
  260. func formatTime(i int) string {
  261. t := time.Unix(int64(i), 0)
  262. return fmt.Sprintf("%d/%d/%d", t.Year(), int(t.Month()), t.Day())
  263. }
  264. func red(in string) string {
  265. if alpmConf.Options&alpm.ConfColor > 0 {
  266. return "\x1b[31m" + in + "\x1b[0m"
  267. }
  268. return in
  269. }
  270. func green(in string) string {
  271. if alpmConf.Options&alpm.ConfColor > 0 {
  272. return "\x1b[32m" + in + "\x1b[0m"
  273. }
  274. return in
  275. }
  276. func yellow(in string) string {
  277. if alpmConf.Options&alpm.ConfColor > 0 {
  278. return "\x1b[33m" + in + "\x1b[0m"
  279. }
  280. return in
  281. }
  282. func blue(in string) string {
  283. if alpmConf.Options&alpm.ConfColor > 0 {
  284. return "\x1b[34m" + in + "\x1b[0m"
  285. }
  286. return in
  287. }
  288. func cyan(in string) string {
  289. if alpmConf.Options&alpm.ConfColor > 0 {
  290. return "\x1b[36m" + in + "\x1b[0m"
  291. }
  292. return in
  293. }
  294. func magenta(in string) string {
  295. if alpmConf.Options&alpm.ConfColor > 0 {
  296. return "\x1b[35m" + in + "\x1b[0m"
  297. }
  298. return in
  299. }
  300. func bold(in string) string {
  301. if alpmConf.Options&alpm.ConfColor > 0 {
  302. return "\x1b[1m" + in + "\x1b[0m"
  303. }
  304. return in
  305. }
  306. func colourHash(name string) (output string) {
  307. if alpmConf.Options&alpm.ConfColor == 0 {
  308. return name
  309. }
  310. var hash = 5381
  311. for i := 0; i < len(name); i++ {
  312. hash = int(name[i]) + ((hash << 5) + (hash))
  313. }
  314. return fmt.Sprintf("\x1b[%dm%s\x1b[0m", hash%6+31, name)
  315. }