[view] improvement: sorting packages by closest match instead of considering installed first (search)

This commit is contained in:
Vinicius Moreira
2022-03-11 11:55:07 -03:00
parent b1a131950c
commit 508e9169c2
5 changed files with 46 additions and 71 deletions

View File

@@ -1,3 +1,6 @@
from typing import Iterable, List
from bauh.api.abstract.model import SoftwarePackage
from bauh.view.qt.view_model import PackageView
@@ -73,3 +76,33 @@ def is_package_hidden(pkg: PackageView, filters: dict) -> bool:
hidden = not filters['name'] in pkg.model.name.lower()
return hidden
def sort_packages(pkgs: Iterable[SoftwarePackage], word: str, limit: int = 0) -> List[SoftwarePackage]:
exact, starts_with, contains, others = [], [], [], []
for p in pkgs:
lower_name = p.name.lower()
if word == lower_name:
exact.append(p)
elif lower_name.startswith(word):
starts_with.append(p)
elif word in lower_name:
contains.append(p)
else:
others.append(p)
res = []
for app_list in (exact, starts_with, contains, others):
if app_list:
last = limit - len(res) if limit > 0 else None
if last is not None and last <= 0:
break
to_add = app_list[0:last]
to_add.sort(key=lambda a: a.name.lower())
res.extend(to_add)
return res