| 1 | module main |
| 2 | |
| 3 | import nspkg |
| 4 | import term |
| 5 | |
| 6 | enum PrintMode { |
| 7 | default |
| 8 | minimal |
| 9 | } |
| 10 | |
| 11 | // Listing is a package list as search and orphans collect it: the fields |
| 12 | // asked for, and one entry per package with its values as plain text. print |
| 13 | // is the terminal's rendering of it |
| 14 | struct Listing { |
| 15 | pub mut: |
| 16 | mode PrintMode |
| 17 | fields []string |
| 18 | entries []ListEntry |
| 19 | } |
| 20 | |
| 21 | // ListEntry is one package of a listing: its name, and its value of every |
| 22 | // field of the listing, in their order. the quiet mode collects the name |
| 23 | // alone. group is the group a search -g member was listed for |
| 24 | struct ListEntry { |
| 25 | name string |
| 26 | values []string |
| 27 | group string @[omitempty] |
| 28 | } |
| 29 | |
| 30 | fn shown(value string) string { |
| 31 | return if value == '' { '-' } else { value } |
| 32 | } |
| 33 | |
| 34 | fn field_cell(field string, value string) Cell { |
| 35 | text := shown(value) |
| 36 | return match field { |
| 37 | 'name' { cell(name_style(text)) } |
| 38 | 'version' { cell(version_style(text)) } |
| 39 | 'source' { cell(source_style(text)) } |
| 40 | else { cell(text) } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | fn (mut l Listing) add_pkg(pkg &nspkg.Package, source string) { |
| 45 | l.entries << ListEntry{ |
| 46 | name: pkg.get_name() |
| 47 | values: if l.mode == .minimal { |
| 48 | []string{} |
| 49 | } else { |
| 50 | l.fields.map(pkg_field(pkg, source, it)) |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // add_member adds a member of a group, search -g's |
| 56 | fn (mut l Listing) add_member(pkg &nspkg.Package, source string, group string) { |
| 57 | l.add_pkg(pkg, source) |
| 58 | last := l.entries.pop() |
| 59 | l.entries << ListEntry{ |
| 60 | ...last |
| 61 | group: group |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | fn (mut l Listing) add_pkgbuild(info PackagePkgbuild, source string) { |
| 66 | l.entries << ListEntry{ |
| 67 | name: info.name |
| 68 | values: if l.mode == .minimal { |
| 69 | []string{} |
| 70 | } else { |
| 71 | l.fields.map(pkgbuild_field(info, it, source)) |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // print renders the listing: the quiet mode one name a line, the default a |
| 77 | // table with every value styled by its field |
| 78 | fn (l Listing) print() { |
| 79 | if ipc_on() { |
| 80 | emit(ListingEvent{ |
| 81 | fields: if l.mode == .minimal { []string{} } else { l.fields } |
| 82 | entries: l.entries |
| 83 | }) |
| 84 | return |
| 85 | } |
| 86 | if l.mode == .minimal { |
| 87 | for e in l.entries { |
| 88 | println(e.name) |
| 89 | } |
| 90 | return |
| 91 | } |
| 92 | if l.entries.len > 0 { |
| 93 | // a listing of group members leads each row with its group, as |
| 94 | // pacman's -Sg does and as the ipc entries carry it |
| 95 | grouped := l.entries.any(it.group != '') |
| 96 | mut rows := []Row{} |
| 97 | for e in l.entries { |
| 98 | mut r := listing_row(l.fields, e) |
| 99 | if grouped { |
| 100 | r.cols.insert(0, cell(shown(e.group))) |
| 101 | } |
| 102 | rows << r |
| 103 | } |
| 104 | println(table_text(rows, '', ' ')) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // print_found is the output of a listing whose collection failed part way: |
| 109 | // the quiet mode names what was found, the default prints nothing |
| 110 | fn (l Listing) print_found() { |
| 111 | if l.mode == .minimal { |
| 112 | l.print() |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | fn listing_row(fields []string, e ListEntry) Row { |
| 117 | mut cols := []Cell{} |
| 118 | // an entry always carries one value per field of its listing; a mismatch |
| 119 | // shortens the row instead of tripping a bounds panic mid-render |
| 120 | for i, f in fields { |
| 121 | value := if i < e.values.len { e.values[i] } else { '' } |
| 122 | cols << field_cell(f, value) |
| 123 | } |
| 124 | return Row{ |
| 125 | cols: cols |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // reason_to_str is an install reason as a person reads it; reason_key is |
| 130 | // the same reason as a frontend reads it. every reason is named in both, no |
| 131 | // else: one libalpm adds fails the build here, rather than reading as |
| 132 | // 'unknown' in one spelling and something else in the other |
| 133 | pub fn reason_to_str(reason nspkg.PackageReason) string { |
| 134 | return match reason { |
| 135 | .explicit { 'explicitly installed' } |
| 136 | .depend { 'installed as a dependency' } |
| 137 | .unknown { 'unknown' } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | pub fn validation_to_str(validation nspkg.PkgValidation) string { |
| 142 | if validation == .unknown { |
| 143 | return 'unknown' |
| 144 | } |
| 145 | if validation == .none { |
| 146 | return 'none' |
| 147 | } |
| 148 | mut parts := []string{} |
| 149 | if int(validation) & int(nspkg.PkgValidation.md5sum) != 0 { |
| 150 | parts << 'MD5 checksum' |
| 151 | } |
| 152 | if int(validation) & int(nspkg.PkgValidation.sha256sum) != 0 { |
| 153 | parts << 'SHA-256 checksum' |
| 154 | } |
| 155 | if int(validation) & int(nspkg.PkgValidation.signature) != 0 { |
| 156 | parts << 'signature' |
| 157 | } |
| 158 | return parts.join(', ') |
| 159 | } |
| 160 | |
| 161 | fn depends_text(deps &nspkg.List) string { |
| 162 | mut d := unsafe { deps } |
| 163 | return d.depends().map(|dep| dep.compute_string()).join(', ') |
| 164 | } |
| 165 | |
| 166 | // InfoField is one field of a package as info collects it: the field's name, |
| 167 | // its value as one piece of text, and for the list fields the items that the |
| 168 | // terminal fills into lines |
| 169 | struct InfoField { |
| 170 | field string |
| 171 | value string |
| 172 | items []string |
| 173 | } |
| 174 | |
| 175 | // wrap_items fills as many items into one line as the width allows, joining |
| 176 | // with ', '. the cells are measured the way the table prints them, so styled |
| 177 | // and wide text wrap right |
| 178 | fn wrap_items(items []string, avail int) []string { |
| 179 | mut wide := avail |
| 180 | if wide < 1 { |
| 181 | wide = 1 |
| 182 | } |
| 183 | mut out := []string{} |
| 184 | mut line := '' |
| 185 | for item in items { |
| 186 | cand := if line == '' { item } else { '${line}, ${item}' } |
| 187 | if line != '' && visible_width(cand) > wide { |
| 188 | out << line |
| 189 | line = item |
| 190 | } else { |
| 191 | line = cand |
| 192 | } |
| 193 | } |
| 194 | if line != '' { |
| 195 | out << line |
| 196 | } |
| 197 | return if out.len == 0 { [''] } else { out } |
| 198 | } |
| 199 | |
| 200 | // info_fields collects what info shows of a package: the fields asked for, |
| 201 | // in order. an empty one is left out unless the fields were named on the |
| 202 | // command line (explicit) |
| 203 | fn info_fields(pkg &nspkg.Package, fields []string, explicit bool) []InfoField { |
| 204 | source := pkg_source(pkg) |
| 205 | mut out := []InfoField{} |
| 206 | for f in fields { |
| 207 | value := pkg_field(pkg, source, f) |
| 208 | if value == '' && !explicit { |
| 209 | continue |
| 210 | } |
| 211 | out << InfoField{ |
| 212 | field: f |
| 213 | value: value |
| 214 | items: pkg_field_items(pkg, f) or { []string{} }.filter(it != '') |
| 215 | } |
| 216 | } |
| 217 | return out |
| 218 | } |
| 219 | |
| 220 | // the width info wraps to when the terminal tells none (output into a pipe |
| 221 | // or a file answers 0), and the narrowest a terminal may answer before its |
| 222 | // answer is taken for none: a column count that small is no real terminal |
| 223 | const info_fallback_cols = 80 |
| 224 | const info_min_cols = 9 |
| 225 | |
| 226 | // info_width is the width the info table wraps to for a terminal's columns |
| 227 | fn info_width(cols int) int { |
| 228 | return if cols >= info_min_cols { cols } else { info_fallback_cols } |
| 229 | } |
| 230 | |
| 231 | // info_table renders a package's fields. the list fields fill as many items |
| 232 | // per line as the terminal lets them, opt depends give every line its own, |
| 233 | // and the continuation lines are padded from the start to the value column |
| 234 | fn info_table(fields []InfoField) string { |
| 235 | if fields.len == 0 { |
| 236 | // the fields list is empty: nothing to show |
| 237 | return '' |
| 238 | } |
| 239 | cols, _ := term.get_terminal_size() |
| 240 | width := info_width(cols) |
| 241 | labels := fields.map(it.field.replace('_', ' ')) |
| 242 | mut label_w := 0 |
| 243 | for l in labels { |
| 244 | if l.len > label_w { |
| 245 | label_w = l.len |
| 246 | } |
| 247 | } |
| 248 | pad := ' '.repeat(label_w + 2) |
| 249 | avail := width - label_w - 2 |
| 250 | mut text := []string{} |
| 251 | for i, f in fields { |
| 252 | lines := if f.field == 'optdepends' { f.items } else { wrap_items(f.items, avail) } |
| 253 | first := if f.items.len == 0 { f.value } else { lines[0] } |
| 254 | text << '${heading(labels[i])}${' '.repeat(label_w - labels[i].len + 2)}${shown(first)}' |
| 255 | // an empty optdepends has no lines at all: -F all shows it as '-' |
| 256 | for line in lines#[1..] { |
| 257 | text << '${pad}${line}' |
| 258 | } |
| 259 | } |
| 260 | return text.join('\n') |
| 261 | } |
| 262 | |