print.go 9.8 KB

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