From 298f0211c1add0f451d77581319c1c696d1ba62e Mon Sep 17 00:00:00 2001 From: Sebastien Helleu Date: Sat, 15 Mar 2014 11:30:08 +0100 Subject: exec: add option "-color" in command /exec (decode ANSI colors by default) The ANSI colors are decoded by default to WeeChat colors (for local display), or IRC colors (if output is sent to buffer with "-o"). --- src/plugins/exec/exec-command.c | 21 ++++++++++++++++++--- src/plugins/exec/exec-command.h | 1 + src/plugins/exec/exec.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/plugins/exec/exec.h | 10 ++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/exec/exec-command.c b/src/plugins/exec/exec-command.c index c6af59fe1..09230c195 100644 --- a/src/plugins/exec/exec-command.c +++ b/src/plugins/exec/exec-command.c @@ -255,6 +255,15 @@ exec_command_parse_options (struct t_exec_cmd_options *cmd_options, { cmd_options->line_numbers = 0; } + else if (weechat_strcasecmp (argv[i], "-color") == 0) + { + if (i + 1 >= argc) + return 0; + i++; + cmd_options->color = exec_search_color (argv[i]); + if (cmd_options->color < 0) + return 0; + } else if (weechat_strcasecmp (argv[i], "-rc") == 0) { cmd_options->display_rc = 1; @@ -326,6 +335,7 @@ exec_command_run (struct t_gui_buffer *buffer, cmd_options.new_buffer = 0; cmd_options.switch_to_buffer = 1; cmd_options.line_numbers = -1; + cmd_options.color = EXEC_COLOR_DECODE; cmd_options.display_rc = 1; cmd_options.ptr_command_name = NULL; @@ -439,6 +449,7 @@ exec_command_run (struct t_gui_buffer *buffer, new_exec_cmd->output_to_buffer = cmd_options.output_to_buffer; new_exec_cmd->line_numbers = (cmd_options.line_numbers < 0) ? cmd_options.new_buffer : cmd_options.line_numbers; + new_exec_cmd->color = cmd_options.color; new_exec_cmd->display_rc = cmd_options.display_rc; /* execute the command */ @@ -670,8 +681,8 @@ exec_command_init () N_("execute external commands"), N_("-list" " || [-sh|-nosh] [-bg|-nobg] [-stdin|-nostdin] [-buffer ] " - "[-l|-o|-n] |-sw|-nosw] [-ln|-noln] [-rc|-norc] " - "[-timeout ] [-name ] " + "[-l|-o|-n] |-sw|-nosw] [-ln|-noln] [-color off|decode|strip] " + "[-rc|-norc] [-timeout ] [-name ] " " || -in " " || -inclose []" " || -signal " @@ -702,6 +713,10 @@ exec_command_init () " -nosw: don't switch to the output buffer\n" " -ln: display line numbers (default in new buffer only)\n" " -noln: don't display line numbers\n" + " -color: action on ANSI colors in output:\n" + " off: keep ANSI codes as-is\n" + " decode: convert ANSI colors to WeeChat/IRC (default)\n" + " strip: remove ANSI colors\n" " -rc: display return code (default)\n" " -norc: don't display return code\n" "-timeout: set a timeout for the command (in seconds)\n" @@ -729,7 +744,7 @@ exec_command_init () "exec.command.default_options."), "-list" " || -sh|-nosh|-bg|-nobg|-stdin|-nostdin|-buffer|-l|-o|-n|-sw|-nosw|" - "-ln|-noln|-timeout|-name|%*" + "-ln|-noln|-color|-timeout|-name|%*" " || -in|-inclose|-signal|-kill %(exec_commands_ids)" " || -killall" " || -set %(exec_commands_ids) stdin|stdin_close|signal" diff --git a/src/plugins/exec/exec-command.h b/src/plugins/exec/exec-command.h index b849e6ad4..252350ea1 100644 --- a/src/plugins/exec/exec-command.h +++ b/src/plugins/exec/exec-command.h @@ -33,6 +33,7 @@ struct t_exec_cmd_options int new_buffer; /* output in a new buffer */ int switch_to_buffer; /* switch to the output buffer */ int line_numbers; /* 1 to display line numbers */ + int color; /* what to do with ANSI colors */ int display_rc; /* 1 to display return code */ const char *ptr_command_name; /* name of command */ }; diff --git a/src/plugins/exec/exec.c b/src/plugins/exec/exec.c index 7d70ccaf2..a8184d680 100644 --- a/src/plugins/exec/exec.c +++ b/src/plugins/exec/exec.c @@ -44,6 +44,33 @@ struct t_exec_cmd *exec_cmds = NULL; /* first executed command */ struct t_exec_cmd *last_exec_cmd = NULL; /* last executed command */ int exec_cmds_count = 0; /* number of executed commands */ +char *exec_color_string[EXEC_NUM_COLORS] = +{ "off", "decode", "strip" }; + + +/* + * Searches for a color action name. + * + * Returns index of color in enum t_exec_color, -1 if not found. + */ + +int +exec_search_color (const char *color) +{ + int i; + + if (!color) + return -1; + + for (i = 0; i < EXEC_NUM_COLORS; i++) + { + if (weechat_strcasecmp (exec_color_string[i], color) == 0) + return i; + } + + /* color not found */ + return -1; +} /* * Searches for an executed command by id, which can be a number or a name. @@ -225,6 +252,19 @@ exec_command_display_output (struct t_exec_cmd *exec_cmd, if (!line) break; + if (exec_cmd->color != EXEC_COLOR_OFF) + { + line2 = weechat_hook_modifier_exec ( + (exec_cmd->output_to_buffer) ? + "irc_color_decode_ansi" : "color_decode_ansi", + (exec_cmd->color == EXEC_COLOR_DECODE) ? "1" : "0", + line); + free (line); + if (!line2) + break; + line = line2; + } + if (exec_cmd->output_to_buffer) { if (exec_cmd->line_numbers) diff --git a/src/plugins/exec/exec.h b/src/plugins/exec/exec.h index 0f9da1068..9958ed7a3 100644 --- a/src/plugins/exec/exec.h +++ b/src/plugins/exec/exec.h @@ -25,6 +25,14 @@ #define weechat_plugin weechat_exec_plugin #define EXEC_PLUGIN_NAME "exec" +enum t_exec_color { + EXEC_COLOR_OFF = 0, + EXEC_COLOR_DECODE, + EXEC_COLOR_STRIP, + /* number of color actions */ + EXEC_NUM_COLORS, +}; + struct t_exec_cmd { /* command/process */ @@ -41,6 +49,7 @@ struct t_exec_cmd int output_to_buffer; /* 1 if output is sent to buffer */ char *buffer_full_name; /* buffer where output is displayed */ int line_numbers; /* 1 if lines numbers are displayed */ + int color; /* what to do with ANSI colors */ int display_rc; /* 1 if return code is displayed */ /* command output */ @@ -59,6 +68,7 @@ extern struct t_exec_cmd *exec_cmds; extern struct t_exec_cmd *last_exec_cmd; extern int exec_cmds_count; +extern int exec_search_color (const char *color); extern struct t_exec_cmd *exec_search_by_id (const char *id); extern struct t_exec_cmd *exec_add (); extern int exec_process_cb (void *data, const char *command, int return_code, -- cgit v1.2.3