print.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 method 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. // Pretty print a set of packages from the same package base.
  88. // Packages foo and bar from a pkgbase named base would print like so:
  89. // base (foo bar)
  90. func formatPkgbase(pkg *rpc.Pkg, bases map[string][]*rpc.Pkg) string {
  91. str := pkg.PackageBase
  92. if len(bases[pkg.PackageBase]) > 1 || pkg.PackageBase != pkg.Name {
  93. str2 := " ("
  94. for _, split := range bases[pkg.PackageBase] {
  95. str2 += split.Name + " "
  96. }
  97. str2 = str2[:len(str2)-1] + ")"
  98. str += str2
  99. }
  100. return str
  101. }
  102. // printDownloadsFromRepo prints repository packages to be downloaded
  103. func printDepCatagories(dc *depCatagories) {
  104. repo := ""
  105. repoMake := ""
  106. aur := ""
  107. aurMake := ""
  108. repoLen := 0
  109. repoMakeLen := 0
  110. aurLen := 0
  111. aurMakeLen := 0
  112. for _, pkg := range dc.Repo {
  113. if dc.MakeOnly.get(pkg.Name()) {
  114. repoMake += " " + pkg.Name() + "-" + pkg.Version()
  115. repoMakeLen++
  116. } else {
  117. repo += " " + pkg.Name() + "-" + pkg.Version()
  118. repoLen++
  119. }
  120. }
  121. for _, pkg := range dc.Aur {
  122. pkgStr := " " + pkg.PackageBase + "-" + pkg.Version
  123. pkgStrMake := pkgStr
  124. push := false
  125. pushMake := false
  126. if len(dc.Bases[pkg.PackageBase]) > 1 || pkg.PackageBase != pkg.Name {
  127. pkgStr += " ("
  128. pkgStrMake += " ("
  129. for _, split := range dc.Bases[pkg.PackageBase] {
  130. if dc.MakeOnly.get(split.Name) {
  131. pkgStrMake += split.Name + " "
  132. aurMakeLen++
  133. pushMake = true
  134. } else {
  135. pkgStr += split.Name + " "
  136. aurLen++
  137. push = true
  138. }
  139. }
  140. pkgStr = pkgStr[:len(pkgStr)-1] + ")"
  141. pkgStrMake = pkgStrMake[:len(pkgStrMake)-1] + ")"
  142. } else if dc.MakeOnly.get(pkg.Name) {
  143. aurMakeLen++
  144. pushMake = true
  145. } else {
  146. aurLen++
  147. push = true
  148. }
  149. if push {
  150. aur += pkgStr
  151. }
  152. if pushMake {
  153. aurMake += pkgStrMake
  154. }
  155. }
  156. printDownloads("Repo", repoLen, repo)
  157. printDownloads("Repo Make", repoMakeLen, repoMake)
  158. printDownloads("Aur", aurLen, aur)
  159. printDownloads("Aur Make", aurMakeLen, aurMake)
  160. }
  161. func printDownloads(repoName string, length int, packages string) {
  162. if length < 1 {
  163. return
  164. }
  165. repoInfo := bold(blue(
  166. "[" + repoName + ": " + strconv.Itoa(length) + "]"))
  167. fmt.Println(repoInfo + magenta(packages))
  168. }
  169. // PrintInfo prints package info like pacman -Si.
  170. func PrintInfo(a *rpc.Pkg) {
  171. fmt.Println(bold("Repository :"), "aur")
  172. fmt.Println(bold("Name :"), a.Name)
  173. fmt.Println(bold("Version :"), a.Version)
  174. fmt.Println(bold("Description :"), a.Description)
  175. fmt.Println(bold("URL :"), a.URL)
  176. fmt.Println(bold("Licenses :"), strings.Join(a.License, " "))
  177. fmt.Println(bold("Depends On :"), strings.Join(a.Depends, " "))
  178. fmt.Println(bold("Make Deps :"), strings.Join(a.MakeDepends, " "))
  179. fmt.Println(bold("Check Deps :"), strings.Join(a.CheckDepends, " "))
  180. fmt.Println(bold("Optional Deps :"), strings.Join(a.OptDepends, " "))
  181. fmt.Println(bold("Conflicts With :"), strings.Join(a.Conflicts, " "))
  182. fmt.Println(bold("Maintainer :"), a.Maintainer)
  183. fmt.Println(bold("Votes :"), a.NumVotes)
  184. fmt.Println(bold("Popularity :"), a.Popularity)
  185. if a.OutOfDate != 0 {
  186. fmt.Println(bold("Out-of-date :"), "Yes", "["+formatTime(a.OutOfDate)+"]")
  187. }
  188. fmt.Println()
  189. }
  190. // BiggestPackages prints the name of the ten biggest packages in the system.
  191. func biggestPackages() {
  192. localDb, err := alpmHandle.LocalDb()
  193. if err != nil {
  194. return
  195. }
  196. pkgCache := localDb.PkgCache()
  197. pkgS := pkgCache.SortBySize().Slice()
  198. if len(pkgS) < 10 {
  199. return
  200. }
  201. for i := 0; i < 10; i++ {
  202. fmt.Println(bold(pkgS[i].Name()) + ": " + cyan(human(pkgS[i].ISize())))
  203. }
  204. // Could implement size here as well, but we just want the general idea
  205. }
  206. // localStatistics prints installed packages statistics.
  207. func localStatistics() error {
  208. info, err := statistics()
  209. if err != nil {
  210. return err
  211. }
  212. _, _, _, remoteNames, err := filterPackages()
  213. if err != nil {
  214. return err
  215. }
  216. fmt.Printf(bold("Yay version v%s\n"), version)
  217. fmt.Println(bold(cyan("===========================================")))
  218. fmt.Println(bold(green("Total installed packages: ")) + magenta(strconv.Itoa(info.Totaln)))
  219. fmt.Println(bold(green("Total foreign installed packages: ")) + magenta(strconv.Itoa(len(remoteNames))))
  220. fmt.Println(bold(green("Explicitly installed packages: ")) + magenta(strconv.Itoa(info.Expln)))
  221. fmt.Println(bold(green("Total Size occupied by packages: ")) + magenta(human(info.TotalSize)))
  222. fmt.Println(bold(cyan("===========================================")))
  223. fmt.Println(bold(green("Ten biggest packages:")))
  224. biggestPackages()
  225. fmt.Println(bold(cyan("===========================================")))
  226. aurInfo(remoteNames)
  227. return nil
  228. }
  229. //TODO: Make it less hacky
  230. func printNumberOfUpdates() error {
  231. //todo
  232. old := os.Stdout // keep backup of the real stdout
  233. os.Stdout = nil
  234. _, _, localNames, remoteNames, err := filterPackages()
  235. dt, _ := getDepTree(append(localNames, remoteNames...))
  236. aurUp, repoUp, err := upList(dt)
  237. os.Stdout = old // restoring the real stdout
  238. if err != nil {
  239. return err
  240. }
  241. fmt.Println(len(aurUp) + len(repoUp))
  242. return nil
  243. }
  244. //TODO: Make it less hacky
  245. func printUpdateList() error {
  246. old := os.Stdout // Keep backup of the real stdout
  247. os.Stdout = nil
  248. _, _, localNames, remoteNames, err := filterPackages()
  249. dt, _ := getDepTree(append(localNames, remoteNames...))
  250. aurUp, repoUp, err := upList(dt)
  251. os.Stdout = old // Restoring the real stdout
  252. if err != nil {
  253. return err
  254. }
  255. for _, pkg := range repoUp {
  256. fmt.Println(pkg.Name)
  257. }
  258. for _, pkg := range aurUp {
  259. fmt.Println(pkg.Name)
  260. }
  261. return nil
  262. }
  263. // Formats a unix timestamp to yyyy/mm/dd
  264. func formatTime(i int) string {
  265. t := time.Unix(int64(i), 0)
  266. return fmt.Sprintf("%d/%02d/%02d", t.Year(), int(t.Month()), t.Day())
  267. }
  268. func red(in string) string {
  269. if alpmConf.Options&alpm.ConfColor > 0 {
  270. return "\x1b[31m" + in + "\x1b[0m"
  271. }
  272. return in
  273. }
  274. func green(in string) string {
  275. if alpmConf.Options&alpm.ConfColor > 0 {
  276. return "\x1b[32m" + in + "\x1b[0m"
  277. }
  278. return in
  279. }
  280. func yellow(in string) string {
  281. if alpmConf.Options&alpm.ConfColor > 0 {
  282. return "\x1b[33m" + in + "\x1b[0m"
  283. }
  284. return in
  285. }
  286. func blue(in string) string {
  287. if alpmConf.Options&alpm.ConfColor > 0 {
  288. return "\x1b[34m" + in + "\x1b[0m"
  289. }
  290. return in
  291. }
  292. func cyan(in string) string {
  293. if alpmConf.Options&alpm.ConfColor > 0 {
  294. return "\x1b[36m" + in + "\x1b[0m"
  295. }
  296. return in
  297. }
  298. func magenta(in string) string {
  299. if alpmConf.Options&alpm.ConfColor > 0 {
  300. return "\x1b[35m" + in + "\x1b[0m"
  301. }
  302. return in
  303. }
  304. func bold(in string) string {
  305. if alpmConf.Options&alpm.ConfColor > 0 {
  306. return "\x1b[1m" + in + "\x1b[0m"
  307. }
  308. return in
  309. }
  310. // Colours text using a hashing algorithm. The same text will always produce the
  311. // same colour while different text will produce a different colour.
  312. func colourHash(name string) (output string) {
  313. if alpmConf.Options&alpm.ConfColor == 0 {
  314. return name
  315. }
  316. var hash = 5381
  317. for i := 0; i < len(name); i++ {
  318. hash = int(name[i]) + ((hash << 5) + (hash))
  319. }
  320. return fmt.Sprintf("\x1b[%dm%s\x1b[0m", hash%6+31, name)
  321. }