types.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package query
  2. import (
  3. "fmt"
  4. "io"
  5. "strconv"
  6. "github.com/Jguer/aur"
  7. "github.com/Jguer/go-alpm/v2"
  8. "github.com/leonelquinteros/gotext"
  9. "github.com/Jguer/yay/v11/pkg/db"
  10. "github.com/Jguer/yay/v11/pkg/text"
  11. )
  12. type (
  13. aurQuery []aur.Pkg // Query is a collection of Results.
  14. repoQuery []alpm.IPackage // Query holds the results of a repository search.
  15. )
  16. type aurSortable struct {
  17. aurQuery
  18. sortBy string
  19. bottomUp bool
  20. }
  21. func (r repoQuery) Reverse() {
  22. for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
  23. r[i], r[j] = r[j], r[i]
  24. }
  25. }
  26. func (r repoQuery) Less(i, j int) bool {
  27. return text.LessRunes([]rune(r[i].Name()), []rune(r[j].Name()))
  28. }
  29. func (q aurSortable) Len() int {
  30. return len(q.aurQuery)
  31. }
  32. func (q aurSortable) Less(i, j int) bool {
  33. var result bool
  34. switch q.sortBy {
  35. case "votes":
  36. result = q.aurQuery[i].NumVotes > q.aurQuery[j].NumVotes
  37. case "popularity":
  38. result = q.aurQuery[i].Popularity > q.aurQuery[j].Popularity
  39. case "name":
  40. result = text.LessRunes([]rune(q.aurQuery[i].Name), []rune(q.aurQuery[j].Name))
  41. case "base":
  42. result = text.LessRunes([]rune(q.aurQuery[i].PackageBase), []rune(q.aurQuery[j].PackageBase))
  43. case "submitted":
  44. result = q.aurQuery[i].FirstSubmitted < q.aurQuery[j].FirstSubmitted
  45. case "modified":
  46. result = q.aurQuery[i].LastModified < q.aurQuery[j].LastModified
  47. case "id":
  48. result = q.aurQuery[i].ID < q.aurQuery[j].ID
  49. case "baseid":
  50. result = q.aurQuery[i].PackageBaseID < q.aurQuery[j].PackageBaseID
  51. }
  52. if q.bottomUp {
  53. return !result
  54. }
  55. return result
  56. }
  57. func (q aurSortable) Swap(i, j int) {
  58. q.aurQuery[i], q.aurQuery[j] = q.aurQuery[j], q.aurQuery[i]
  59. }
  60. func getSearchBy(value string) aur.By {
  61. switch value {
  62. case "name":
  63. return aur.Name
  64. case "maintainer":
  65. return aur.Maintainer
  66. case "depends":
  67. return aur.Depends
  68. case "makedepends":
  69. return aur.MakeDepends
  70. case "optdepends":
  71. return aur.OptDepends
  72. case "checkdepends":
  73. return aur.CheckDepends
  74. default:
  75. return aur.NameDesc
  76. }
  77. }
  78. // PrintSearch handles printing search results in a given format.
  79. func (q aurQuery) printSearch(
  80. w io.Writer,
  81. start int,
  82. dbExecutor db.Executor,
  83. searchMode SearchVerbosity,
  84. bottomUp,
  85. singleLineResults bool,
  86. ) {
  87. for i := range q {
  88. if searchMode == Minimal {
  89. _, _ = fmt.Fprintln(w, q[i].Name)
  90. continue
  91. }
  92. var toprint string
  93. if searchMode == NumberMenu {
  94. if bottomUp {
  95. toprint += text.Magenta(strconv.Itoa(len(q)+start-i-1) + " ")
  96. } else {
  97. toprint += text.Magenta(strconv.Itoa(start+i) + " ")
  98. }
  99. }
  100. toprint += text.Bold(text.ColorHash("aur")) + "/" + text.Bold(q[i].Name) +
  101. " " + text.Cyan(q[i].Version) +
  102. text.Bold(" (+"+strconv.Itoa(q[i].NumVotes)) +
  103. " " + text.Bold(strconv.FormatFloat(q[i].Popularity, 'f', 2, 64)+") ")
  104. if q[i].Maintainer == "" {
  105. toprint += text.Bold(text.Red(gotext.Get("(Orphaned)"))) + " "
  106. }
  107. if q[i].OutOfDate != 0 {
  108. toprint += text.Bold(text.Red(gotext.Get("(Out-of-date: %s)", text.FormatTime(q[i].OutOfDate)))) + " "
  109. }
  110. if pkg := dbExecutor.LocalPackage(q[i].Name); pkg != nil {
  111. if pkg.Version() != q[i].Version {
  112. toprint += text.Bold(text.Green(gotext.Get("(Installed: %s)", pkg.Version())))
  113. } else {
  114. toprint += text.Bold(text.Green(gotext.Get("(Installed)")))
  115. }
  116. }
  117. if singleLineResults {
  118. toprint += "\t"
  119. } else {
  120. toprint += "\n "
  121. }
  122. toprint += q[i].Description
  123. _, _ = fmt.Fprintln(w, toprint)
  124. }
  125. }
  126. // PrintSearch receives a RepoSearch type and outputs pretty text.
  127. func (r repoQuery) printSearch(w io.Writer, dbExecutor db.Executor, searchMode SearchVerbosity, bottomUp, singleLineResults bool) {
  128. for i, res := range r {
  129. if searchMode == Minimal {
  130. _, _ = fmt.Fprintln(w, res.Name())
  131. continue
  132. }
  133. var toprint string
  134. if searchMode == NumberMenu {
  135. if bottomUp {
  136. toprint += text.Magenta(strconv.Itoa(len(r)-i) + " ")
  137. } else {
  138. toprint += text.Magenta(strconv.Itoa(i+1) + " ")
  139. }
  140. }
  141. toprint += text.Bold(text.ColorHash(res.DB().Name())) + "/" + text.Bold(res.Name()) +
  142. " " + text.Cyan(res.Version()) +
  143. text.Bold(" ("+text.Human(res.Size())+
  144. " "+text.Human(res.ISize())+") ")
  145. packageGroups := dbExecutor.PackageGroups(res)
  146. if len(packageGroups) != 0 {
  147. toprint += fmt.Sprint(packageGroups, " ")
  148. }
  149. if pkg := dbExecutor.LocalPackage(res.Name()); pkg != nil {
  150. if pkg.Version() != res.Version() {
  151. toprint += text.Bold(text.Green(gotext.Get("(Installed: %s)", pkg.Version())))
  152. } else {
  153. toprint += text.Bold(text.Green(gotext.Get("(Installed)")))
  154. }
  155. }
  156. if singleLineResults {
  157. toprint += "\t"
  158. } else {
  159. toprint += "\n "
  160. }
  161. toprint += res.Description()
  162. _, _ = fmt.Fprintln(w, toprint)
  163. }
  164. }