From da56f43d57fcc37e2e49493e774eade779b54cf5 Mon Sep 17 00:00:00 2001 From: Scoopta Date: Mon, 28 Oct 2019 14:17:06 -0700 Subject: [PATCH] The text used for searches is no longer the same as the text displayed, this allows for a lot more powerful searching especially in drun mode --- inc/wofi.h | 2 +- modes/dmenu.c | 4 ++-- modes/drun.c | 16 ++++++++++++++-- modes/run.c | 8 ++++++-- src/wofi.c | 23 +++++++---------------- 5 files changed, 30 insertions(+), 23 deletions(-) diff --git a/inc/wofi.h b/inc/wofi.h index 204fc17..136380b 100644 --- a/inc/wofi.h +++ b/inc/wofi.h @@ -45,7 +45,7 @@ void wofi_init(struct map* config); struct wl_list* wofi_read_cache(char* mode); -void wofi_insert_widget(char* text, char* action); +void wofi_insert_widget(char* text, char* search_text, char* action); bool wofi_allow_images(); diff --git a/modes/dmenu.c b/modes/dmenu.c index b303322..91aceaa 100644 --- a/modes/dmenu.c +++ b/modes/dmenu.c @@ -23,7 +23,7 @@ void dmenu_init() { struct cache_line* node, *tmp; wl_list_for_each_safe(node, tmp, cache, link) { - wofi_insert_widget(node->line, node->line); + wofi_insert_widget(node->line, node->line, node->line); map_put(cached, node->line, "true"); free(node->line); wl_list_remove(&node->link); @@ -42,7 +42,7 @@ void dmenu_init() { if(map_contains(cached, line)) { continue; } - wofi_insert_widget(line, line); + wofi_insert_widget(line, line, line); } free(line); map_free(cached); diff --git a/modes/drun.c b/modes/drun.c index 123e0e3..4f0dd02 100644 --- a/modes/drun.c +++ b/modes/drun.c @@ -50,6 +50,14 @@ static char* get_text(char* file) { } } +static char* get_search_text(char* file, char* name) { + GDesktopAppInfo* info = g_desktop_app_info_new_from_filename(file); + const char* exec = g_app_info_get_executable(G_APP_INFO(info)); + const char* description = g_app_info_get_description(G_APP_INFO(info)); + const char* const* keywords = g_desktop_app_info_get_keywords(info); + return utils_concat(5, name, file, exec == NULL ? "" : exec, description == NULL ? "" : description, keywords == NULL ? (const char* const*) "" : keywords); +} + void drun_init() { struct map* cached = map_init(); struct map* entries = map_init(); @@ -61,8 +69,10 @@ void drun_init() { if(text == NULL) { goto cache_cont; } - wofi_insert_widget(text, node->line); + char* search_text = get_search_text(node->line, text); + wofi_insert_widget(text, search_text, node->line); map_put(cached, node->line, "true"); + free(search_text); free(text); cache_cont: @@ -113,8 +123,10 @@ void drun_init() { continue; } map_put(entries, entry->d_name, "true"); - wofi_insert_widget(text, full_path); + char* search_text = get_search_text(full_path, text); + wofi_insert_widget(text, search_text, full_path); free(text); + free(search_text); free(full_path); } closedir(dir); diff --git a/modes/run.c b/modes/run.c index b65192e..b0acab3 100644 --- a/modes/run.c +++ b/modes/run.c @@ -24,8 +24,10 @@ void run_init() { struct cache_line* node, *tmp; wl_list_for_each_safe(node, tmp, cache, link) { char* text = strrchr(node->line, '/') + 1; - wofi_insert_widget(text, node->line); + char* search_text = utils_concat(2, text, node->line); + wofi_insert_widget(text, search_text, node->line); map_put(cached, node->line, "true"); + free(search_text); free(node->line); wl_list_remove(&node->link); free(node); @@ -50,7 +52,9 @@ void run_init() { struct stat info; stat(full_path, &info); if(access(full_path, X_OK) == 0 && S_ISREG(info.st_mode) && !map_contains(cached, full_path)) { - wofi_insert_widget(entry->d_name, full_path); + char* search_text = utils_concat(2, entry->d_name, full_path); + wofi_insert_widget(entry->d_name, search_text, full_path); + free(search_text); } free(full_path); } diff --git a/src/wofi.c b/src/wofi.c index 151115f..86dc2b6 100644 --- a/src/wofi.c +++ b/src/wofi.c @@ -38,7 +38,7 @@ static void (*mode_exec)(const gchar* cmd); static bool (*exec_search)(); struct node { - char* text, *action; + char* text, *search_text, *action; }; static void nop() {} @@ -78,7 +78,7 @@ static void get_search(GtkSearchEntry* entry, gpointer data) { gtk_flow_box_invalidate_filter(GTK_FLOW_BOX(inner_box)); } -static GtkWidget* create_label(char* text, char* action) { +static GtkWidget* create_label(char* text, char* search_text, char* action) { GtkWidget* box = wofi_property_box_new(GTK_ORIENTATION_HORIZONTAL, 0); gtk_widget_set_name(box, "unselected"); GtkStyleContext* style = gtk_widget_get_style_context(box); @@ -88,7 +88,6 @@ static GtkWidget* create_label(char* text, char* action) { char* tmp = strdup(text); char* original = tmp; char* mode = NULL; - char* filter = NULL; size_t colon_count = utils_split(tmp, ':'); for(size_t count = 0; count < colon_count; ++count) { @@ -114,13 +113,6 @@ static GtkWidget* create_label(char* text, char* action) { gtk_widget_set_name(img, "img"); gtk_container_add(GTK_CONTAINER(box), img); } else if(strcmp(mode, "text") == 0) { - if(filter == NULL) { - filter = strdup(tmp); - } else { - char* tmp_filter = utils_concat(2, filter, tmp); - free(filter); - filter = tmp_filter; - } GtkWidget* label = gtk_label_new(tmp); gtk_widget_set_name(label, "text"); gtk_label_set_use_markup(GTK_LABEL(label), allow_markup); @@ -131,28 +123,26 @@ static GtkWidget* create_label(char* text, char* action) { } tmp += strlen(tmp) + 1; } - wofi_property_box_add_property(WOFI_PROPERTY_BOX(box), "filter", filter); - free(filter); free(original); } else { - wofi_property_box_add_property(WOFI_PROPERTY_BOX(box), "filter", text); GtkWidget* label = gtk_label_new(text); gtk_widget_set_name(label, "text"); gtk_label_set_use_markup(GTK_LABEL(label), allow_markup); gtk_label_set_xalign(GTK_LABEL(label), 0); gtk_container_add(GTK_CONTAINER(box), label); } - + wofi_property_box_add_property(WOFI_PROPERTY_BOX(box), "filter", search_text); return box; } static gboolean _insert_widget(gpointer data) { struct node* node = data; - GtkWidget* box = create_label(node->text, node->action); + GtkWidget* box = create_label(node->text, node->search_text, node->action); gtk_container_add(GTK_CONTAINER(inner_box), box); gtk_widget_show_all(box); free(node->text); + free(node->search_text); free(node->action); free(node); return FALSE; @@ -213,9 +203,10 @@ struct wl_list* wofi_read_cache(char* mode) { return cache; } -void wofi_insert_widget(char* text, char* action) { +void wofi_insert_widget(char* text, char* search_text, char* action) { struct node* widget = malloc(sizeof(struct node)); widget->text = strdup(text); + widget->search_text = strdup(search_text); widget->action = strdup(action); g_idle_add(_insert_widget, widget); utils_sleep_millis(1);