print.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 pkg, err := localDb.PkgByName(res.Name); err == nil {
  49. if pkg.Version() != res.Version {
  50. toprint += bold(green("(Installed: " + pkg.Version() + ")"))
  51. } else {
  52. toprint += bold(green("(Installed)"))
  53. }
  54. }
  55. toprint += "\n " + res.Description
  56. fmt.Println(toprint)
  57. }
  58. }
  59. // PrintSearch receives a RepoSearch type and outputs pretty text.
  60. func (s repoQuery) printSearch() {
  61. for i, res := range s {
  62. var toprint string
  63. if config.SearchMode == NumberMenu {
  64. if config.SortMode == BottomUp {
  65. toprint += magenta(strconv.Itoa(len(s)-i) + " ")
  66. } else {
  67. toprint += magenta(strconv.Itoa(i+1) + " ")
  68. }
  69. } else if config.SearchMode == Minimal {
  70. fmt.Println(res.Name())
  71. continue
  72. }
  73. toprint += bold(colourHash(res.DB().Name())) + "/" + bold(res.Name()) +
  74. " " + cyan(res.Version()) +
  75. bold(" ("+human(res.Size())+
  76. " "+human(res.ISize())+") ")
  77. if len(res.Groups().Slice()) != 0 {
  78. toprint += fmt.Sprint(res.Groups().Slice(), " ")
  79. }
  80. localDb, err := alpmHandle.LocalDb()
  81. if err == nil {
  82. if pkg, err := localDb.PkgByName(res.Name()); err == nil {
  83. if pkg.Version() != res.Version() {
  84. toprint += bold(green("(Installed: " + pkg.Version() + ")"))
  85. } else {
  86. toprint += bold(green("(Installed)"))
  87. }
  88. }
  89. }
  90. toprint += "\n " + res.Description()
  91. fmt.Println(toprint)
  92. }
  93. }
  94. // Pretty print a set of packages from the same package base.
  95. // Packages foo and bar from a pkgbase named base would print like so:
  96. // base (foo bar)
  97. func formatPkgbase(pkg *rpc.Pkg, bases map[string][]*rpc.Pkg) string {
  98. str := pkg.PackageBase
  99. if len(bases[pkg.PackageBase]) > 1 || pkg.PackageBase != pkg.Name {
  100. str2 := " ("
  101. for _, split := range bases[pkg.PackageBase] {
  102. str2 += split.Name + " "
  103. }
  104. str2 = str2[:len(str2)-1] + ")"
  105. str += str2
  106. }
  107. return str
  108. }
  109. // Print prints the details of the packages to upgrade.
  110. func (u upSlice) Print(start int) {
  111. for k, i := range u {
  112. left, right := getVersionDiff(i.LocalVersion, i.RemoteVersion)
  113. fmt.Print(magenta(fmt.Sprintf("%3d ", len(u)+start-k-1)))
  114. fmt.Print(bold(colourHash(i.Repository)), "/", cyan(i.Name))
  115. w := 70 - len(i.Repository) - len(i.Name)
  116. padding := fmt.Sprintf("%%%ds", w)
  117. fmt.Printf(padding, left)
  118. fmt.Printf(" -> %s\n", right)
  119. }
  120. }
  121. // printDownloadsFromRepo prints repository packages to be downloaded
  122. func printDepCatagories(dc *depCatagories) {
  123. repo := ""
  124. repoMake := ""
  125. aur := ""
  126. aurMake := ""
  127. repoLen := 0
  128. repoMakeLen := 0
  129. aurLen := 0
  130. aurMakeLen := 0
  131. for _, pkg := range dc.Repo {
  132. if dc.MakeOnly.get(pkg.Name()) {
  133. repoMake += " " + pkg.Name() + "-" + pkg.Version()
  134. repoMakeLen++
  135. } else {
  136. repo += " " + pkg.Name() + "-" + pkg.Version()
  137. repoLen++
  138. }
  139. }
  140. for _, pkg := range dc.Aur {
  141. pkgStr := " " + pkg.PackageBase + "-" + pkg.Version
  142. pkgStrMake := pkgStr
  143. push := false
  144. pushMake := false
  145. if len(dc.Bases[pkg.PackageBase]) > 1 || pkg.PackageBase != pkg.Name {
  146. pkgStr += " ("
  147. pkgStrMake += " ("
  148. for _, split := range dc.Bases[pkg.PackageBase] {
  149. if dc.MakeOnly.get(split.Name) {
  150. pkgStrMake += split.Name + " "
  151. aurMakeLen++
  152. pushMake = true
  153. } else {
  154. pkgStr += split.Name + " "
  155. aurLen++
  156. push = true
  157. }
  158. }
  159. pkgStr = pkgStr[:len(pkgStr)-1] + ")"
  160. pkgStrMake = pkgStrMake[:len(pkgStrMake)-1] + ")"
  161. } else if dc.MakeOnly.get(pkg.Name) {
  162. aurMakeLen++
  163. pushMake = true
  164. } else {
  165. aurLen++
  166. push = true
  167. }
  168. if push {
  169. aur += pkgStr
  170. }
  171. if pushMake {
  172. aurMake += pkgStrMake
  173. }
  174. }
  175. printDownloads("Repo", repoLen, repo)
  176. printDownloads("Repo Make", repoMakeLen, repoMake)
  177. printDownloads("Aur", aurLen, aur)
  178. printDownloads("Aur Make", aurMakeLen, aurMake)
  179. }
  180. func printDownloads(repoName string, length int, packages string) {
  181. if length < 1 {
  182. return
  183. }
  184. repoInfo := bold(blue(
  185. "[" + repoName + ": " + strconv.Itoa(length) + "]"))
  186. fmt.Println(repoInfo + magenta(packages))
  187. }
  188. // PrintInfo prints package info like pacman -Si.
  189. func PrintInfo(a *rpc.Pkg) {
  190. fmt.Println(bold("Repository :"), "aur")
  191. fmt.Println(bold("Name :"), a.Name)
  192. fmt.Println(bold("Version :"), a.Version)
  193. fmt.Println(bold("Description :"), a.Description)
  194. fmt.Println(bold("URL :"), a.URL)
  195. fmt.Println(bold("Licenses :"), strings.Join(a.License, " "))
  196. fmt.Println(bold("Provides :"), strings.Join(a.Provides, " "))
  197. fmt.Println(bold("Depends On :"), strings.Join(a.Depends, " "))
  198. fmt.Println(bold("Make Deps :"), strings.Join(a.MakeDepends, " "))
  199. fmt.Println(bold("Check Deps :"), strings.Join(a.CheckDepends, " "))
  200. fmt.Println(bold("Optional Deps :"), strings.Join(a.OptDepends, " "))
  201. fmt.Println(bold("Conflicts With :"), strings.Join(a.Conflicts, " "))
  202. fmt.Println(bold("Maintainer :"), a.Maintainer)
  203. fmt.Println(bold("Votes :"), a.NumVotes)
  204. fmt.Println(bold("Popularity :"), a.Popularity)
  205. if a.OutOfDate != 0 {
  206. fmt.Println(bold("Out-of-date :"), "Yes", "["+formatTime(a.OutOfDate)+"]")
  207. }
  208. fmt.Println()
  209. }
  210. // BiggestPackages prints the name of the ten biggest packages in the system.
  211. func biggestPackages() {
  212. localDb, err := alpmHandle.LocalDb()
  213. if err != nil {
  214. return
  215. }
  216. pkgCache := localDb.PkgCache()
  217. pkgS := pkgCache.SortBySize().Slice()
  218. if len(pkgS) < 10 {
  219. return
  220. }
  221. for i := 0; i < 10; i++ {
  222. fmt.Println(bold(pkgS[i].Name()) + ": " + cyan(human(pkgS[i].ISize())))
  223. }
  224. // Could implement size here as well, but we just want the general idea
  225. }
  226. // localStatistics prints installed packages statistics.
  227. func localStatistics() error {
  228. info, err := statistics()
  229. if err != nil {
  230. return err
  231. }
  232. _, _, _, remoteNames, err := filterPackages()
  233. if err != nil {
  234. return err
  235. }
  236. fmt.Printf(bold("Yay version v%s\n"), version)
  237. fmt.Println(bold(cyan("===========================================")))
  238. fmt.Println(bold(green("Total installed packages: ")) + magenta(strconv.Itoa(info.Totaln)))
  239. fmt.Println(bold(green("Total foreign installed packages: ")) + magenta(strconv.Itoa(len(remoteNames))))
  240. fmt.Println(bold(green("Explicitly installed packages: ")) + magenta(strconv.Itoa(info.Expln)))
  241. fmt.Println(bold(green("Total Size occupied by packages: ")) + magenta(human(info.TotalSize)))
  242. fmt.Println(bold(cyan("===========================================")))
  243. fmt.Println(bold(green("Ten biggest packages:")))
  244. biggestPackages()
  245. fmt.Println(bold(cyan("===========================================")))
  246. aurInfo(remoteNames)
  247. return nil
  248. }
  249. //TODO: Make it less hacky
  250. func printNumberOfUpdates() error {
  251. //todo
  252. old := os.Stdout // keep backup of the real stdout
  253. os.Stdout = nil
  254. aurUp, repoUp, err := upList()
  255. os.Stdout = old // restoring the real stdout
  256. if err != nil {
  257. return err
  258. }
  259. fmt.Println(len(aurUp) + len(repoUp))
  260. return nil
  261. }
  262. //TODO: Make it less hacky
  263. func printUpdateList(parser *arguments) error {
  264. old := os.Stdout // keep backup of the real stdout
  265. os.Stdout = nil
  266. _, _, localNames, remoteNames, err := filterPackages()
  267. aurUp, repoUp, err := upList()
  268. os.Stdout = old // restoring the real stdout
  269. if err != nil {
  270. return err
  271. }
  272. noTargets := len(parser.targets) == 0
  273. if !parser.existsArg("m", "foreigne") {
  274. for _, pkg := range repoUp {
  275. if noTargets || parser.targets.get(pkg.Name) {
  276. fmt.Printf("%s %s -> %s\n", bold(pkg.Name), green(pkg.LocalVersion), green(pkg.RemoteVersion))
  277. delete(parser.targets, pkg.Name)
  278. }
  279. }
  280. }
  281. if !parser.existsArg("n", "native") {
  282. for _, pkg := range aurUp {
  283. if noTargets || parser.targets.get(pkg.Name) {
  284. fmt.Printf("%s %s -> %s\n", bold(pkg.Name), green(pkg.LocalVersion), green(pkg.RemoteVersion))
  285. delete(parser.targets, pkg.Name)
  286. }
  287. }
  288. }
  289. missing := false
  290. outer:
  291. for pkg := range parser.targets {
  292. for _, name := range localNames {
  293. if name == pkg {
  294. continue outer
  295. }
  296. }
  297. for _, name := range remoteNames {
  298. if name == pkg {
  299. continue outer
  300. }
  301. }
  302. fmt.Println(red(bold("error:")), "package '"+pkg+"' was not found")
  303. missing = true
  304. }
  305. if missing {
  306. return fmt.Errorf("")
  307. }
  308. return nil
  309. }
  310. // Formats a unix timestamp to yyyy/mm/dd
  311. func formatTime(i int) string {
  312. t := time.Unix(int64(i), 0)
  313. return fmt.Sprintf("%d/%02d/%02d", t.Year(), int(t.Month()), t.Day())
  314. }
  315. const (
  316. redCode = "\x1b[31m"
  317. greenCode = "\x1b[32m"
  318. yellowCode = "\x1b[33m"
  319. blueCode = "\x1b[34m"
  320. magentaCode = "\x1b[35m"
  321. cyanCode = "\x1b[36m"
  322. boldCode = "\x1b[1m"
  323. resetCode = "\x1b[0m"
  324. )
  325. func stylize(startCode, in string) string {
  326. if useColor {
  327. return startCode + in + resetCode
  328. }
  329. return in
  330. }
  331. func red(in string) string {
  332. return stylize(redCode, in)
  333. }
  334. func green(in string) string {
  335. return stylize(greenCode, in)
  336. }
  337. func yellow(in string) string {
  338. return stylize(yellowCode, in)
  339. }
  340. func blue(in string) string {
  341. return stylize(blueCode, in)
  342. }
  343. func cyan(in string) string {
  344. return stylize(cyanCode, in)
  345. }
  346. func magenta(in string) string {
  347. return stylize(magentaCode, in)
  348. }
  349. func bold(in string) string {
  350. return stylize(boldCode, in)
  351. }
  352. // Colours text using a hashing algorithm. The same text will always produce the
  353. // same colour while different text will produce a different colour.
  354. func colourHash(name string) (output string) {
  355. if !useColor {
  356. return name
  357. }
  358. var hash = 5381
  359. for i := 0; i < len(name); i++ {
  360. hash = int(name[i]) + ((hash << 5) + (hash))
  361. }
  362. return fmt.Sprintf("\x1b[%dm%s\x1b[0m", hash%6+31, name)
  363. }