1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Added new help format to c plugins WIP

This commit is contained in:
James Booth 2016-02-18 01:20:17 +00:00
parent 2ac911618a
commit a14b7815ae
12 changed files with 108 additions and 15 deletions

View File

@ -1370,7 +1370,12 @@ cmd_help(ProfWin *window, const char *const command, gchar **args)
if (command) { if (command) {
cons_show_help(command); cons_show_help(command);
} else { } 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(""); cons_show("");
} }

View File

@ -65,19 +65,39 @@ api_cons_show(const char * const message)
void void
api_register_command(const char *command_name, int min_args, int max_args, 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)) void(*callback_func)(PluginCommand *command, gchar **args))
{ {
PluginCommand *command = malloc(sizeof(PluginCommand)); PluginCommand *command = malloc(sizeof(PluginCommand));
command->command_name = command_name; command->command_name = command_name;
command->min_args = min_args; command->min_args = min_args;
command->max_args = max_args; command->max_args = max_args;
command->usage = usage;
command->short_help = short_help;
command->long_help = long_help;
command->callback = callback; command->callback = callback;
command->callback_func = callback_func; 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); callbacks_add_command(command);
} }

View File

@ -45,7 +45,7 @@ char * api_get_current_recipient(void);
char * api_get_current_muc(void); char * api_get_current_muc(void);
void api_register_command(const char *command_name, int min_args, int max_args, 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 *callback, void(*callback_func)(PluginCommand *command, gchar **args));
void api_register_timed(void *callback, int interval_seconds, void api_register_timed(void *callback, int interval_seconds,
void (*callback_func)(PluginTimedFunction *timed_function)); void (*callback_func)(PluginTimedFunction *timed_function));

View File

@ -69,12 +69,13 @@ c_api_cons_show(const char * const message)
static void static void
c_api_register_command(const char *command_name, int min_args, int max_args, 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)); CommandWrapper *wrapper = malloc(sizeof(CommandWrapper));
wrapper->func = callback; wrapper->func = callback;
api_register_command(command_name, min_args, max_args, usage, api_register_command(command_name, min_args, max_args, synopsis,
short_help, long_help, wrapper, c_command_callback); description, arguments, examples, wrapper, c_command_callback);
} }
static void static void

View File

@ -91,7 +91,7 @@ plugins_run_command(const char * const input)
gboolean result; gboolean result;
gchar **args = parse_args(input, command->min_args, command->max_args, &result); gchar **args = parse_args(input, command->min_args, command->max_args, &result);
if (result == FALSE) { if (result == FALSE) {
ui_invalid_command_usage(command->usage, NULL); ui_invalid_command_usage(command->command_name, NULL);
g_strfreev(split); g_strfreev(split);
return TRUE; return TRUE;
} else { } else {
@ -107,6 +107,22 @@ plugins_run_command(const char * const input)
return FALSE; 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 void
plugins_run_timed(void) plugins_run_timed(void)
{ {

View File

@ -37,13 +37,13 @@
#include <glib.h> #include <glib.h>
#include "command/command.h"
typedef struct p_command { typedef struct p_command {
const char *command_name; const char *command_name;
int min_args; int min_args;
int max_args; int max_args;
const char *usage; CommandHelp *help;
const char *short_help;
const char *long_help;
void *callback; void *callback;
void (*callback_func)(struct p_command *command, gchar **args); void (*callback_func)(struct p_command *command, gchar **args);
} PluginCommand; } PluginCommand;

View File

@ -35,6 +35,8 @@
#ifndef PLUGINS_H #ifndef PLUGINS_H
#define PLUGINS_H #define PLUGINS_H
#include "command/command.h"
typedef enum { typedef enum {
LANG_C LANG_C
} lang_t; } 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); gboolean plugins_run_command(const char * const cmd);
void plugins_run_timed(void); void plugins_run_timed(void);
gchar * plugins_get_dir(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); void plugins_win_process_line(char *win, const char * const line);
#endif #endif

View File

@ -42,7 +42,8 @@ void (*prof_cons_alert)(void) = NULL;
void (*prof_cons_show)(const char * const message) = NULL; void (*prof_cons_show)(const char * const message) = NULL;
void (*prof_register_command)(const char *command_name, int min_args, int max_args, 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; void (*prof_register_timed)(void(*callback)(void), int interval_seconds) = NULL;

View File

@ -42,7 +42,8 @@ void (*prof_cons_alert)(void);
void (*prof_cons_show)(const char * const message); void (*prof_cons_show)(const char * const message);
void (*prof_register_command)(const char *command_name, int min_args, int max_args, 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); void (*prof_register_timed)(void(*callback)(void), int interval_seconds);

View File

@ -119,6 +119,49 @@ cons_show_padded(int pad, const char *const msg, ...)
va_end(arg); 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 void
cons_show_help(Command *command) cons_show_help(Command *command)
{ {

View File

@ -231,6 +231,7 @@ void cons_show_padded(int pad, const char *const msg, ...);
void cons_about(void); void cons_about(void);
void cons_help(void); void cons_help(void);
void cons_show_help(Command *command); 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_bad_cmd_usage(const char *const cmd);
void cons_navigation_help(void); void cons_navigation_help(void);
void cons_prefs(void); void cons_prefs(void);

View File

@ -322,6 +322,8 @@ void cons_show(const char * const msg, ...)
void cons_show_padded(int pad, const char * const msg, ...) {} void cons_show_padded(int pad, const char * const msg, ...) {}
void cons_show_help(Command *command) {} void cons_show_help(Command *command) {}
void cons_show_plugin_help(const char *const cmd, CommandHelp *help) {}
void cons_about(void) {} void cons_about(void) {}
void cons_help(void) {} void cons_help(void) {}