print.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. "strings"
  7. "time"
  8. rpc "github.com/mikkeloscar/aur"
  9. )
  10. const arrow = "==>"
  11. // human method 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 += magenta(strconv.Itoa(len(q)+start-i-1) + " ")
  31. } else {
  32. toprint += magenta(strconv.Itoa(start+i) + " ")
  33. }
  34. } else if config.SearchMode == Minimal {
  35. fmt.Println(res.Name)
  36. continue
  37. }
  38. toprint += bold(colourHash("aur")) + "/" + bold(res.Name) +
  39. " " + cyan(res.Version) +
  40. bold(" (+"+strconv.Itoa(res.NumVotes)) +
  41. " " + bold(strconv.FormatFloat(res.Popularity, 'f', 2, 64)+"%) ")
  42. if res.Maintainer == "" {
  43. toprint += bold(red("(Orphaned)")) + " "
  44. }
  45. if res.OutOfDate != 0 {
  46. toprint += bold(red("(Out-of-date "+formatTime(res.OutOfDate)+")")) + " "
  47. }
  48. if _, err := localDb.PkgByName(res.Name); err == nil {
  49. toprint += bold(green("(Installed)"))
  50. }
  51. toprint += "\n " + res.Description
  52. fmt.Println(toprint)
  53. }
  54. }
  55. // PrintSearch receives a RepoSearch type and outputs pretty text.
  56. func (s repoQuery) printSearch() {
  57. for i, res := range s {
  58. var toprint string
  59. if config.SearchMode == NumberMenu {
  60. if config.SortMode == BottomUp {
  61. toprint += magenta(strconv.Itoa(len(s)-i) + " ")
  62. } else {
  63. toprint += magenta(strconv.Itoa(i+1) + " ")
  64. }
  65. } else if config.SearchMode == Minimal {
  66. fmt.Println(res.Name())
  67. continue
  68. }
  69. toprint += bold(colourHash(res.DB().Name())) + "/" + bold(res.Name()) +
  70. " " + cyan(res.Version()) +
  71. bold(" ("+human(res.Size())+
  72. " "+human(res.ISize())+") ")
  73. if len(res.Groups().Slice()) != 0 {
  74. toprint += fmt.Sprint(res.Groups().Slice(), " ")
  75. }
  76. localDb, err := alpmHandle.LocalDb()
  77. if err == nil {
  78. if _, err = localDb.PkgByName(res.Name()); err == nil {
  79. toprint += bold(green("(Installed)"))
  80. }
  81. }
  82. toprint += "\n " + res.Description()
  83. fmt.Println(toprint)
  84. }
  85. }
  86. // Pretty print a set of packages from the same package base.
  87. // Packages foo and bar from a pkgbase named base would print like so:
  88. // base (foo bar)
  89. func formatPkgbase(pkg *rpc.Pkg, bases map[string][]*rpc.Pkg) string {
  90. str := pkg.PackageBase
  91. if len(bases[pkg.PackageBase]) > 1 || pkg.PackageBase != pkg.Name {
  92. str2 := " ("
  93. for _, split := range bases[pkg.PackageBase] {
  94. str2 += split.Name + " "
  95. }
  96. str2 = str2[:len(str2)-1] + ")"
  97. str += str2
  98. }
  99. return str
  100. }
  101. // Print prints the details of the packages to upgrade.
  102. func (u upSlice) Print(start int) {
  103. for k, i := range u {
  104. left, right := getVersionDiff(i.LocalVersion, i.RemoteVersion)
  105. fmt.Print(magenta(fmt.Sprintf("%3d ", len(u)+start-k-1)))
  106. fmt.Print(bold(colourHash(i.Repository)), "/", cyan(i.Name))
  107. w := 70 - len(i.Repository) - len(i.Name)
  108. padding := fmt.Sprintf("%%%ds", w)
  109. fmt.Printf(padding, left)
  110. fmt.Printf(" -> %s\n", right)
  111. }
  112. }
  113. // printDownloadsFromRepo prints repository packages to be downloaded
  114. func printDepCatagories(dc *depCatagories) {
  115. repo := ""
  116. repoMake := ""
  117. aur := ""
  118. aurMake := ""
  119. repoLen := 0
  120. repoMakeLen := 0
  121. aurLen := 0
  122. aurMakeLen := 0
  123. for _, pkg := range dc.Repo {
  124. if dc.MakeOnly.get(pkg.Name()) {
  125. repoMake += " " + pkg.Name() + "-" + pkg.Version()
  126. repoMakeLen++
  127. } else {
  128. repo += " " + pkg.Name() + "-" + pkg.Version()
  129. repoLen++
  130. }
  131. }
  132. for _, pkg := range dc.Aur {
  133. pkgStr := " " + pkg.PackageBase + "-" + pkg.Version
  134. pkgStrMake := pkgStr
  135. push := false
  136. pushMake := false
  137. if len(dc.Bases[pkg.PackageBase]) > 1 || pkg.PackageBase != pkg.Name {
  138. pkgStr += " ("
  139. pkgStrMake += " ("
  140. for _, split := range dc.Bases[pkg.PackageBase] {
  141. if dc.MakeOnly.get(split.Name) {
  142. pkgStrMake += split.Name + " "
  143. aurMakeLen++
  144. pushMake = true
  145. } else {
  146. pkgStr += split.Name + " "
  147. aurLen++
  148. push = true
  149. }
  150. }
  151. pkgStr = pkgStr[:len(pkgStr)-1] + ")"
  152. pkgStrMake = pkgStrMake[:len(pkgStrMake)-1] + ")"
  153. } else if dc.MakeOnly.get(pkg.Name) {
  154. aurMakeLen++
  155. pushMake = true
  156. } else {
  157. aurLen++
  158. push = true
  159. }
  160. if push {
  161. aur += pkgStr
  162. }
  163. if pushMake {
  164. aurMake += pkgStrMake
  165. }
  166. }
  167. printDownloads("Repo", repoLen, repo)
  168. printDownloads("Repo Make", repoMakeLen, repoMake)
  169. printDownloads("Aur", aurLen, aur)
  170. printDownloads("Aur Make", aurMakeLen, aurMake)
  171. }
  172. func printDownloads(repoName string, length int, packages string) {
  173. if length < 1 {
  174. return
  175. }
  176. repoInfo := bold(blue(
  177. "[" + repoName + ": " + strconv.Itoa(length) + "]"))
  178. fmt.Println(repoInfo + magenta(packages))
  179. }
  180. // PrintInfo prints package info like pacman -Si.
  181. func PrintInfo(a *rpc.Pkg) {
  182. fmt.Println(bold("Repository :"), "aur")
  183. fmt.Println(bold("Name :"), a.Name)
  184. fmt.Println(bold("Version :"), a.Version)
  185. fmt.Println(bold("Description :"), a.Description)
  186. fmt.Println(bold("URL :"), a.URL)
  187. fmt.Println(bold("Licenses :"), strings.Join(a.License, " "))
  188. fmt.Println(bold("Provides :"), strings.Join(a.Provides, " "))
  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(parser *arguments) 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. noTargets := len(parser.targets) == 0
  268. if !parser.existsArg("m", "foreigne") {
  269. for _, pkg := range repoUp {
  270. if noTargets || parser.targets.get(pkg.Name) {
  271. fmt.Printf("%s %s -> %s\n", bold(pkg.Name), green(pkg.LocalVersion), green(pkg.RemoteVersion))
  272. delete(parser.targets, pkg.Name)
  273. }
  274. }
  275. }
  276. if !parser.existsArg("n", "native") {
  277. for _, pkg := range aurUp {
  278. if noTargets || parser.targets.get(pkg.Name) {
  279. fmt.Printf("%s %s -> %s\n", bold(pkg.Name), green(pkg.LocalVersion), green(pkg.RemoteVersion))
  280. delete(parser.targets, pkg.Name)
  281. }
  282. }
  283. }
  284. for pkg := range parser.targets {
  285. fmt.Println(red(bold("error:")), "package '"+pkg+"' was not found")
  286. }
  287. return nil
  288. }
  289. // Formats a unix timestamp to yyyy/mm/dd
  290. func formatTime(i int) string {
  291. t := time.Unix(int64(i), 0)
  292. return fmt.Sprintf("%d/%02d/%02d", t.Year(), int(t.Month()), t.Day())
  293. }
  294. func red(in string) string {
  295. if useColor {
  296. return "\x1b[31m" + in + "\x1b[0m"
  297. }
  298. return in
  299. }
  300. func green(in string) string {
  301. if useColor {
  302. return "\x1b[32m" + in + "\x1b[0m"
  303. }
  304. return in
  305. }
  306. func yellow(in string) string {
  307. if useColor {
  308. return "\x1b[33m" + in + "\x1b[0m"
  309. }
  310. return in
  311. }
  312. func blue(in string) string {
  313. if useColor {
  314. return "\x1b[34m" + in + "\x1b[0m"
  315. }
  316. return in
  317. }
  318. func cyan(in string) string {
  319. if useColor {
  320. return "\x1b[36m" + in + "\x1b[0m"
  321. }
  322. return in
  323. }
  324. func magenta(in string) string {
  325. if useColor {
  326. return "\x1b[35m" + in + "\x1b[0m"
  327. }
  328. return in
  329. }
  330. func bold(in string) string {
  331. if useColor {
  332. return "\x1b[1m" + in + "\x1b[0m"
  333. }
  334. return in
  335. }
  336. // Colours text using a hashing algorithm. The same text will always produce the
  337. // same colour while different text will produce a different colour.
  338. func colourHash(name string) (output string) {
  339. if !useColor {
  340. return name
  341. }
  342. var hash = 5381
  343. for i := 0; i < len(name); i++ {
  344. hash = int(name[i]) + ((hash << 5) + (hash))
  345. }
  346. return fmt.Sprintf("\x1b[%dm%s\x1b[0m", hash%6+31, name)
  347. }