From f679c48a24b200195ca703bf673a2ae72dc65d1c Mon Sep 17 00:00:00 2001 From: matteo4375 Date: Sat, 26 Oct 2024 12:31:40 -0400 Subject: apps: add option to include categories in queries --- lib/apps/application.vala | 36 ++++++++++++++++++++++++++++++++---- lib/apps/apps.vala | 13 +++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) (limited to 'lib/apps') diff --git a/lib/apps/application.vala b/lib/apps/application.vala index 0a2f73c..07b44e4 100644 --- a/lib/apps/application.vala +++ b/lib/apps/application.vala @@ -47,6 +47,17 @@ public class AstalApps.Application : Object { */ public string[] keywords { owned get { return app.get_keywords(); } } + /** + * `Categories` field from the desktop file. + */ + public string[] categories { owned get { + var categories = app.get_categories(); + if (categories != null) + return categories.split(";"); + + return new string[0]; + } } + internal Application(string id, int? frequency = 0) { Object(app: new DesktopAppInfo(id)); this.frequency = frequency; @@ -94,6 +105,12 @@ public class AstalApps.Application : Object { score.keywords = s; } } + foreach (var category in categories) { + var s = fuzzy_match_string(term, category); + if (s > score.categories) { + score.categories = s; + } + } return score; } @@ -117,6 +134,11 @@ public class AstalApps.Application : Object { score.keywords = keyword.down().contains(term.down()) ? 1 : 0; } } + foreach (var category in categories) { + if (score.categories == 0) { + score.categories = category.down().contains(term.down()) ? 1 : 0; + } + } return score; } @@ -129,16 +151,21 @@ public class AstalApps.Application : Object { .set_member_name("executable").add_string_value(executable) .set_member_name("description").add_string_value(description) .set_member_name("icon_name").add_string_value(icon_name) - .set_member_name("frequency").add_int_value(frequency) - .set_member_name("keywords") - .begin_array(); + .set_member_name("frequency").add_int_value(frequency); + builder.set_member_name("keywords").begin_array(); foreach (string keyword in keywords) { builder.add_string_value(keyword); } + builder.end_array(); + + builder.set_member_name("categories").begin_array(); + foreach (string category in categories) { + builder.add_string_value(category); + } + builder.end_array(); return builder - .end_array() .end_object() .get_root(); } @@ -150,4 +177,5 @@ public struct AstalApps.Score { int executable; int description; int keywords; + int categories; } diff --git a/lib/apps/apps.vala b/lib/apps/apps.vala index dde7d44..30c305a 100644 --- a/lib/apps/apps.vala +++ b/lib/apps/apps.vala @@ -49,6 +49,12 @@ public class AstalApps.Apps : Object { */ public double keywords_multiplier { get; set; default = 0.5; } + /** + * Extra multiplier to apply when matching the categories of an application. + * Defaults to `0.5` + */ + public double categories_multiplier { get; set; default = 0.5; } + /** * Consider the name of an application during queries. * Defaults to `true` @@ -79,6 +85,12 @@ public class AstalApps.Apps : Object { */ public bool include_keywords { get; set; default = false; } + /** + * Consider the categories of an application during queries. + * Defaults to `false` + */ + public bool include_categories { get; set; default = false; } + construct { cache_directory = Environment.get_user_cache_dir() + "/astal"; cache_file = cache_directory + "/apps-frequents.json"; @@ -120,6 +132,7 @@ public class AstalApps.Apps : Object { if (include_executable) r += s.executable * executable_multiplier; if (include_description) r += s.description * description_multiplier; if (include_keywords) r += s.keywords * keywords_multiplier; + if (include_categories) r += s.categories * categories_multiplier; return r; } -- cgit v1.2.3 From e5d183860e6b21fee97e427ecac4867559b1c6e4 Mon Sep 17 00:00:00 2001 From: Aylur Date: Fri, 1 Nov 2024 13:44:03 +0000 Subject: fix: trailing empty category --- lib/apps/application.vala | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) (limited to 'lib/apps') diff --git a/lib/apps/application.vala b/lib/apps/application.vala index 07b44e4..75b47e9 100644 --- a/lib/apps/application.vala +++ b/lib/apps/application.vala @@ -50,13 +50,16 @@ public class AstalApps.Application : Object { /** * `Categories` field from the desktop file. */ - public string[] categories { owned get { - var categories = app.get_categories(); - if (categories != null) - return categories.split(";"); - - return new string[0]; - } } + public string[] categories { + owned get { + if (app.get_categories() == null) + return {}; + + var categories = app.get_categories(); + var arr = categories.split(";"); + return categories.has_suffix(";") ? arr[0:arr.length-1] : arr; + } + } internal Application(string id, int? frequency = 0) { Object(app: new DesktopAppInfo(id)); @@ -144,28 +147,26 @@ public class AstalApps.Application : Object { } internal Json.Node to_json() { - var builder = new Json.Builder() - .begin_object() - .set_member_name("name").add_string_value(name) - .set_member_name("entry").add_string_value(entry) - .set_member_name("executable").add_string_value(executable) - .set_member_name("description").add_string_value(description) - .set_member_name("icon_name").add_string_value(icon_name) - .set_member_name("frequency").add_int_value(frequency); - - builder.set_member_name("keywords").begin_array(); + var keyword_arr = new Json.Builder().begin_array(); foreach (string keyword in keywords) { - builder.add_string_value(keyword); + keyword_arr.add_string_value(keyword); } - builder.end_array(); - builder.set_member_name("categories").begin_array(); + var category_arr = new Json.Builder().begin_array(); foreach (string category in categories) { - builder.add_string_value(category); + category_arr.add_string_value(category); } - builder.end_array(); - return builder + return new Json.Builder() + .begin_object() + .set_member_name("name").add_string_value(name) + .set_member_name("entry").add_string_value(entry) + .set_member_name("executable").add_string_value(executable) + .set_member_name("description").add_string_value(description) + .set_member_name("icon_name").add_string_value(icon_name) + .set_member_name("frequency").add_int_value(frequency) + .set_member_name("keywords").add_value(keyword_arr.end_array().get_root()) + .set_member_name("categories").add_value(category_arr.end_array().get_root()) .end_object() .get_root(); } -- cgit v1.2.3 From df4c505097b0394d1a75565c2a5e8b47837c3050 Mon Sep 17 00:00:00 2001 From: Aylur Date: Fri, 1 Nov 2024 14:01:05 +0000 Subject: apps: remove redundant properties there is no need for the enable properties since setting the multiplier to a non zero or zero value is the same thing --- lib/apps/application.vala | 3 +++ lib/apps/apps.vala | 69 +++++++++++++---------------------------------- 2 files changed, 22 insertions(+), 50 deletions(-) (limited to 'lib/apps') diff --git a/lib/apps/application.vala b/lib/apps/application.vala index 75b47e9..3a9900a 100644 --- a/lib/apps/application.vala +++ b/lib/apps/application.vala @@ -1,3 +1,6 @@ +/** + * Object representing an applications .desktop file. + */ public class AstalApps.Application : Object { /** * The underlying DesktopAppInfo. diff --git a/lib/apps/apps.vala b/lib/apps/apps.vala index 30c305a..999643c 100644 --- a/lib/apps/apps.vala +++ b/lib/apps/apps.vala @@ -1,3 +1,8 @@ +/** + * This object can be used to query applications. + * Multipliers can be set to customize [struct@AstalApps.Score] results + * from queries which then are summed and sorted accordingly. + */ public class AstalApps.Apps : Object { private string cache_directory; private string cache_file; @@ -27,21 +32,21 @@ public class AstalApps.Apps : Object { /** * Extra multiplier to apply when matching the entry of an application. - * Defaults to `1` + * Defaults to `0` */ - public double entry_multiplier { get; set; default = 1; } + public double entry_multiplier { get; set; default = 0; } /** * Extra multiplier to apply when matching the executable of an application. - * Defaults to `1` + * Defaults to `0.5` */ - public double executable_multiplier { get; set; default = 1; } + public double executable_multiplier { get; set; default = 0.5; } /** * Extra multiplier to apply when matching the description of an application. - * Defaults to `0.5` + * Defaults to `0` */ - public double description_multiplier { get; set; default = 0.5; } + public double description_multiplier { get; set; default = 0; } /** * Extra multiplier to apply when matching the keywords of an application. @@ -51,45 +56,9 @@ public class AstalApps.Apps : Object { /** * Extra multiplier to apply when matching the categories of an application. - * Defaults to `0.5` - */ - public double categories_multiplier { get; set; default = 0.5; } - - /** - * Consider the name of an application during queries. - * Defaults to `true` - */ - public bool include_name { get; set; default = true; } - - /** - * Consider the entry of an application during queries. - * Defaults to `false` - */ - public bool include_entry { get; set; default = false; } - - /** - * Consider the executable of an application during queries. - * Defaults to `false` - */ - public bool include_executable { get; set; default = false; } - - /** - * Consider the description of an application during queries. - * Defaults to `false` - */ - public bool include_description { get; set; default = false; } - - /** - * Consider the keywords of an application during queries. - * Defaults to `false` - */ - public bool include_keywords { get; set; default = false; } - - /** - * Consider the categories of an application during queries. - * Defaults to `false` + * Defaults to `0` */ - public bool include_categories { get; set; default = false; } + public double categories_multiplier { get; set; default = 0; } construct { cache_directory = Environment.get_user_cache_dir() + "/astal"; @@ -127,12 +96,12 @@ public class AstalApps.Apps : Object { if (alg == FUZZY) s = a.fuzzy_match(search); if (alg == EXACT) s = a.exact_match(search); - if (include_name) r += s.name * name_multiplier; - if (include_entry) r += s.entry * entry_multiplier; - if (include_executable) r += s.executable * executable_multiplier; - if (include_description) r += s.description * description_multiplier; - if (include_keywords) r += s.keywords * keywords_multiplier; - if (include_categories) r += s.categories * categories_multiplier; + r += s.name * name_multiplier; + r += s.entry * entry_multiplier; + r += s.executable * executable_multiplier; + r += s.description * description_multiplier; + r += s.keywords * keywords_multiplier; + r += s.categories * categories_multiplier; return r; } -- cgit v1.2.3