mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-08 13:14:17 +02:00
[view] improvement: sorting packages by closest match instead of considering installed first (search)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user