From a14b7815ae86bb1d552197d32c215157aaa4dc10 Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 18 Feb 2016 01:20:17 +0000 Subject: [PATCH] Added new help format to c plugins WIP --- src/command/commands.c | 7 +++++- src/plugins/api.c | 28 +++++++++++++++++++---- src/plugins/api.h | 2 +- src/plugins/c_api.c | 7 +++--- src/plugins/callbacks.c | 18 ++++++++++++++- src/plugins/callbacks.h | 6 ++--- src/plugins/plugins.h | 3 +++ src/plugins/profapi.c | 3 ++- src/plugins/profapi.h | 3 ++- src/ui/console.c | 43 ++++++++++++++++++++++++++++++++++++ src/ui/ui.h | 1 + tests/unittests/ui/stub_ui.c | 2 ++ 12 files changed, 108 insertions(+), 15 deletions(-) diff --git a/src/command/commands.c b/src/command/commands.c index 49200858..780778c2 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -1370,7 +1370,12 @@ cmd_help(ProfWin *window, const char *const command, gchar **args) if (command) { cons_show_help(command); } else { - cons_show("No such command."); + CommandHelp *commandHelp = plugins_get_help(cmd_with_slash); + if (commandHelp) { + cons_show_plugin_help(cmd_with_slash, commandHelp); + } else { + cons_show("No such command."); + } } cons_show(""); } diff --git a/src/plugins/api.c b/src/plugins/api.c index 4f378344..00e1f1e9 100644 --- a/src/plugins/api.c +++ b/src/plugins/api.c @@ -65,19 +65,39 @@ api_cons_show(const char * const message) void api_register_command(const char *command_name, int min_args, int max_args, - const char *usage, const char *short_help, const char *long_help, void *callback, + const char **synopsis, const char *description, const char *arguments[][2], const char **examples, void *callback, void(*callback_func)(PluginCommand *command, gchar **args)) { PluginCommand *command = malloc(sizeof(PluginCommand)); command->command_name = command_name; command->min_args = min_args; command->max_args = max_args; - command->usage = usage; - command->short_help = short_help; - command->long_help = long_help; command->callback = callback; command->callback_func = callback_func; + CommandHelp *help = malloc(sizeof(CommandHelp)); + + int i = 0; + for (i = 0; synopsis[i] != NULL; i++) { + help->synopsis[i] = strdup(synopsis[i]); + } + help->synopsis[i] = NULL; + + help->desc = strdup(description); + for (i = 0; arguments[i][0] != NULL; i++) { + help->args[i][0] = strdup(arguments[i][0]); + help->args[i][1] = strdup(arguments[i][1]); + } + help->args[i][0] = NULL; + help->args[i][1] = NULL; + + for (i = 0; examples[i] != NULL; i++) { + help->examples[i] = strdup(examples[i]); + } + help->examples[i] = NULL; + + command->help = help; + callbacks_add_command(command); } diff --git a/src/plugins/api.h b/src/plugins/api.h index a99cee65..e0682033 100644 --- a/src/plugins/api.h +++ b/src/plugins/api.h @@ -45,7 +45,7 @@ char * api_get_current_recipient(void); char * api_get_current_muc(void); void api_register_command(const char *command_name, int min_args, int max_args, - const char *usage, const char *short_help, const char *long_help, + const char **synopsis, const char *description, const char *arguments[][2], const char **examples, void *callback, void(*callback_func)(PluginCommand *command, gchar **args)); void api_register_timed(void *callback, int interval_seconds, void (*callback_func)(PluginTimedFunction *timed_function)); diff --git a/src/plugins/c_api.c b/src/plugins/c_api.c index 3409c1ea..bb4edb01 100644 --- a/src/plugins/c_api.c +++ b/src/plugins/c_api.c @@ -69,12 +69,13 @@ c_api_cons_show(const char * const message) static void c_api_register_command(const char *command_name, int min_args, int max_args, - const char *usage, const char *short_help, const char *long_help, void(*callback)(char **args)) + const char **synopsis, const char *description, const char *arguments[][2], const char **examples, + void(*callback)(char **args)) { CommandWrapper *wrapper = malloc(sizeof(CommandWrapper)); wrapper->func = callback; - api_register_command(command_name, min_args, max_args, usage, - short_help, long_help, wrapper, c_command_callback); + api_register_command(command_name, min_args, max_args, synopsis, + description, arguments, examples, wrapper, c_command_callback); } static void diff --git a/src/plugins/callbacks.c b/src/plugins/callbacks.c index 9da8947e..2ae35f35 100644 --- a/src/plugins/callbacks.c +++ b/src/plugins/callbacks.c @@ -91,7 +91,7 @@ plugins_run_command(const char * const input) gboolean result; gchar **args = parse_args(input, command->min_args, command->max_args, &result); if (result == FALSE) { - ui_invalid_command_usage(command->usage, NULL); + ui_invalid_command_usage(command->command_name, NULL); g_strfreev(split); return TRUE; } else { @@ -107,6 +107,22 @@ plugins_run_command(const char * const input) return FALSE; } +CommandHelp* +plugins_get_help(const char *const cmd) +{ + GSList *curr = p_commands; + while (curr) { + PluginCommand *command = curr->data; + if (g_strcmp0(cmd, command->command_name) == 0) { + return command->help; + } + + curr = g_slist_next(curr); + } + + return NULL; +} + void plugins_run_timed(void) { diff --git a/src/plugins/callbacks.h b/src/plugins/callbacks.h index a5af8550..09dc0375 100644 --- a/src/plugins/callbacks.h +++ b/src/plugins/callbacks.h @@ -37,13 +37,13 @@ #include +#include "command/command.h" + typedef struct p_command { const char *command_name; int min_args; int max_args; - const char *usage; - const char *short_help; - const char *long_help; + CommandHelp *help; void *callback; void (*callback_func)(struct p_command *command, gchar **args); } PluginCommand; diff --git a/src/plugins/plugins.h b/src/plugins/plugins.h index c633bdd4..5c513597 100644 --- a/src/plugins/plugins.h +++ b/src/plugins/plugins.h @@ -35,6 +35,8 @@ #ifndef PLUGINS_H #define PLUGINS_H +#include "command/command.h" + typedef enum { LANG_C } lang_t; @@ -100,6 +102,7 @@ void plugins_post_priv_message_send(const char * const jid, const char * const gboolean plugins_run_command(const char * const cmd); void plugins_run_timed(void); gchar * plugins_get_dir(void); +CommandHelp* plugins_get_help(const char *const cmd); void plugins_win_process_line(char *win, const char * const line); #endif diff --git a/src/plugins/profapi.c b/src/plugins/profapi.c index 9e68bb68..ceeab254 100644 --- a/src/plugins/profapi.c +++ b/src/plugins/profapi.c @@ -42,7 +42,8 @@ void (*prof_cons_alert)(void) = NULL; void (*prof_cons_show)(const char * const message) = NULL; void (*prof_register_command)(const char *command_name, int min_args, int max_args, - const char *usage, const char *short_help, const char *long_help, void(*callback)(char **args)) = NULL; + const char **synopsis, const char *description, const char *arguments[][2], const char **examples, + void(*callback)(char **args)) = NULL; void (*prof_register_timed)(void(*callback)(void), int interval_seconds) = NULL; diff --git a/src/plugins/profapi.h b/src/plugins/profapi.h index 1f98f357..b1ecf352 100644 --- a/src/plugins/profapi.h +++ b/src/plugins/profapi.h @@ -42,7 +42,8 @@ void (*prof_cons_alert)(void); void (*prof_cons_show)(const char * const message); void (*prof_register_command)(const char *command_name, int min_args, int max_args, - const char *usage, const char *short_help, const char *long_help, void(*callback)(char **args)); + const char **synopsis, const char *description, const char *arguments[][2], const char **examples, + void(*callback)(char **args)); void (*prof_register_timed)(void(*callback)(void), int interval_seconds); diff --git a/src/ui/console.c b/src/ui/console.c index f4ce0e3c..aaf502f2 100644 --- a/src/ui/console.c +++ b/src/ui/console.c @@ -119,6 +119,49 @@ cons_show_padded(int pad, const char *const msg, ...) va_end(arg); } +void +cons_show_plugin_help(const char *const cmd, CommandHelp *help) +{ + ProfWin *console = wins_get_console(); + + cons_show(""); + win_vprint(console, '-', 0, NULL, 0, THEME_WHITE_BOLD, "", "%s", &cmd[1]); + win_print(console, '-', 0, NULL, NO_EOL, THEME_WHITE_BOLD, "", ""); + int i; + for (i = 0; i < strlen(cmd) - 1 ; i++) { + win_print(console, '-', 0, NULL, NO_EOL | NO_DATE, THEME_WHITE_BOLD, "", "-"); + } + win_print(console, '-', 0, NULL, NO_DATE, THEME_WHITE_BOLD, "", ""); + cons_show(""); + + win_print(console, '-', 0, NULL, 0, THEME_WHITE_BOLD, "", "Synopsis"); + ui_show_lines(console, help->synopsis); + cons_show(""); + + win_print(console, '-', 0, NULL, 0, THEME_WHITE_BOLD, "", "Description"); + win_println(console, 0, help->desc); + + int maxlen = 0; + for (i = 0; help->args[i][0] != NULL; i++) { + if (strlen(help->args[i][0]) > maxlen) + maxlen = strlen(help->args[i][0]); + } + + if (i > 0) { + cons_show(""); + win_print(console, '-', 0, NULL, 0, THEME_WHITE_BOLD, "", "Arguments"); + for (i = 0; help->args[i][0] != NULL; i++) { + win_vprint(console, '-', maxlen + 3, NULL, 0, 0, "", "%-*s: %s", maxlen + 1, help->args[i][0], help->args[i][1]); + } + } + + if (g_strv_length((gchar**)help->examples) > 0) { + cons_show(""); + win_print(console, '-', 0, NULL, 0, THEME_WHITE_BOLD, "", "Examples"); + ui_show_lines(console, help->examples); + } +} + void cons_show_help(Command *command) { diff --git a/src/ui/ui.h b/src/ui/ui.h index afca8448..a29c8f36 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -231,6 +231,7 @@ void cons_show_padded(int pad, const char *const msg, ...); void cons_about(void); void cons_help(void); void cons_show_help(Command *command); +void cons_show_plugin_help(const char *const cmd, CommandHelp *help); void cons_bad_cmd_usage(const char *const cmd); void cons_navigation_help(void); void cons_prefs(void); diff --git a/tests/unittests/ui/stub_ui.c b/tests/unittests/ui/stub_ui.c index 3381c2c6..13321025 100644 --- a/tests/unittests/ui/stub_ui.c +++ b/tests/unittests/ui/stub_ui.c @@ -322,6 +322,8 @@ void cons_show(const char * const msg, ...) void cons_show_padded(int pad, const char * const msg, ...) {} void cons_show_help(Command *command) {} +void cons_show_plugin_help(const char *const cmd, CommandHelp *help) {} + void cons_about(void) {} void cons_help(void) {}