From 2fafaec8a7dc9bc01ee894d83214590598b32914 Mon Sep 17 00:00:00 2001 From: James Booth Date: Fri, 7 Apr 2017 00:51:29 +0100 Subject: [PATCH] Allow search_any and search_all for help --- src/command/cmd_ac.c | 3 ++- src/command/cmd_defs.c | 40 ++++++++++++++++++++++++++++++++++------ src/command/cmd_defs.h | 3 ++- src/command/cmd_funcs.c | 24 ++++++++++++++++++++++-- 4 files changed, 60 insertions(+), 10 deletions(-) diff --git a/src/command/cmd_ac.c b/src/command/cmd_ac.c index 60c2762b..e5ec6f11 100644 --- a/src/command/cmd_ac.c +++ b/src/command/cmd_ac.c @@ -204,7 +204,8 @@ cmd_ac_init(void) help_ac = autocomplete_new(); autocomplete_add(help_ac, "commands"); autocomplete_add(help_ac, "navigation"); - autocomplete_add(help_ac, "search"); + autocomplete_add(help_ac, "search_all"); + autocomplete_add(help_ac, "search_any"); help_commands_ac = autocomplete_new(); autocomplete_add(help_commands_ac, "chat"); diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c index a9bc9771..ae77bf53 100644 --- a/src/command/cmd_defs.c +++ b/src/command/cmd_defs.c @@ -118,7 +118,7 @@ static struct cmd_t command_defs[] = CMD_MAINFUNC(cmd_help) CMD_NOTAGS CMD_SYN( - "/help [||search] []") + "/help [||search_all|search_any] []") CMD_DESC( "Help on using Profanity. Passing no arguments list help areas. " "For command help, optional arguments are shown using square brackets, " @@ -126,11 +126,12 @@ static struct cmd_t command_defs[] = "Arguments that may be one of a number of values are separated by a pipe " "e.g. val1|val2|val3.") CMD_ARGS( - { "", "Summary help for commands in a certain area of functionality." }, - { "", "Full help for a specific command, for example '/help connect'." }, - { "search ", "Search commands for search_term." }) + { "", "Summary help for commands in a certain area of functionality." }, + { "", "Full help for a specific command, for example '/help connect'." }, + { "search_all ", "Search commands for returning matches that contain all of the search terms." }, + { "search_any ", "Search commands for returning matches that contain any of the search terms." }) CMD_EXAMPLES( - "/help search presence online", + "/help search_all presence online", "/help commands", "/help presence", "/help who") @@ -2315,7 +2316,34 @@ _cmd_index(Command *cmd) { } GList* -cmd_search_index(char *term) +cmd_search_index_any(char *term) +{ + GList *results = NULL; + + gchar **processed_terms = g_str_tokenize_and_fold(term, NULL, NULL); + int terms_len = g_strv_length(processed_terms); + + int i = 0; + for (i = 0; i < terms_len; i++) { + GList *index_keys = g_hash_table_get_keys(search_index); + GList *curr = index_keys; + while (curr) { + char *index_entry = g_hash_table_lookup(search_index, curr->data); + if (g_str_match_string(processed_terms[i], index_entry, FALSE)) { + results = g_list_append(results, curr->data); + } + curr = g_list_next(curr); + } + g_list_free(index_keys); + } + + g_strfreev(processed_terms); + + return results; +} + +GList* +cmd_search_index_all(char *term) { GList *results = NULL; diff --git a/src/command/cmd_defs.h b/src/command/cmd_defs.h index 286bad0d..e0cadaf2 100644 --- a/src/command/cmd_defs.h +++ b/src/command/cmd_defs.h @@ -49,6 +49,7 @@ gboolean cmd_valid_tag(const char *const str); void command_docgen(void); -GList* cmd_search_index(char *term); +GList* cmd_search_index_all(char *term); +GList* cmd_search_index_any(char *term); #endif diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index fea2d55b..71c8816a 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -1565,11 +1565,31 @@ cmd_help(ProfWin *window, const char *const command, gchar **args) int num_args = g_strv_length(args); if (num_args == 0) { cons_help(); - } else if (strcmp(args[0], "search") == 0) { + } else if (strcmp(args[0], "search_all") == 0) { if (args[1] == NULL) { cons_bad_cmd_usage(command); } else { - GList *cmds = cmd_search_index(args[1]); + GList *cmds = cmd_search_index_all(args[1]); + if (cmds == NULL) { + cons_show("No commands found."); + } else { + GList *curr = cmds; + GList *results = NULL; + while (curr) { + results = g_list_insert_sorted(results, curr->data, (GCompareFunc)g_strcmp0); + curr = g_list_next(curr); + } + cons_show("Search results:"); + _cmd_list_commands(results); + g_list_free(results); + } + g_list_free(cmds); + } + } else if (strcmp(args[0], "search_any") == 0) { + if (args[1] == NULL) { + cons_bad_cmd_usage(command); + } else { + GList *cmds = cmd_search_index_any(args[1]); if (cmds == NULL) { cons_show("No commands found."); } else {