diff options
author | matteo4375 <[email protected]> | 2024-10-24 17:39:38 -0400 |
---|---|---|
committer | GitHub <[email protected]> | 2024-10-24 23:39:38 +0200 |
commit | 61727b01ba06f876664addca83e774cb2bad9ce9 (patch) | |
tree | 0eee2eea3a2c686c684889e9383abe045819157f | |
parent | e6250c7a4366da033d98a67b426ae5e08aa6f712 (diff) |
apps: add option to include keywords in queries (#56)
-rw-r--r-- | lib/apps/application.vala | 30 | ||||
-rw-r--r-- | lib/apps/apps.vala | 13 |
2 files changed, 42 insertions, 1 deletions
diff --git a/lib/apps/application.vala b/lib/apps/application.vala index 4b4fc44..c137183 100644 --- a/lib/apps/application.vala +++ b/lib/apps/application.vala @@ -42,6 +42,11 @@ public class AstalApps.Application : Object { */ public string icon_name { owned get { return app.get_string("Icon"); } } + /** + * `Keywords` field from the desktop file. + */ + public string[] keywords { owned get { return app.get_keywords(); } } + internal Application(string id, int? frequency = 0) { Object(app: new DesktopAppInfo(id)); this.frequency = frequency; @@ -74,6 +79,7 @@ public class AstalApps.Application : Object { */ public Score fuzzy_match(string term) { var score = Score(); + if (name != null) score.name = fuzzy_match_string(term, name); if (entry != null) @@ -82,6 +88,12 @@ public class AstalApps.Application : Object { score.executable = fuzzy_match_string(term, executable); if (description != null) score.description = fuzzy_match_string(term, description); + foreach (var keyword in keywords) { + var s = fuzzy_match_string(term, keyword); + if (s > score.keywords) { + score.keywords = s; + } + } return score; } @@ -91,6 +103,7 @@ public class AstalApps.Application : Object { */ public Score exact_match(string term) { var score = Score(); + if (name != null) score.name = name.down().contains(term.down()) ? 1 : 0; if (entry != null) @@ -99,12 +112,17 @@ public class AstalApps.Application : Object { score.executable = executable.down().contains(term.down()) ? 1 : 0; if (description != null) score.description = description.down().contains(term.down()) ? 1 : 0; + foreach (var keyword in keywords) { + if (score.keywords == 0) { + score.keywords = keyword.down().contains(term.down()) ? 1 : 0; + } + } return score; } internal Json.Node to_json() { - return new Json.Builder() + var builder = new Json.Builder() .begin_object() .set_member_name("name").add_string_value(name) .set_member_name("entry").add_string_value(entry) @@ -112,6 +130,15 @@ public class AstalApps.Application : Object { .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(); + + foreach (string keyword in keywords) { + builder.add_string_value(keyword); + } + + return builder + .end_array() .end_object() .get_root(); } @@ -122,4 +149,5 @@ public struct AstalApps.Score { int entry; int executable; int description; + int keywords; } diff --git a/lib/apps/apps.vala b/lib/apps/apps.vala index ac48121..dde7d44 100644 --- a/lib/apps/apps.vala +++ b/lib/apps/apps.vala @@ -44,6 +44,12 @@ public class AstalApps.Apps : Object { public double description_multiplier { get; set; default = 0.5; } /** + * Extra multiplier to apply when matching the keywords of an application. + * Defaults to `0.5` + */ + public double keywords_multiplier { get; set; default = 0.5; } + + /** * Consider the name of an application during queries. * Defaults to `true` */ @@ -67,6 +73,12 @@ public class AstalApps.Apps : Object { */ 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; } + construct { cache_directory = Environment.get_user_cache_dir() + "/astal"; cache_file = cache_directory + "/apps-frequents.json"; @@ -107,6 +119,7 @@ public class AstalApps.Apps : Object { 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; return r; } |