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