print.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. func (u upgrade) StylizedNameWithRepository() string {
  134. return bold(colourHash(u.Repository)) + "/" + bold(u.Name)
  135. }
  136. // Print prints the details of the packages to upgrade.
  137. func (u upSlice) Print() {
  138. longestName, longestVersion := 0, 0
  139. for _, pack := range u {
  140. packNameLen := len(pack.StylizedNameWithRepository())
  141. version, _ := getVersionDiff(pack.LocalVersion, pack.RemoteVersion)
  142. packVersionLen := len(version)
  143. longestName = max(packNameLen, longestName)
  144. longestVersion = max(packVersionLen, longestVersion)
  145. }
  146. namePadding := fmt.Sprintf("%%-%ds ", longestName)
  147. versionPadding := fmt.Sprintf("%%-%ds", longestVersion)
  148. numberPadding := fmt.Sprintf("%%%dd ", len(fmt.Sprintf("%v", len(u))))
  149. for k, i := range u {
  150. left, right := getVersionDiff(i.LocalVersion, i.RemoteVersion)
  151. fmt.Print(magenta(fmt.Sprintf(numberPadding, len(u)-k)))
  152. fmt.Printf(namePadding, i.StylizedNameWithRepository())
  153. fmt.Printf("%s -> %s\n", fmt.Sprintf(versionPadding, left), right)
  154. }
  155. }
  156. // printDownloadsFromRepo prints repository packages to be downloaded
  157. func printDepCatagories(dc *depCatagories) {
  158. repo := ""
  159. repoMake := ""
  160. aur := ""
  161. aurMake := ""
  162. repoLen := 0
  163. repoMakeLen := 0
  164. aurLen := 0
  165. aurMakeLen := 0
  166. for _, pkg := range dc.Repo {
  167. if dc.MakeOnly.get(pkg.Name()) {
  168. repoMake += " " + pkg.Name() + "-" + pkg.Version()
  169. repoMakeLen++
  170. } else {
  171. repo += " " + pkg.Name() + "-" + pkg.Version()
  172. repoLen++
  173. }
  174. }
  175. for _, pkg := range dc.Aur {
  176. pkgStr := " " + pkg.PackageBase + "-" + pkg.Version
  177. pkgStrMake := pkgStr
  178. push := false
  179. pushMake := false
  180. if len(dc.Bases[pkg.PackageBase]) > 1 || pkg.PackageBase != pkg.Name {
  181. pkgStr += " ("
  182. pkgStrMake += " ("
  183. for _, split := range dc.Bases[pkg.PackageBase] {
  184. if dc.MakeOnly.get(split.Name) {
  185. pkgStrMake += split.Name + " "
  186. aurMakeLen++
  187. pushMake = true
  188. } else {
  189. pkgStr += split.Name + " "
  190. aurLen++
  191. push = true
  192. }
  193. }
  194. pkgStr = pkgStr[:len(pkgStr)-1] + ")"
  195. pkgStrMake = pkgStrMake[:len(pkgStrMake)-1] + ")"
  196. } else if dc.MakeOnly.get(pkg.Name) {
  197. aurMakeLen++
  198. pushMake = true
  199. } else {
  200. aurLen++
  201. push = true
  202. }
  203. if push {
  204. aur += pkgStr
  205. }
  206. if pushMake {
  207. aurMake += pkgStrMake
  208. }
  209. }
  210. printDownloads("Repo", repoLen, repo)
  211. printDownloads("Repo Make", repoMakeLen, repoMake)
  212. printDownloads("Aur", aurLen, aur)
  213. printDownloads("Aur Make", aurMakeLen, aurMake)
  214. }
  215. func printDownloads(repoName string, length int, packages string) {
  216. if length < 1 {
  217. return
  218. }
  219. repoInfo := bold(blue(
  220. "[" + repoName + ": " + strconv.Itoa(length) + "]"))
  221. fmt.Println(repoInfo + cyan(packages))
  222. }
  223. // PrintInfo prints package info like pacman -Si.
  224. func PrintInfo(a *rpc.Pkg) {
  225. fmt.Println(bold("Repository :"), "aur")
  226. fmt.Println(bold("Name :"), a.Name)
  227. fmt.Println(bold("Version :"), a.Version)
  228. fmt.Println(bold("Description :"), a.Description)
  229. fmt.Println(bold("URL :"), a.URL)
  230. fmt.Println(bold("Licenses :"), strings.Join(a.License, " "))
  231. fmt.Println(bold("Provides :"), strings.Join(a.Provides, " "))
  232. fmt.Println(bold("Depends On :"), strings.Join(a.Depends, " "))
  233. fmt.Println(bold("Make Deps :"), strings.Join(a.MakeDepends, " "))
  234. fmt.Println(bold("Check Deps :"), strings.Join(a.CheckDepends, " "))
  235. fmt.Println(bold("Optional Deps :"), strings.Join(a.OptDepends, " "))
  236. fmt.Println(bold("Conflicts With :"), strings.Join(a.Conflicts, " "))
  237. fmt.Println(bold("Maintainer :"), a.Maintainer)
  238. fmt.Println(bold("Votes :"), a.NumVotes)
  239. fmt.Println(bold("Popularity :"), a.Popularity)
  240. if a.OutOfDate != 0 {
  241. fmt.Println(bold("Out-of-date :"), "Yes", "["+formatTime(a.OutOfDate)+"]")
  242. }
  243. fmt.Println()
  244. }
  245. // BiggestPackages prints the name of the ten biggest packages in the system.
  246. func biggestPackages() {
  247. localDb, err := alpmHandle.LocalDb()
  248. if err != nil {
  249. return
  250. }
  251. pkgCache := localDb.PkgCache()
  252. pkgS := pkgCache.SortBySize().Slice()
  253. if len(pkgS) < 10 {
  254. return
  255. }
  256. for i := 0; i < 10; i++ {
  257. fmt.Println(bold(pkgS[i].Name()) + ": " + cyan(human(pkgS[i].ISize())))
  258. }
  259. // Could implement size here as well, but we just want the general idea
  260. }
  261. // localStatistics prints installed packages statistics.
  262. func localStatistics() error {
  263. info, err := statistics()
  264. if err != nil {
  265. return err
  266. }
  267. _, _, _, remoteNames, err := filterPackages()
  268. if err != nil {
  269. return err
  270. }
  271. fmt.Printf(bold("Yay version v%s\n"), version)
  272. fmt.Println(bold(cyan("===========================================")))
  273. fmt.Println(bold(green("Total installed packages: ")) + cyan(strconv.Itoa(info.Totaln)))
  274. fmt.Println(bold(green("Total foreign installed packages: ")) + cyan(strconv.Itoa(len(remoteNames))))
  275. fmt.Println(bold(green("Explicitly installed packages: ")) + cyan(strconv.Itoa(info.Expln)))
  276. fmt.Println(bold(green("Total Size occupied by packages: ")) + cyan(human(info.TotalSize)))
  277. fmt.Println(bold(cyan("===========================================")))
  278. fmt.Println(bold(green("Ten biggest packages:")))
  279. biggestPackages()
  280. fmt.Println(bold(cyan("===========================================")))
  281. aurInfoPrint(remoteNames)
  282. return nil
  283. }
  284. //TODO: Make it less hacky
  285. func printNumberOfUpdates() error {
  286. //todo
  287. warnings := &aurWarnings{}
  288. old := os.Stdout // keep backup of the real stdout
  289. os.Stdout = nil
  290. aurUp, repoUp, err := upList(warnings)
  291. os.Stdout = old // restoring the real stdout
  292. if err != nil {
  293. return err
  294. }
  295. fmt.Println(len(aurUp) + len(repoUp))
  296. return nil
  297. }
  298. //TODO: Make it less hacky
  299. func printUpdateList(parser *arguments) error {
  300. warnings := &aurWarnings{}
  301. old := os.Stdout // keep backup of the real stdout
  302. os.Stdout = nil
  303. _, _, localNames, remoteNames, err := filterPackages()
  304. if err != nil {
  305. return err
  306. }
  307. aurUp, repoUp, err := upList(warnings)
  308. os.Stdout = old // restoring the real stdout
  309. if err != nil {
  310. return err
  311. }
  312. noTargets := len(parser.targets) == 0
  313. if !parser.existsArg("m", "foreigne") {
  314. for _, pkg := range repoUp {
  315. if noTargets || parser.targets.get(pkg.Name) {
  316. fmt.Printf("%s %s -> %s\n", bold(pkg.Name), green(pkg.LocalVersion), green(pkg.RemoteVersion))
  317. delete(parser.targets, pkg.Name)
  318. }
  319. }
  320. }
  321. if !parser.existsArg("n", "native") {
  322. for _, pkg := range aurUp {
  323. if noTargets || parser.targets.get(pkg.Name) {
  324. fmt.Printf("%s %s -> %s\n", bold(pkg.Name), green(pkg.LocalVersion), green(pkg.RemoteVersion))
  325. delete(parser.targets, pkg.Name)
  326. }
  327. }
  328. }
  329. missing := false
  330. outer:
  331. for pkg := range parser.targets {
  332. for _, name := range localNames {
  333. if name == pkg {
  334. continue outer
  335. }
  336. }
  337. for _, name := range remoteNames {
  338. if name == pkg {
  339. continue outer
  340. }
  341. }
  342. fmt.Println(red(bold("error:")), "package '"+pkg+"' was not found")
  343. missing = true
  344. }
  345. if missing {
  346. return fmt.Errorf("")
  347. }
  348. return nil
  349. }
  350. // Formats a unix timestamp to yyyy/mm/dd
  351. func formatTime(i int) string {
  352. t := time.Unix(int64(i), 0)
  353. return fmt.Sprintf("%d/%02d/%02d", t.Year(), int(t.Month()), t.Day())
  354. }
  355. const (
  356. redCode = "\x1b[31m"
  357. greenCode = "\x1b[32m"
  358. yellowCode = "\x1b[33m"
  359. blueCode = "\x1b[34m"
  360. magentaCode = "\x1b[35m"
  361. cyanCode = "\x1b[36m"
  362. boldCode = "\x1b[1m"
  363. resetCode = "\x1b[0m"
  364. )
  365. func stylize(startCode, in string) string {
  366. if useColor {
  367. return startCode + in + resetCode
  368. }
  369. return in
  370. }
  371. func red(in string) string {
  372. return stylize(redCode, in)
  373. }
  374. func green(in string) string {
  375. return stylize(greenCode, in)
  376. }
  377. func yellow(in string) string {
  378. return stylize(yellowCode, in)
  379. }
  380. func blue(in string) string {
  381. return stylize(blueCode, in)
  382. }
  383. func cyan(in string) string {
  384. return stylize(cyanCode, in)
  385. }
  386. func magenta(in string) string {
  387. return stylize(magentaCode, in)
  388. }
  389. func bold(in string) string {
  390. return stylize(boldCode, in)
  391. }
  392. // Colours text using a hashing algorithm. The same text will always produce the
  393. // same colour while different text will produce a different colour.
  394. func colourHash(name string) (output string) {
  395. if !useColor {
  396. return name
  397. }
  398. var hash = 5381
  399. for i := 0; i < len(name); i++ {
  400. hash = int(name[i]) + ((hash << 5) + (hash))
  401. }
  402. return fmt.Sprintf("\x1b[%dm%s\x1b[0m", hash%6+31, name)
  403. }