fix #68 and #134: sort search result by relevance

This commit is contained in:
guinux 2016-08-25 10:54:24 +02:00
parent 6660e832f1
commit eb57d65876
2 changed files with 40 additions and 3 deletions

View File

@ -40,6 +40,39 @@ private int alpm_pkg_compare_name (Alpm.Package pkg_a, Alpm.Package pkg_b) {
return strcmp (pkg_a.name, pkg_b.name); return strcmp (pkg_a.name, pkg_b.name);
} }
private string global_search_string;
private int alpm_pkg_sort_search_by_relevance (Alpm.Package pkg_a, Alpm.Package pkg_b) {
if (global_search_string != null) {
// display exact match first
if (pkg_a.name == global_search_string) {
return 0;
}
if (pkg_b.name == global_search_string) {
return 1;
}
if (pkg_a.name.has_prefix (global_search_string + "-")) {
return 0;
}
if (pkg_b.name.has_prefix (global_search_string + "-")) {
return 1;
}
if (pkg_a.name.has_prefix (global_search_string)) {
return 0;
}
if (pkg_b.name.has_prefix (global_search_string)) {
return 1;
}
if (pkg_a.name.contains (global_search_string)) {
return 0;
}
if (pkg_b.name.contains (global_search_string)) {
return 1;
}
}
return strcmp (pkg_a.name, pkg_b.name);
}
namespace Pamac { namespace Pamac {
[DBus (name = "org.manjaro.pamac")] [DBus (name = "org.manjaro.pamac")]
public class Daemon: Object { public class Daemon: Object {
@ -535,7 +568,9 @@ namespace Pamac {
syncdbs.next (); syncdbs.next ();
} }
result.join (syncpkgs.diff (result, (Alpm.List.CompareFunc) alpm_pkg_compare_name)); result.join (syncpkgs.diff (result, (Alpm.List.CompareFunc) alpm_pkg_compare_name));
//result.sort ((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; return result;
} }

View File

@ -195,8 +195,6 @@ namespace Pamac {
typeof (string), //repo typeof (string), //repo
typeof (uint64), //isize typeof (uint64), //isize
typeof (string)); //GLib.format (isize) typeof (string)); //GLib.format (isize)
// sort packages by name by default
packages_list.set_sort_column_id (1, Gtk.SortType.ASCENDING);
packages_treeview.set_model (packages_list); packages_treeview.set_model (packages_list);
// add custom cellrenderer to packages_treeview and aur_treewiew // add custom cellrenderer to packages_treeview and aur_treewiew
var packages_state_renderer = new ActivableCellRendererPixbuf (); var packages_state_renderer = new ActivableCellRendererPixbuf ();
@ -1091,6 +1089,8 @@ namespace Pamac {
switch (packages_stack.visible_child_name) { switch (packages_stack.visible_child_name) {
case "repos": case "repos":
transaction.search_pkgs.begin (search_string, (obj, res) => { transaction.search_pkgs.begin (search_string, (obj, res) => {
// get custom sort by relevance
packages_list.set_sort_column_id (Gtk.TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
populate_packages_list (transaction.search_pkgs.end (res)); populate_packages_list (transaction.search_pkgs.end (res));
}); });
break; break;
@ -1309,6 +1309,8 @@ namespace Pamac {
case "repos": case "repos":
transaction.search_pkgs.begin (search_string, (obj, res) => { transaction.search_pkgs.begin (search_string, (obj, res) => {
var pkgs = transaction.search_pkgs.end (res); var pkgs = transaction.search_pkgs.end (res);
// get custom sort by relevance
packages_list.set_sort_column_id (Gtk.TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
populate_packages_list (pkgs); populate_packages_list (pkgs);
if (search_aur_button.get_active ()) { if (search_aur_button.get_active ()) {
if (pkgs.length == 0) { if (pkgs.length == 0) {