From eb57d658765f8c8ea4d775412f3aa340e9e1338c Mon Sep 17 00:00:00 2001 From: guinux Date: Thu, 25 Aug 2016 10:54:24 +0200 Subject: [PATCH] fix #68 and #134: sort search result by relevance --- src/daemon.vala | 37 ++++++++++++++++++++++++++++++++++++- src/manager_window.vala | 6 ++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/daemon.vala b/src/daemon.vala index 80cbe0c..e8c2492 100644 --- a/src/daemon.vala +++ b/src/daemon.vala @@ -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); } +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 { [DBus (name = "org.manjaro.pamac")] public class Daemon: Object { @@ -535,7 +568,9 @@ namespace Pamac { syncdbs.next (); } 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; } diff --git a/src/manager_window.vala b/src/manager_window.vala index 0f02c5b..fdf2894 100644 --- a/src/manager_window.vala +++ b/src/manager_window.vala @@ -195,8 +195,6 @@ namespace Pamac { typeof (string), //repo typeof (uint64), //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); // add custom cellrenderer to packages_treeview and aur_treewiew var packages_state_renderer = new ActivableCellRendererPixbuf (); @@ -1091,6 +1089,8 @@ namespace Pamac { switch (packages_stack.visible_child_name) { case "repos": 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)); }); break; @@ -1309,6 +1309,8 @@ namespace Pamac { case "repos": transaction.search_pkgs.begin (search_string, (obj, 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); if (search_aur_button.get_active ()) { if (pkgs.length == 0) {