print.go 11 KB

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