1
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-02-02 15:08:15 -05:00

Allow search_any and search_all for help

This commit is contained in:
James Booth 2017-04-07 00:51:29 +01:00
parent a39440b61d
commit 2fafaec8a7
4 changed files with 60 additions and 10 deletions

View File

@ -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");

View File

@ -118,7 +118,7 @@ static struct cmd_t command_defs[] =
CMD_MAINFUNC(cmd_help)
CMD_NOTAGS
CMD_SYN(
"/help [<area>|<command>|search] [<search_term>]")
"/help [<area>|<command>|search_all|search_any] [<search_terms>]")
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(
{ "<area>", "Summary help for commands in a certain area of functionality." },
{ "<command>", "Full help for a specific command, for example '/help connect'." },
{ "search <search_term>", "Search commands for search_term." })
{ "<area>", "Summary help for commands in a certain area of functionality." },
{ "<command>", "Full help for a specific command, for example '/help connect'." },
{ "search_all <search_terms>", "Search commands for returning matches that contain all of the search terms." },
{ "search_any <search_terms>", "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;

View File

@ -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

View File

@ -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 {