query.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package aur
  2. import (
  3. "fmt"
  4. "sort"
  5. "github.com/jguer/yay/pacman"
  6. "github.com/jguer/yay/util"
  7. )
  8. // Query is a collection of Results
  9. type Query []Result
  10. func (q Query) Len() int {
  11. return len(q)
  12. }
  13. func (q Query) Less(i, j int) bool {
  14. if util.SortMode == util.BottomUp {
  15. return q[i].NumVotes < q[j].NumVotes
  16. }
  17. return q[i].NumVotes > q[j].NumVotes
  18. }
  19. func (q Query) Swap(i, j int) {
  20. q[i], q[j] = q[j], q[i]
  21. }
  22. // PrintSearch handles printing search results in a given format
  23. func (q Query) PrintSearch(start int) {
  24. for i, res := range q {
  25. var toprint string
  26. if util.SearchVerbosity == util.NumberMenu {
  27. if util.SortMode == util.BottomUp {
  28. toprint += fmt.Sprintf("%d ", len(q)+start-i-1)
  29. } else {
  30. toprint += fmt.Sprintf("%d ", start+i)
  31. }
  32. } else if util.SearchVerbosity == util.Minimal {
  33. fmt.Println(res.Name)
  34. continue
  35. }
  36. toprint += fmt.Sprintf("\x1b[1m%s/\x1b[33m%s \x1b[36m%s \x1b[0m(%d) ", "aur", res.Name, res.Version, res.NumVotes)
  37. if res.Maintainer == "" {
  38. toprint += fmt.Sprintf("\x1b[31;40m(Orphaned)\x1b[0m ")
  39. }
  40. if res.OutOfDate != 0 {
  41. toprint += fmt.Sprintf("\x1b[31;40m(Out-of-date)\x1b[0m ")
  42. }
  43. if res.Installed == true {
  44. toprint += fmt.Sprintf("\x1b[32;40mInstalled\x1b[0m")
  45. }
  46. toprint += "\n" + res.Description
  47. fmt.Println(toprint)
  48. }
  49. return
  50. }
  51. // Info returns an AUR search with package details
  52. func Info(pkg string) (Query, int, error) {
  53. type returned struct {
  54. Results Query `json:"results"`
  55. ResultCount int `json:"resultcount"`
  56. }
  57. r := returned{}
  58. err := getJSON("https://aur.archlinux.org/rpc/?v=5&type=info&arg[]="+pkg, &r)
  59. return r.Results, r.ResultCount, err
  60. }
  61. // MultiInfo takes a slice of strings and returns a slice with the info of each package
  62. func MultiInfo(pkgS []string) (Query, int, error) {
  63. type returned struct {
  64. Results Query `json:"results"`
  65. ResultCount int `json:"resultcount"`
  66. }
  67. r := returned{}
  68. var pkg string
  69. for _, pkgn := range pkgS {
  70. pkg += "&arg[]=" + pkgn
  71. }
  72. err := getJSON("https://aur.archlinux.org/rpc/?v=5&type=info"+pkg, &r)
  73. return r.Results, r.ResultCount, err
  74. }
  75. // Search returns an AUR search
  76. func Search(pkg string, sortS bool) (Query, int, error) {
  77. type returned struct {
  78. Results Query `json:"results"`
  79. ResultCount int `json:"resultcount"`
  80. }
  81. r := returned{}
  82. err := getJSON("https://aur.archlinux.org/rpc/?v=5&type=search&arg="+pkg, &r)
  83. if sortS {
  84. sort.Sort(r.Results)
  85. }
  86. setter := pacman.PFactory(pFSetTrue)
  87. for i, res := range r.Results {
  88. if i == len(r.Results)-1 {
  89. setter(res.Name, &r.Results[i], true)
  90. continue
  91. }
  92. setter(res.Name, &r.Results[i], false)
  93. }
  94. return r.Results, r.ResultCount, err
  95. }
  96. // This is very dirty but it works so good.
  97. func pFSetTrue(res interface{}) {
  98. f, ok := res.(*Result)
  99. if !ok {
  100. fmt.Println("Unable to convert back to Result")
  101. return
  102. }
  103. f.Installed = true
  104. return
  105. }
  106. // MissingPackage warns if the Query was unable to find a package
  107. func (q Query) MissingPackage(pkgS []string) {
  108. for _, depName := range pkgS {
  109. found := false
  110. for _, dep := range q {
  111. if dep.Name == depName {
  112. found = true
  113. break
  114. }
  115. }
  116. if !found {
  117. fmt.Println("\x1b[31mUnable to find", depName, "in AUR\x1b[0m")
  118. }
  119. }
  120. return
  121. }