diff options
Diffstat (limited to 'lib/apps')
-rw-r--r-- | lib/apps/application.vala | 55 | ||||
-rw-r--r-- | lib/apps/apps.vala | 66 | ||||
-rw-r--r-- | lib/apps/fuzzy.vala | 34 |
3 files changed, 122 insertions, 33 deletions
diff --git a/lib/apps/application.vala b/lib/apps/application.vala index 75ff6b2..7e20b06 100644 --- a/lib/apps/application.vala +++ b/lib/apps/application.vala @@ -1,12 +1,45 @@ -namespace AstalApps { -public class Application : Object { +public class AstalApps.Application : Object { + /** + * The underlying DesktopAppInfo. + */ public DesktopAppInfo app { get; construct set; } + + /** + * The number of times [[email protected]] was called on this Application. + */ public int frequency { get; set; default = 0; } + + /** + * The name of this Application. + */ public string name { get { return app.get_name(); } } + + /** + * Name of the .desktop of this Application. + */ public string entry { get { return app.get_id(); } } + + /** + * Description of this Application. + */ public string description { get { return app.get_description(); } } + + /** + * The StartupWMClass field from the desktop file. + * This represents the WM_CLASS property of the main window of the application. + */ public string wm_class { get { return app.get_startup_wm_class(); } } + + /** + * `Exec` field from the desktop file. + * Note that if you want to launch this Application you should use the [[email protected]] method. + */ public string executable { owned get { return app.get_string("Exec"); } } + + /** + * `Icon` field from the desktop file. + * This is usually a named icon or a path to a file. + */ public string icon_name { owned get { return app.get_string("Icon"); } } internal Application(string id, int? frequency = 0) { @@ -14,10 +47,17 @@ public class Application : Object { this.frequency = frequency; } + /** + * Get a value from the .desktop file by its key. + */ public string get_key(string key) { return app.get_string(key); } + /** + * Launches this application. + * The launched application inherits the environment of the launching process + */ public bool launch() { try { var s = app.launch(null, null); @@ -29,7 +69,7 @@ public class Application : Object { } } - public Score fuzzy_match(string term) { + internal Score fuzzy_match(string term) { var score = Score(); if (name != null) score.name = fuzzy_match_string(term, name); @@ -43,7 +83,7 @@ public class Application : Object { return score; } - public Score exact_match(string term) { + internal Score exact_match(string term) { var score = Score(); if (name != null) score.name = name.down().contains(term.down()) ? 1 : 0; @@ -71,14 +111,9 @@ public class Application : Object { } } -int min3(int a, int b, int c) { - return (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c); -} - -public struct Score { +public struct AstalApps.Score { int name; int entry; int executable; int description; } -} diff --git a/lib/apps/apps.vala b/lib/apps/apps.vala index b07961e..bbe07c6 100644 --- a/lib/apps/apps.vala +++ b/lib/apps/apps.vala @@ -1,23 +1,70 @@ -namespace AstalApps { -public class Apps : Object { +public class AstalApps.Apps : Object { private string cache_directory; private string cache_file; private List<Application> _list; private HashTable<string, int> frequents { get; private set; } + /** + * Indicates wether hidden applications should included in queries. + */ public bool show_hidden { get; set; } + + /** + * Full list of available applications. + */ public List<weak Application> list { owned get { return _list.copy(); } } - public int min_score { get; set; default = 0; } + /** + * The minimum score the application has to meet in order to be included in queries. + */ + public double min_score { get; set; default = 0; } + /** + * Extra multiplier to apply when matching the `name` of an application. + * Defaults to `2` + */ public double name_multiplier { get; set; default = 2; } + + /** + * Extra multiplier to apply when matching the entry of an application. + * Defaults to `1` + */ public double entry_multiplier { get; set; default = 1; } + + /** + * Extra multiplier to apply when matching the executable of an application. + * Defaults to `1` + */ public double executable_multiplier { get; set; default = 1; } + + /** + * Extra multiplier to apply when matching the description of an application. + * Defaults to `0.5` + */ public double description_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; } construct { @@ -61,11 +108,10 @@ public class Apps : Object { r += am.executable * executable_multiplier; if (include_description) r += am.description * description_multiplier; - return r; } - public List<weak Application> query(string? search = "", bool exact = false) { + internal List<weak Application> query(string? search = "", bool exact = false) { if (search == null) search = ""; @@ -114,14 +160,23 @@ public class Apps : Object { return arr; } + /** + * Query the `list` of applications with a fuzzy matching algorithm. + */ public List<weak Application> fuzzy_query(string? search = "") { return query(search, false); } + /** + * Query the `list` of applications with a simple string matching algorithm. + */ public List<weak Application> exact_query(string? search = "") { return query(search, true); } + /** + * Reload the `list` of Applications. + */ public void reload() { var arr = AppInfo.get_all(); @@ -169,4 +224,3 @@ public class Apps : Object { } } } -} diff --git a/lib/apps/fuzzy.vala b/lib/apps/fuzzy.vala index f93b2eb..7745320 100644 --- a/lib/apps/fuzzy.vala +++ b/lib/apps/fuzzy.vala @@ -1,25 +1,22 @@ - namespace AstalApps { - -private int max(int a, int b) { - return a > b ? a : b; -} - -public int fuzzy_match_string(string pattern, string str) { +private int fuzzy_match_string(string pattern, string str) { const int unmatched_letter_penalty = -1; int score = 100; if (pattern.length == 0) return score; if (str.length < pattern.length) return int.MIN; + bool found = fuzzy_match_recurse(pattern, str, score, true, out score); score += unmatched_letter_penalty * (str.length - pattern.length); - score = fuzzy_match_recurse(pattern, str, score, true); + + if(!found) score = -10; return score; } -private int fuzzy_match_recurse(string pattern, string str, int score, bool first_char) { - if (pattern.length == 0) return score; +private bool fuzzy_match_recurse(string pattern, string str, int score, bool first_char, out int result) { + result = score; + if (pattern.length == 0) return true; int match_idx = 0; int offset = 0; @@ -28,16 +25,19 @@ private int fuzzy_match_recurse(string pattern, string str, int score, bool firs while ((match_idx = str.casefold().substring(offset).index_of_char(search)) >= 0) { offset += match_idx; - int subscore = fuzzy_match_recurse( + int subscore; + bool found = fuzzy_match_recurse( pattern.substring(1), str.substring(offset + 1), - compute_score(offset, first_char, str, offset), false); - best_score = max(best_score, subscore); + compute_score(offset, first_char, str, offset), false, out subscore); + if(!found) break; + best_score = int.max(best_score, subscore); offset++; } - - if (best_score == int.MIN) return int.MIN; - return score + best_score; + + if (best_score == int.MIN) return false; + result += best_score; + return true; } private int compute_score(int jump, bool first_char, string match, int idx) { @@ -65,7 +65,7 @@ private int compute_score(int jump, bool first_char, string match, int idx) { score += first_letter_bonus; } if (first_char) { - score += max(leading_letter_penalty * jump, max_leading_letter_penalty); + score += int.max(leading_letter_penalty * jump, max_leading_letter_penalty); } return score; |