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

Implemented /script show

This commit is contained in:
James Booth 2015-10-17 22:58:45 +01:00
parent a35cbea732
commit eca7390951
6 changed files with 64 additions and 1 deletions

View File

@ -702,6 +702,10 @@ cmd_script(ProfWin *window, const char * const command, gchar **args)
GSList *scripts = scripts_list();
cons_show_scripts(scripts);
g_slist_free_full(scripts, g_free);
} else if ((g_strcmp0(args[0], "show") == 0) && args[1]) {
GSList *commands = scripts_read(args[1]);
cons_show_script(args[1], commands);
g_slist_free_full(commands, g_free);
} else {
cons_bad_cmd_usage(command);
}

View File

@ -94,6 +94,44 @@ scripts_list(void)
return result;
}
GSList*
scripts_read(const char *const script)
{
gchar *data_home = xdg_get_data_home();
GString *scriptpath = g_string_new(data_home);
free(data_home);
g_string_append(scriptpath, "/profanity/scripts/");
g_string_append(scriptpath, script);
FILE *scriptfile = g_fopen(scriptpath->str, "r");
if (!scriptfile) {
log_info("Script not found: %s", scriptpath->str);
g_string_free(scriptpath, TRUE);
return NULL;
}
g_string_free(scriptpath, TRUE);
char *line = NULL;
size_t len = 0;
ssize_t read;
GSList *result = NULL;
while ((read = getline(&line, &len, scriptfile)) != -1) {
if (g_str_has_suffix(line, "\n")) {
result = g_slist_append(result, g_strndup(line, strlen(line) -1));
} else {
result = g_slist_append(result, strdup(line));
}
}
fclose(scriptfile);
if (line) free(line);
return result;
}
gboolean
scripts_exec(const char *const script)
{

View File

@ -36,4 +36,5 @@
void scripts_init(void);
GSList* scripts_list(void);
GSList* scripts_read(const char *const script);
gboolean scripts_exec(const char *const script);

View File

@ -1599,6 +1599,24 @@ cons_show_scripts(GSList *scripts)
cons_alert();
}
void
cons_show_script(const char *const script, GSList *commands)
{
cons_show("");
if (commands == NULL) {
cons_show("Script not found: %s", script);
} else {
cons_show("%s:", script);
while (commands) {
cons_show(" %s", commands->data);
commands = g_slist_next(commands);
}
}
cons_alert();
}
void
cons_prefs(void)
{

View File

@ -279,6 +279,7 @@ void cons_show_info(PContact pcontact);
void cons_show_caps(const char * const fulljid, resource_presence_t presence);
void cons_show_themes(GSList *themes);
void cons_show_scripts(GSList *scripts);
void cons_show_script(const char *const script, GSList *commands);
void cons_show_aliases(GList *aliases);
void cons_show_login_success(ProfAccount *account, int secured);
void cons_show_software_version(const char * const jid,

View File

@ -416,6 +416,7 @@ void cons_show_info(PContact pcontact) {}
void cons_show_caps(const char * const fulljid, resource_presence_t presence) {}
void cons_show_themes(GSList *themes) {}
void cons_show_scripts(GSList *scripts) {}
void cons_show_script(const char *const script, GSList *commands) {}
void cons_show_aliases(GList *aliases)
{