Commit 1cf78407 authored by guinux's avatar guinux

split search installed/repos pkgs

parent 31b21c89
......@@ -934,12 +934,9 @@ namespace Pamac {
}
}
SearchFunc<Package, string> search_in_pkgs = (pkg, name) => {
return strcmp (pkg.name, name);
};
async void search_pkgs (string search_string) {
var pkgs = database.search_pkgs (search_string);
var installed_pkgs = database.search_installed_pkgs (search_string);
var repos_pkgs = database.search_repos_pkgs (search_string);
var aur_pkgs = new List<AURPackage> ();
if (database.config.enable_aur) {
aur_pkgs = yield database.search_in_aur (search_string);
......@@ -957,7 +954,15 @@ namespace Pamac {
}
int version_length = 0;
int repo_length = 0;
foreach (unowned Package pkg in pkgs) {
foreach (unowned Package pkg in installed_pkgs) {
if (pkg.version.length > version_length) {
version_length = pkg.version.length;
}
if (pkg.repo.length > repo_length) {
repo_length = pkg.repo.length;
}
}
foreach (unowned Package pkg in repos_pkgs) {
if (pkg.version.length > version_length) {
version_length = pkg.version.length;
}
......@@ -976,40 +981,51 @@ namespace Pamac {
}
}
int available_width = get_term_width () - (version_length + repo_length + 4);
if (pkgs.length () > 0) {
foreach (unowned Package pkg in pkgs) {
string name = pkg.name;
if (pkg.installed_version != "") {
name = "%s [%s]".printf (pkg.name, dgettext (null, "Installed"));
}
var str_builder = new StringBuilder ();
str_builder.append (name);
str_builder.append (" ");
int diff = available_width - name.char_count ();
if (diff > 0) {
while (diff > 0) {
str_builder.append (" ");
diff--;
}
foreach (unowned Package pkg in installed_pkgs) {
string installed = "[%s]".printf (dgettext (null, "Installed"));
int installed_available_width = available_width - (installed.char_count () + 1);
var str_builder = new StringBuilder ();
str_builder.append (pkg.name);
str_builder.append (" ");
int diff = installed_available_width - pkg.name.length;
if (diff > 0) {
while (diff > 0) {
str_builder.append (" ");
diff--;
}
str_builder.append ("%-*s %s \n".printf (version_length, pkg.version, pkg.repo));
stdout.printf ("%s", str_builder.str);
string[] cuts = split_string (pkg.desc, 2, available_width);
foreach (unowned string cut in cuts) {
print_aligned ("", cut, 2);
}
str_builder.append (installed);
str_builder.append (" ");
str_builder.append ("%-*s %s \n".printf (version_length, pkg.version, pkg.repo));
stdout.printf ("%s", str_builder.str);
string[] cuts = split_string (pkg.desc, 2, available_width);
foreach (unowned string cut in cuts) {
print_aligned ("", cut, 2);
}
}
foreach (unowned Package pkg in repos_pkgs) {
var str_builder = new StringBuilder ();
str_builder.append (pkg.name);
str_builder.append (" ");
int diff = available_width - pkg.name.length;
if (diff > 0) {
while (diff > 0) {
str_builder.append (" ");
diff--;
}
}
str_builder.append ("%-*s %s \n".printf (version_length, pkg.version, pkg.repo));
stdout.printf ("%s", str_builder.str);
string[] cuts = split_string (pkg.desc, 2, available_width);
foreach (unowned string cut in cuts) {
print_aligned ("", cut, 2);
}
}
if (aur_pkgs.length () > 0) {
if (pkgs.length () > 0) {
if (installed_pkgs.length () > 0 || repos_pkgs.length () > 0) {
stdout.printf ("\n");
}
foreach (unowned AURPackage aur_pkg in aur_pkgs) {
unowned List<Package>? found = pkgs.search (aur_pkg.name, search_in_pkgs);
if (found != null) {
// pkg already printed
continue;
}
var str_builder = new StringBuilder ();
string name = aur_pkg.name;
if (aur_pkg.installed_version != "") {
......
......@@ -596,6 +596,134 @@ namespace Pamac {
return new Package.from_struct (initialise_pkg_struct (find_dbs_satisfier (depstring)));
}
Alpm.List<unowned Alpm.Package> search_local_db (string search_string) {
Alpm.List<unowned string> needles = null;
string[] splitted = search_string.split (" ");
foreach (unowned string part in splitted) {
needles.add (part);
}
Alpm.List<unowned Alpm.Package> result = alpm_handle.localdb.search (needles);
// search in appstream
string[]? search_terms = As.utils_search_tokenize (search_string);
if (search_terms != null) {
Alpm.List<unowned Alpm.Package> appstream_result = null;
app_store.get_apps ().foreach ((app) => {
uint match_score = app.search_matches_all (search_terms);
if (match_score > 0) {
unowned string pkgname = app.get_pkgname_default ();
unowned Alpm.Package? alpm_pkg = alpm_handle.localdb.get_pkg (pkgname);
if (alpm_pkg != null) {
if (appstream_result.find (alpm_pkg, (Alpm.List.CompareFunc) alpm_pkg_compare_name) == null) {
appstream_result.add (alpm_pkg);
}
}
}
});
result.join (appstream_result.diff (result, (Alpm.List.CompareFunc) alpm_pkg_compare_name));
}
// use custom sort function
global_search_string = search_string;
result.sort (result.length, (Alpm.List.CompareFunc) alpm_pkg_sort_search_by_relevance);
return result;
}
Alpm.List<unowned Alpm.Package> search_sync_dbs (string search_string) {
Alpm.List<unowned string> needles = null;
string[] splitted = search_string.split (" ");
foreach (unowned string part in splitted) {
needles.add (part);
}
Alpm.List<unowned Alpm.Package> localpkgs = alpm_handle.localdb.search (needles);
Alpm.List<unowned Alpm.Package> syncpkgs = null;
unowned Alpm.List<unowned Alpm.DB> syncdbs = alpm_handle.syncdbs;
while (syncdbs != null) {
unowned Alpm.DB db = syncdbs.data;
if (syncpkgs.length == 0) {
syncpkgs = db.search (needles);
} else {
syncpkgs.join (db.search (needles).diff (syncpkgs, (Alpm.List.CompareFunc) alpm_pkg_compare_name));
}
syncdbs.next ();
}
// remove localpkgs
Alpm.List<unowned Alpm.Package> result = syncpkgs.diff (localpkgs, (Alpm.List.CompareFunc) alpm_pkg_compare_name);
// search in appstream
string[]? search_terms = As.utils_search_tokenize (search_string);
if (search_terms != null) {
Alpm.List<unowned Alpm.Package> appstream_result = null;
app_store.get_apps ().foreach ((app) => {
uint match_score = app.search_matches_all (search_terms);
if (match_score > 0) {
unowned string pkgname = app.get_pkgname_default ();
unowned Alpm.Package? alpm_pkg = alpm_handle.localdb.get_pkg (pkgname);
if (alpm_pkg == null) {
alpm_pkg = get_syncpkg (pkgname);
if (alpm_pkg != null) {
if (appstream_result.find (alpm_pkg, (Alpm.List.CompareFunc) alpm_pkg_compare_name) == null) {
appstream_result.add (alpm_pkg);
}
}
}
}
});
result.join (appstream_result.diff (result, (Alpm.List.CompareFunc) alpm_pkg_compare_name));
}
// use custom sort function
global_search_string = search_string;
result.sort (result.length, (Alpm.List.CompareFunc) alpm_pkg_sort_search_by_relevance);
return result;
}
public List<Package> search_installed_pkgs (string search_string) {
var pkgs = new List<Package> ();
Alpm.List<unowned Alpm.Package> alpm_pkgs = search_local_db (search_string);
unowned Alpm.List<unowned Alpm.Package> list = alpm_pkgs;
while (list != null) {
unowned Alpm.Package alpm_pkg = list.data;
foreach (unowned Package pkg in initialise_pkgs (alpm_pkg)) {
pkgs.append (pkg);
}
list.next ();
}
return pkgs;
}
public async List<Package> search_installed_pkgs_async (string search_string) {
var pkgs = new List<Package> ();
new Thread<int> ("search_installed_pkgs", () => {
pkgs = search_installed_pkgs (search_string);
Idle.add (search_installed_pkgs_async.callback);
return 0;
});
yield;
return (owned) pkgs;
}
public List<Package> search_repos_pkgs (string search_string) {
var pkgs = new List<Package> ();
Alpm.List<unowned Alpm.Package> alpm_pkgs = search_sync_dbs (search_string);
unowned Alpm.List<unowned Alpm.Package> list = alpm_pkgs;
while (list != null) {
unowned Alpm.Package alpm_pkg = list.data;
foreach (unowned Package pkg in initialise_pkgs (alpm_pkg)) {
pkgs.append (pkg);
}
list.next ();
}
return pkgs;
}
public async List<Package> search_repos_pkgs_async (string search_string) {
var pkgs = new List<Package> ();
new Thread<int> ("search_repos_pkgs", () => {
pkgs = search_repos_pkgs (search_string);
Idle.add (search_repos_pkgs_async.callback);
return 0;
});
yield;
return (owned) pkgs;
}
Alpm.List<unowned Alpm.Package> search_all_dbs (string search_string) {
Alpm.List<unowned string> needles = null;
string[] splitted = search_string.split (" ");
......@@ -711,8 +839,9 @@ namespace Pamac {
Json.Array aur_pkgs = aur_search_results.get (search_string);
aur_pkgs.foreach_element ((array, index, node) => {
Json.Object aur_pkg = node.get_object ();
// remove results which exist in repos
if (get_syncpkg (aur_pkg.get_string_member ("Name")) == null) {
// remove results which is installed or exist in repos
if (alpm_handle.localdb.get_pkg (aur_pkg.get_string_member ("Name")) == null
&& get_syncpkg (aur_pkg.get_string_member ("Name")) == null) {
result.append (new AURPackage.from_struct (initialise_aur_struct (aur_pkg)));
}
});
......
This diff is collapsed.
......@@ -49,7 +49,7 @@ namespace Pamac {
}
str_builder.append (str);
}
List<Package> pkgs = yield database.search_pkgs_async (str_builder.str);
List<Package> pkgs = yield database.search_repos_pkgs_async (str_builder.str);
var result = new GenericArray<string> ();
foreach (unowned Package pkg in pkgs) {
result.add (pkg.name);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment