types.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 += aurPkgSearchString(&q[i], dbExecutor, singleLineResults)
  101. _, _ = fmt.Fprintln(w, toprint)
  102. }
  103. }
  104. func aurPkgSearchString(
  105. pkg *aur.Pkg,
  106. dbExecutor db.Executor,
  107. singleLineResults bool,
  108. ) string {
  109. toPrint := text.Bold(text.ColorHash("aur")) + "/" + text.Bold(pkg.Name) +
  110. " " + text.Cyan(pkg.Version) +
  111. text.Bold(" (+"+strconv.Itoa(pkg.NumVotes)) +
  112. " " + text.Bold(strconv.FormatFloat(pkg.Popularity, 'f', 2, 64)+") ")
  113. if pkg.Maintainer == "" {
  114. toPrint += text.Bold(text.Red(gotext.Get("(Orphaned)"))) + " "
  115. }
  116. if pkg.OutOfDate != 0 {
  117. toPrint += text.Bold(text.Red(gotext.Get("(Out-of-date: %s)", text.FormatTime(pkg.OutOfDate)))) + " "
  118. }
  119. if localPkg := dbExecutor.LocalPackage(pkg.Name); localPkg != nil {
  120. if localPkg.Version() != pkg.Version {
  121. toPrint += text.Bold(text.Green(gotext.Get("(Installed: %s)", localPkg.Version())))
  122. } else {
  123. toPrint += text.Bold(text.Green(gotext.Get("(Installed)")))
  124. }
  125. }
  126. if singleLineResults {
  127. toPrint += "\t"
  128. } else {
  129. toPrint += "\n "
  130. }
  131. toPrint += pkg.Description
  132. return toPrint
  133. }
  134. // PrintSearch receives a RepoSearch type and outputs pretty text.
  135. func (r repoQuery) printSearch(w io.Writer, dbExecutor db.Executor, searchMode SearchVerbosity, bottomUp, singleLineResults bool) {
  136. for i, res := range r {
  137. if searchMode == Minimal {
  138. _, _ = fmt.Fprintln(w, res.Name())
  139. continue
  140. }
  141. var toprint string
  142. if searchMode == NumberMenu {
  143. if bottomUp {
  144. toprint += text.Magenta(strconv.Itoa(len(r)-i) + " ")
  145. } else {
  146. toprint += text.Magenta(strconv.Itoa(i+1) + " ")
  147. }
  148. }
  149. toprint += syncPkgSearchString(res, dbExecutor, singleLineResults)
  150. _, _ = fmt.Fprintln(w, toprint)
  151. }
  152. }
  153. // PrintSearch receives a RepoSearch type and outputs pretty text.
  154. func syncPkgSearchString(pkg alpm.IPackage, dbExecutor db.Executor, singleLineResults bool) string {
  155. toPrint := text.Bold(text.ColorHash(pkg.DB().Name())) + "/" + text.Bold(pkg.Name()) +
  156. " " + text.Cyan(pkg.Version()) +
  157. text.Bold(" ("+text.Human(pkg.Size())+
  158. " "+text.Human(pkg.ISize())+") ")
  159. packageGroups := dbExecutor.PackageGroups(pkg)
  160. if len(packageGroups) != 0 {
  161. toPrint += fmt.Sprint(packageGroups, " ")
  162. }
  163. if localPkg := dbExecutor.LocalPackage(pkg.Name()); localPkg != nil {
  164. if localPkg.Version() != pkg.Version() {
  165. toPrint += text.Bold(text.Green(gotext.Get("(Installed: %s)", localPkg.Version())))
  166. } else {
  167. toPrint += text.Bold(text.Green(gotext.Get("(Installed)")))
  168. }
  169. }
  170. if singleLineResults {
  171. toPrint += "\t"
  172. } else {
  173. toPrint += "\n "
  174. }
  175. toPrint += pkg.Description()
  176. return toPrint
  177. }