query.go 3.0 KB

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