module main import alpm import strings // terms_match is the classic match: every term is in the name or in the // description, case ignored. no terms match everything, as pacman's -Ss // with no target lists everything fn terms_match(name string, desc string, terms []string) bool { n, d := name.to_lower(), desc.to_lower() for term in terms { t := term.to_lower() if !n.contains(t) && !d.contains(t) { return false } } return true } // match_classic is the classic match of a libalpm package fn match_classic(pkg &alpm.Package, config ConfigSearch) bool { return terms_match(pkg.get_name(), pkg.get_desc(), config.query) } fn jw_if_match(config ConfigSearch, needle string, haystack string) ?f64 { score := strings.jaro_winkler_similarity(needle, haystack) if score >= config.thold { return score } return none } // match_fuzzy_name needs every term to match the name, and scores the name by // the average of the terms fn match_fuzzy_name(pkg &alpm.Package, config ConfigSearch) ?f64 { if config.query.len == 0 { return none } name := pkg.get_name().to_lower() mut total := 0.0 for term in config.query { score := jw_if_match(config, term.to_lower(), name) or { return none } total += score } return total / f64(config.query.len) } // fuzzy_desc_hit is true when one word of the description matches the term fn fuzzy_desc_hit(config ConfigSearch, term string, words []string) bool { for h in words { if _ := jw_if_match(config, term, h) { return true } } return false } fn match_fuzzy_desc(pkg &alpm.Package, config ConfigSearch) bool { if config.query.len == 0 { return false } words := pkg.get_desc().to_lower().split(' ').filter(|word| word.runes().len > 1) for term in config.query { if !fuzzy_desc_hit(config, term.to_lower(), words) { return false } } return true } struct Scored { pkg &alpm.Package score f64 } fn build_matches(pkgs []&alpm.Package, config ConfigSearch) []&alpm.Package { return if config.fuzzy { mut m_name := []Scored{cap: pkgs.len} for pkg in pkgs { if s := match_fuzzy_name(pkg, config) { m_name << Scored{pkg, s} } } // strong name matches lead; desc-only matches trail them. a package // listed for its name is not listed again for its description: the // names matched are a set, one lookup per package instead of a scan // of the matches (one call reads one db, where names are unique) m_name.sort(a.score > b.score) m_name_pkg := m_name.map(|pkg| pkg.pkg) mut by_name := map[string]bool{} for pkg in m_name_pkg { by_name[pkg.get_name()] = true } m_desc_pkg := pkgs.filter(|pkg| !by_name[pkg.get_name()] && match_fuzzy_desc(pkg, config)) // one new list, the name matches then the description ones: neither // of the two it is made of is written to mut matches := []&alpm.Package{cap: m_name_pkg.len + m_desc_pkg.len} matches << m_name_pkg matches << m_desc_pkg matches } else { pkgs.filter(|pkg| match_classic(pkg, config)) } } // pkgbuild_matches is the classic match for AUR results, which only have a name and // a description fn pkgbuild_matches(pkg PackagePkgbuild, terms []string) bool { return terms_match(pkg.name, pkg.description, terms) } // filtered is true when a filter flag asks for installed packages with a property fn (config ConfigSearch) filtered() bool { return config.deps || config.explicit || config.foreign || config.native || config.unrequired || config.upgrades } // GroupRef is one group of a db, as the bare group listing collects it: a // sync db's, or the local db's (repo 'local') with -l struct GroupRef { repo string name string } // group_source is how a listing names a db: the local one is 'local', as // the installed packages are everywhere else fn group_source(handle &alpm.Handle, db &alpm.DB) string { if localdb := handle.get_localdb() { if voidptr(localdb) == voidptr(db) { return 'local' } } return db.get_name() } // sync_groups lists every group of the dbs, db by db fn sync_groups(handle &alpm.Handle, dbs []&alpm.DB) []GroupRef { mut out := []GroupRef{} for db in dbs { mut node := db.get_groupcache() or { continue } for node != unsafe { nil } { if group := node.get_group() { out << GroupRef{group_source(handle, db), group.get_name()} } node = node.next } } return out } // scope_repo narrows a search to one repo when its first name is one, like // 'vpac s -r extra zsh': the dbs left, and the query without the repo fn scope_repo(dbs []&alpm.DB, query []string) ([]&alpm.DB, []string) { if query.len > 1 && dbs.any(it.get_name() == query[0]) { return dbs.filter(it.get_name() == query[0]), query[1..] } return dbs, query } // collect_group_members lists the members of the named groups of dbs: the // sync dbs, or the local db for pacman's -Qg. the filters look at the // installed packages: a member that is not installed cannot pass them, and // sync_dbs is what the sync-comparing ones compare with fn collect_group_members(mut listing Listing, handle &alpm.Handle, dbs []&alpm.DB, sync_dbs []&alpm.DB, config ConfigSearch) ! { for name in config.query { mut found := false // a group several dbs carry lists each member once, from the first // db that has it, as alpm_find_group_pkgs does mut seen := map[string]bool{} for db in dbs { group := db.get_group(name) or { continue } found = true for pkg in group.packages.pkgs() { if seen[pkg.get_name()] { continue } seen[pkg.get_name()] = true local := handle.find_local_pkg(pkg.get_name()) if config.installed && local == none { continue } if config.filtered() { l := local or { continue } if !pkg_matches_filter(l, config, sync_dbs) { continue } } listing.add_member(pkg, group_source(handle, db), name) } } // as pacman's -Sg: one missing group fails the run, the rest list if !found { target_error('group "${name}" not found') } } } // collect_search lists the matches of the query: the installed packages, the // sync dbs and the pkgbuild repos, each when the config asks for it fn collect_search(mut listing Listing, mut ui TransUI, mut handle &alpm.Handle, config ConfigSearch) ! { if config.local { mut pkgcache := local_pkgcache(handle)! sync_dbs := filter_sync_dbs(mut ui, mut handle, config)! installed := pkgcache.pkgs().filter(|pkg| pkg_matches_filter(pkg, config, sync_dbs)) for pkg in build_matches(installed, config) { listing.add_pkg(pkg, 'local') } } if config.remote { mut scope := config dbs, query := scope_repo(init_repos(mut handle)!, config.query) scope.query = query if scope.sync_dbs { check_ask_db_lock(mut ui) update_repos(mut handle, false)! } else { check_sync_fresh(mut ui, mut handle)! } for db in dbs { mut pkgcache := pkgcache_of(handle, db)! for pkg in build_matches(pkgcache.pkgs(), scope) { if scope.installed && handle.find_local_pkg(pkg.get_name()) == none { continue } listing.add_pkg(pkg, db.get_name()) } } } if config.pkgbuild { mut brepos := pkgbuild_repos() if brepos.len == 0 { return error('no pkgbuild repo in the config: add (repo :name "aur" :type "pkgbuild" :src "https://aur.archlinux.org")') } // a first name that is a pkgbuild repo narrows the search to it mut bquery := config.query.clone() if bquery.len > 1 { known := brepos.map(it.name) if bquery[0] in known { brepos = brepos.filter(it.name == bquery[0]) bquery = bquery[1..] } } // each pkgbuild repo is searched on its own site, and its results // carry the repo's name needle := if bquery.len > 0 { bquery[0] } else { '' } rest := if bquery.len > 1 { bquery[1..] } else { []string{} } for repo in brepos { pkgs := get_pkgbuild_packages(repo.servers[0].trim_right('/'), needle) or { return err } for pkg in pkgs { if pkgbuild_matches(pkg, rest) { if config.installed && handle.find_local_pkg(pkg.name) == none { continue } listing.add_pkgbuild(pkg, repo.name) } } } } } // search collects the matches, then prints them. a search that fails part // way prints no partial table, but its quiet listing still names what was // found before the error, the way a stream of names does fn search(config ConfigSearch) ! { if config.filtered() && !config.groups && (config.remote || config.pkgbuild) { return error('the filters only apply to installed packages: use them with -l, not -r or -a') } // the groups are the installed packages' or the sync dbs', pacman's -Qg // or -Sg: asked for both, one of the two would be dropped without a word if config.groups && config.local && config.remote { return error('search -g lists the groups of the installed packages (-l) or of the sync dbs (-r), not both at once') } mut ui := new_trans_ui(noconfirm: config.noconfirm) mut handle := init_handle(ui)! defer { handle.release() or { went_on('release the alpm handle', err) } } defer { ui.stop() } fields := parse_fields(config.fields, cfg_search_fields)! mut listing := Listing{ mode: if config.quiet { PrintMode.minimal } else { PrintMode.default } fields: fields } if config.groups { mut scope := config mut dbs := []&alpm.DB{} mut sync_dbs := []&alpm.DB{} if config.local { // pacman's -Qg: the groups of the installed packages, read from // the local db. the sync dbs come in only for a filter that // compares with them dbs << handle.get_localdb() or { return error('could not open the local database: ${err.msg()}') } sync_dbs = filter_sync_dbs(mut ui, mut handle, scope)! } else { // the same repo scoping narrows the group listing scoped, query := scope_repo(init_repos(mut handle)!, config.query) dbs = scoped.clone() scope.query = query.clone() // the sync-comparing filters (--native, --foreign, --upgrades) reach // into these dbs through pkg_matches_filter, so they run the same // freshness check the installed-package listing does. the dbs are // registered above already, and libalpm refuses a second // registration, so the check is all that is left of // filter_sync_dbs here if scope.foreign || scope.native || scope.upgrades { check_sync_fresh(mut ui, mut handle)! } sync_dbs = dbs.clone() } if scope.query.len == 0 && config.groups_level >= 2 { // -gg: every group with its members, pacman's 'group package' // lines; each group is listed once, whichever dbs carry it mut names := []string{} for g in sync_groups(handle, dbs) { if g.name !in names { names << g.name } } scope.query = names collect_group_members(mut listing, handle, dbs, sync_dbs, scope)! if ipc_on() || config.quiet { listing.print() } else { for e in listing.entries { println('${e.group} ${name_style(e.name)}') } } return } if scope.query.len == 0 { // no names: every group of every db, one line each groups := sync_groups(handle, dbs) if ipc_on() { emit(GroupsEvent{ groups: groups }) return } for g in groups { println(table_text([row(g.repo, g.name)], ' ', ' ')) } return } collect_group_members(mut listing, handle, dbs, sync_dbs, scope) or { listing.print_found() return err } listing.print() return } collect_search(mut listing, mut ui, mut handle, config) or { listing.print_found() return err } listing.print() }