summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2016-02-24 13:00:15 +0000
committerRichard Hughes <richard@hughsie.com>2016-02-24 13:00:15 +0000
commit45ab1764b800a08c4f0d17604f6fd6caf76d7e6a (patch)
treed178a50c86f89d173e71544c0287e2c95d7611d2 /client
parent7c5a33cd6b6275c753c7b974565e5e29f387c7c9 (diff)
downloadappstream-glib-45ab1764b800a08c4f0d17604f6fd6caf76d7e6a.tar.gz
Return results from 'appstream-util search' in relevance order
Diffstat (limited to 'client')
-rw-r--r--client/as-util.c37
1 files changed, 34 insertions, 3 deletions
diff --git a/client/as-util.c b/client/as-util.c
index 888606e..b7e80ef 100644
--- a/client/as-util.c
+++ b/client/as-util.c
@@ -1277,14 +1277,29 @@ as_util_dump (AsUtilPrivate *priv, gchar **values, GError **error)
}
/**
+ * as_util_sort_apps_by_sort_key_cb:
+ **/
+static gint
+as_util_sort_apps_by_sort_key_cb (gconstpointer a, gconstpointer b)
+{
+ AsApp *app1 = *((AsApp **) a);
+ AsApp *app2 = *((AsApp **) b);
+ return g_strcmp0 (as_app_get_metadata_item (app2, "SortKey"),
+ as_app_get_metadata_item (app1, "SortKey"));
+}
+
+/**
* as_util_search:
**/
static gboolean
as_util_search (AsUtilPrivate *priv, gchar **values, GError **error)
{
+ AsApp *app;
GPtrArray *apps;
guint i;
+ guint score;
g_autoptr(AsStore) store = NULL;
+ g_autoptr(GPtrArray) array = NULL;
/* check args */
if (g_strv_length (values) < 1) {
@@ -1307,12 +1322,28 @@ as_util_search (AsUtilPrivate *priv, gchar **values, GError **error)
AS_STORE_LOAD_FLAG_DESKTOP,
NULL, error))
return FALSE;
+
+ /* add matches to an array */
apps = as_store_get_apps (store);
+ array = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
for (i = 0; i < apps->len; i++) {
- AsApp *app;
app = g_ptr_array_index (apps, i);
- if (as_app_search_matches_all (app, values))
- g_print ("%s\n", as_app_get_id (app));
+ score = as_app_search_matches_all (app, values);
+ if (score > 0) {
+ g_autofree gchar *sort_key = NULL;
+ sort_key = g_strdup_printf ("%05i", score);
+ as_app_add_metadata (app, "SortKey", sort_key);
+ g_ptr_array_add (array, g_object_ref (app));
+ }
+ }
+
+ /* print sorted results */
+ g_ptr_array_sort (array, as_util_sort_apps_by_sort_key_cb);
+ for (i = 0; i < array->len; i++) {
+ app = g_ptr_array_index (array, i);
+ g_print ("[%s] %s\n",
+ as_app_get_metadata_item (app, "SortKey"),
+ as_app_get_id (app));
}
return TRUE;
}