@[has_globals] module main import alpm import time import v.profiler // pkg_field_names are the fields info and search can show. both take the same // names through --fields, so a field means the same thing in either command const pkg_field_names = ['source', 'name', 'version', 'description', 'arch', 'url', 'licenses', 'groups', 'provides', 'depends', 'optdepends', 'required_by', 'optional_for', 'conflicts', 'replaces', 'size', 'packager', 'build_date', 'install_date', 'install_reason', 'install_script', 'validation'] // search_default_fields are the columns of a search unless --fields says otherwise const search_default_fields = ['source', 'name', 'version', 'description'] // cfg_search_fields are the columns the config asks for, (search :fields): // search_default_fields unless it says otherwise. cfg_search_thold is the // fuzzy search's threshold when --thold names none, (search :thold) __global cfg_search_fields = ['source', 'name', 'version', 'description'] __global cfg_search_thold = 0.9 // what a mode starts from when its flag names nothing: (clean :keep), the // versions of a package the cache keeps, and (tree :depth), how deep a tree // is drawn; 0 leaves each to the mode __global cfg_clean_keep = 0 __global cfg_tree_depth = 0 // parse_fields reads --fields: nothing gives the defaults, 'all' every field, // and anything else a comma separated list of field names fn parse_fields(spec string, defaults []string) ![]string { if spec == '' { return defaults } if spec == 'all' { return pkg_field_names } mut out := []string{} for f in spec.split(',') { name := f.trim_space() if name !in pkg_field_names { return error('unknown field "${name}", the fields are: ${pkg_field_names.join(', ')}') } out << name } return out } // strs_text is a list of strings as one value: 'a, b'. the list arrives as a // borrowed pointer, and strs() walks it without writing to it — the mut // binding is only the mutable receiver the method spells, never a write to // the C list fn strs_text(list &alpm.List) string { mut l := unsafe { list } return l.strs().join(', ') } // pkg_source is where an alpm package comes from: 'local', or the sync repo fn pkg_source(pkg &alpm.Package) string { origin := pkg.get_origin() if origin == .file { return 'file' } if origin != .syncdb { return 'local' } db := pkg.get_db() or { return 'remote' } return db.get_name() } // pkg_field_items gives a field's value as separate items, for the fields that // wrap their lists across lines. none for the fields whose value is one piece // of text fn pkg_field_items(pkg &alpm.Package, field string) ?[]string { return match field { 'licenses' { pkg.get_licenses().strs() } 'groups' { pkg.get_groups().strs() } 'provides' { pkg.get_provides().depends().map(|d| d.compute_string()) } 'depends' { pkg.get_depends().depends().map(|d| d.compute_string()) } // for a sync package libalpm looks through every sync db, as pacman -Sii 'required_by' { pkg.compute_required_by() } 'optional_for' { pkg.compute_optional_for() } 'conflicts' { pkg.get_conflicts().depends().map(|d| d.compute_string()) } 'replaces' { pkg.get_replaces().depends().map(|d| d.compute_string()) } // every optional dependency of its own line: name, then its reason. // an installed one carries pacman's [installed] mark 'optdepends' { pkg.get_opt_depends().depends().map(|d| optdepend_text(d, pkg)) } else { none } } } // optdepend_text is one line of the optional deps: name, then its reason. an // installed package marks the deps the local db satisfies, as pacman's -Qi // does; a sync package's list stays bare, like pacman's -Si fn optdepend_text(d alpm.Depend, pkg &alpm.Package) string { name := d.get_name() desc := d.get_desc() text := if desc == '' { name } else { '${name}: ${desc}' } // the local cache hangs off the package's own db, no separate lookup if pkg.get_origin() != .localdb { return text } if db := pkg.get_db() { if cache := db.get_pkgcache() { if _ := cache.find_satisfier(d.compute_string()) { return text + ' [installed]' } } } return text } // pkg_field is one field of an alpm package. '' when the field does not apply, // such as the install fields of a package that is not installed fn pkg_field(pkg &alpm.Package, source string, field string) string { sync := pkg.get_origin() == .syncdb return match field { 'source' { source } 'name' { pkg.get_name() } 'version' { pkg.get_version() } 'description' { pkg.get_desc() } 'arch' { pkg.get_arch() } 'url' { pkg.get_url() } 'licenses' { strs_text(pkg.get_licenses()) } 'groups' { strs_text(pkg.get_groups()) } 'provides' { depends_text(pkg.get_provides()) } 'depends' { depends_text(pkg.get_depends()) } // the mark rides along here too, since info's one-value fields and // the listing columns of search read the same sense from the field: // an installed package marks the deps the local db satisfies, as // pacman's -Qi and -Ss listings do 'optdepends' { pkg.get_opt_depends().depends().map(|d| optdepend_text(d, pkg)).join(', ') } 'required_by' { pkg.compute_required_by().join(', ') } 'optional_for' { pkg.compute_optional_for().join(', ') } 'conflicts' { depends_text(pkg.get_conflicts()) } 'replaces' { depends_text(pkg.get_replaces()) } 'size' { if sync { profiler.format_bytes(u64(pkg.download_size())) + ' to download' } else { profiler.format_bytes(u64(pkg.get_isize())) } } 'packager' { pkg.get_packager() } 'build_date' { time.unix(pkg.get_build_date()).format_ss() } 'install_date' { if sync { '' } else { time.unix(pkg.get_install_date()).format_ss() } } 'install_reason' { if sync { '' } else { reason_to_str(pkg.get_reason()) } } 'install_script' { if sync { '' } else { if pkg.has_scriptlet() { 'yes' } else { 'no' } } } 'validation' { validation_to_str(pkg.get_validation()) } else { '' } } } // pkgbuild_field is one field of a pkgbuild result. the rpc only knows some of // them; the source is the repo's own name fn pkgbuild_field(info PackagePkgbuild, field string, source string) string { return match field { 'source' { source } 'name' { info.name } 'version' { info.version } 'description' { info.description } 'url' { info.url } 'packager' { info.maintainer or { '' } } else { '' } } }