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:
parent
2ac911618a
commit
a14b7815ae
@ -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("");
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -37,13 +37,13 @@
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#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;
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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);
|
||||
|
@ -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) {}
|
||||
|
Loading…
Reference in New Issue
Block a user