module main import alpm import term enum PrintMode { default minimal } // Listing is a package list as search and orphans collect it: the fields // asked for, and one entry per package with its values as plain text. print // is the terminal's rendering of it struct Listing { pub mut: mode PrintMode fields []string entries []ListEntry } // ListEntry is one package of a listing: its name, and its value of every // field of the listing, in their order. the quiet mode collects the name // alone. group is the group a search -g member was listed for struct ListEntry { name string values []string group string @[omitempty] } fn shown(value string) string { return if value == '' { '-' } else { value } } fn field_cell(field string, value string) Cell { text := shown(value) return match field { 'name' { cell(name_style(text)) } 'version' { cell(version_style(text)) } 'source' { cell(source_style(text)) } else { cell(text) } } } fn (mut l Listing) add_pkg(pkg &alpm.Package, source string) { l.entries << ListEntry{ name: pkg.get_name() values: if l.mode == .minimal { []string{} } else { l.fields.map(pkg_field(pkg, source, it)) } } } // add_member adds a member of a group, search -g's fn (mut l Listing) add_member(pkg &alpm.Package, source string, group string) { l.add_pkg(pkg, source) last := l.entries.pop() l.entries << ListEntry{ ...last group: group } } fn (mut l Listing) add_pkgbuild(info PackagePkgbuild, source string) { l.entries << ListEntry{ name: info.name values: if l.mode == .minimal { []string{} } else { l.fields.map(pkgbuild_field(info, it, source)) } } } // print renders the listing: the quiet mode one name a line, the default a // table with every value styled by its field fn (l Listing) print() { if ipc_on() { emit(ListingEvent{ fields: if l.mode == .minimal { []string{} } else { l.fields } entries: l.entries }) return } if l.mode == .minimal { for e in l.entries { println(e.name) } return } if l.entries.len > 0 { // a listing of group members leads each row with its group, as // pacman's -Sg does and as the ipc entries carry it grouped := l.entries.any(it.group != '') mut rows := []Row{} for e in l.entries { mut r := listing_row(l.fields, e) if grouped { r.cols.insert(0, cell(shown(e.group))) } rows << r } println(table_text(rows, '', ' ')) } } // print_found is the output of a listing whose collection failed part way: // the quiet mode names what was found, the default prints nothing fn (l Listing) print_found() { if l.mode == .minimal { l.print() } } fn listing_row(fields []string, e ListEntry) Row { mut cols := []Cell{} // an entry always carries one value per field of its listing; a mismatch // shortens the row instead of tripping a bounds panic mid-render for i, f in fields { value := if i < e.values.len { e.values[i] } else { '' } cols << field_cell(f, value) } return Row{ cols: cols } } // reason_to_str is an install reason as a person reads it; reason_key is // the same reason as a frontend reads it. every reason is named in both, no // else: one libalpm adds fails the build here, rather than reading as // 'unknown' in one spelling and something else in the other pub fn reason_to_str(reason alpm.PackageReason) string { return match reason { .explicit { 'explicitly installed' } .depend { 'installed as a dependency' } .unknown { 'unknown' } } } pub fn validation_to_str(validation alpm.PkgValidation) string { if validation == .unknown { return 'unknown' } if validation == .none { return 'none' } mut parts := []string{} if int(validation) & int(alpm.PkgValidation.md5sum) != 0 { parts << 'MD5 checksum' } if int(validation) & int(alpm.PkgValidation.sha256sum) != 0 { parts << 'SHA-256 checksum' } if int(validation) & int(alpm.PkgValidation.signature) != 0 { parts << 'signature' } return parts.join(', ') } fn depends_text(deps &alpm.List) string { mut d := unsafe { deps } return d.depends().map(|dep| dep.compute_string()).join(', ') } // InfoField is one field of a package as info collects it: the field's name, // its value as one piece of text, and for the list fields the items that the // terminal fills into lines struct InfoField { field string value string items []string } // wrap_items fills as many items into one line as the width allows, joining // with ', '. the cells are measured the way the table prints them, so styled // and wide text wrap right fn wrap_items(items []string, avail int) []string { mut wide := avail if wide < 1 { wide = 1 } mut out := []string{} mut line := '' for item in items { cand := if line == '' { item } else { '${line}, ${item}' } if line != '' && visible_width(cand) > wide { out << line line = item } else { line = cand } } if line != '' { out << line } return if out.len == 0 { [''] } else { out } } // info_fields collects what info shows of a package: the fields asked for, // in order. an empty one is left out unless the fields were named on the // command line (explicit) fn info_fields(pkg &alpm.Package, fields []string, explicit bool) []InfoField { source := pkg_source(pkg) mut out := []InfoField{} for f in fields { value := pkg_field(pkg, source, f) if value == '' && !explicit { continue } out << InfoField{ field: f value: value items: pkg_field_items(pkg, f) or { []string{} }.filter(it != '') } } return out } // the width info wraps to when the terminal tells none (output into a pipe // or a file answers 0), and the narrowest a terminal may answer before its // answer is taken for none: a column count that small is no real terminal const info_fallback_cols = 80 const info_min_cols = 9 // info_width is the width the info table wraps to for a terminal's columns fn info_width(cols int) int { return if cols >= info_min_cols { cols } else { info_fallback_cols } } // info_table renders a package's fields. the list fields fill as many items // per line as the terminal lets them, opt depends give every line its own, // and the continuation lines are padded from the start to the value column fn info_table(fields []InfoField) string { if fields.len == 0 { // the fields list is empty: nothing to show return '' } cols, _ := term.get_terminal_size() width := info_width(cols) labels := fields.map(it.field.replace('_', ' ')) mut label_w := 0 for l in labels { if l.len > label_w { label_w = l.len } } pad := ' '.repeat(label_w + 2) avail := width - label_w - 2 mut text := []string{} for i, f in fields { lines := if f.field == 'optdepends' { f.items } else { wrap_items(f.items, avail) } first := if f.items.len == 0 { f.value } else { lines[0] } text << '${heading(labels[i])}${' '.repeat(label_w - labels[i].len + 2)}${shown(first)}' // an empty optdepends has no lines at all: -F all shows it as '-' for line in lines#[1..] { text << '${pad}${line}' } } return text.join('\n') }