1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-07 10:13:12 +02:00

core: add option -color in command /pipe

This commit is contained in:
Sébastien Helleu
2025-02-01 09:37:22 +01:00
parent 04aea1bcb5
commit daef5971ae
17 changed files with 638 additions and 378 deletions
+28 -4
View File
@@ -4979,7 +4979,8 @@ COMMAND_CALLBACK(pipe)
const char *ptr_command, *ptr_filename, *ptr_hsignal;
const char *ptr_concat_separator, *ptr_strip_chars;
char *command, space[2] = " ", newline[2] = "\n";
int i, index_command, skip_empty_lines, send_to_buffer, no_locale, pipe_set;
int i, index_command, skip_empty_lines, send_to_buffer, no_locale;
int pipe_set, color, color_set;
struct t_gui_buffer *ptr_buffer;
/* make C compiler happy */
@@ -5002,6 +5003,8 @@ COMMAND_CALLBACK(pipe)
ptr_filename = NULL;
ptr_hsignal = NULL;
ptr_command = NULL;
color = GUI_CHAT_PIPE_COLOR_STRIP;
color_set = 0;
for (i = 1; i < argc; i++)
{
@@ -5070,6 +5073,22 @@ COMMAND_CALLBACK(pipe)
ptr_hsignal = argv[i];
index_command = i + 1;
}
else if (string_strcmp (argv[i], "-color") == 0)
{
COMMAND_MIN_ARGS(i + 2, argv[i]);
i++;
color = gui_chat_pipe_search_color (argv[i]);
if (color < 0)
{
gui_chat_printf (NULL,
_("%sInvalid color: \"%s\""),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
argv[i]);
return WEECHAT_RC_ERROR;
}
color_set = 1;
index_command = i + 1;
}
else
break;
}
@@ -5120,6 +5139,8 @@ COMMAND_CALLBACK(pipe)
gui_chat_pipe_buffer = (ptr_buffer) ? ptr_buffer : buffer;
if (!gui_chat_pipe_buffer)
COMMAND_ERROR;
if (!color_set && !send_to_buffer)
color = GUI_CHAT_PIPE_COLOR_KEEP;
if (gui_chat_pipe_buffer->type != GUI_BUFFER_TYPE_FORMATTED)
{
gui_chat_printf (NULL,
@@ -5145,6 +5166,7 @@ COMMAND_CALLBACK(pipe)
gui_chat_pipe_skip_empty_lines = skip_empty_lines;
if (no_locale)
setlocale (LC_ALL, "C");
gui_chat_pipe_color = color;
pipe_set = 1;
gui_chat_pipe = 1;
}
@@ -9337,8 +9359,8 @@ command_init ()
N_("redirect command output to a buffer, a file or a hsignal"),
/* TRANSLATORS: only text between angle brackets (eg: "<name>") may be translated */
N_("[-buffer <name>|-file <filename>|-hsignal <name>] "
"[-concat <separator>] [-strip <chars>] [-skipempty] [-c] [-o] "
"[-g] [-nl] <command>"),
"[-color strip|keep|ansi] [-concat <separator>] "
"[-strip <chars>] [-skipempty] [-c] [-o] [-g] [-nl] <command>"),
CMD_ARGS_DESC(
N_("raw[-buffer]: display command output on this buffer"),
N_("name: full buffer name (examples: \"core.weechat\", "
@@ -9347,6 +9369,7 @@ command_init ()
N_("raw[-hsignal]: send command output as hsignal; "
"keys: \"command\", \"output\" (lines separated by separator) "
"and \"tags\" (tags of each line separated by newline)"),
N_("raw[-color]: convert colors"),
N_("raw[-o]: send command output to the buffer as input; "
"colors are stripped and commands are NOT executed "
"(used only with -buffer)"),
@@ -9383,7 +9406,8 @@ command_init ()
"-buffer %(buffers_plugins_names) %(commands:/)|%*"
" || -file %(filename) %(commands:/)|%*"
" || -hsignal %- %(commands:/)|%*"
" || -o %(commands:/)|%*",
" || -o %(commands:/)|%*"
" || %(commands:/)|%*",
&command_pipe, NULL, NULL);
hook_command (
NULL, "plugin",
+81 -6
View File
@@ -73,6 +73,10 @@ char **gui_chat_pipe_concat_lines = NULL; /* concatenated lines */
char **gui_chat_pipe_concat_tags = NULL; /* concatenated tags */
char *gui_chat_pipe_strip_chars = NULL; /* chars to strip on lines */
int gui_chat_pipe_skip_empty_lines = 0; /* skip empty lines */
/* colors in pipe output */
enum t_gui_chat_pipe_color gui_chat_pipe_color = GUI_CHAT_PIPE_COLOR_STRIP;
char *gui_chat_pipe_color_string[GUI_CHAT_PIPE_NUM_COLORS] =
{ "strip", "keep", "ansi" };
/*
@@ -604,9 +608,60 @@ gui_chat_buffer_valid (struct t_gui_buffer *buffer,
return 1;
}
/*
* Searches for a pipe color name.
*
* Returns index of color in enum t_gui_chat_pipe_color, -1 if not found.
*/
int
gui_chat_pipe_search_color (const char *color)
{
int i;
if (!color)
return -1;
for (i = 0; i < GUI_CHAT_PIPE_NUM_COLORS; i++)
{
if (strcmp (gui_chat_pipe_color_string[i], color) == 0)
return i;
}
/* color not found */
return -1;
}
/*
* Converts color in a message, according to variable gui_chat_pipe_color.
*
* Note: result must be freed after use.
*/
char *
gui_chat_pipe_convert_color (const char *data)
{
if (!data)
return NULL;
switch (gui_chat_pipe_color)
{
case GUI_CHAT_PIPE_COLOR_STRIP:
return gui_color_decode (data, NULL);
case GUI_CHAT_PIPE_COLOR_KEEP:
return strdup (data);
case GUI_CHAT_PIPE_COLOR_ANSI:
return gui_color_encode_ansi (data);
case GUI_CHAT_PIPE_NUM_COLORS:
break;
}
return strdup (data);
}
/*
* Builds a message with a line: "<prefix> <message>" or "<message>" if there
* is no prefix. The colors are stripped from prefix and message.
* is no prefix.
*
* Note: result must be freed after use.
*/
@@ -619,10 +674,8 @@ gui_chat_pipe_build_message (struct t_gui_line *line)
if (!line)
return NULL;
prefix = (line->data->prefix) ?
gui_color_decode (line->data->prefix, NULL) : strdup ("");
message = (line->data->message) ?
gui_color_decode (line->data->message, NULL) : strdup ("");
prefix = gui_chat_pipe_convert_color (line->data->prefix);
message = gui_chat_pipe_convert_color (line->data->message);
string_asprintf (&data,
"%s%s%s",
@@ -645,10 +698,15 @@ gui_chat_pipe_send_buffer_input (struct t_gui_buffer *buffer, const char *data)
{
struct t_gui_buffer *buffer_saved;
int send_to_buffer_saved;
char *data_color;
if (!buffer || !data)
return;
data_color = gui_chat_pipe_convert_color (data);
if (!data_color)
return;
buffer_saved = gui_chat_pipe_buffer;
send_to_buffer_saved = gui_chat_pipe_send_to_buffer;
@@ -658,7 +716,7 @@ gui_chat_pipe_send_buffer_input (struct t_gui_buffer *buffer, const char *data)
input_data (
buffer,
(data[0]) ? data : " ",
(data_color[0]) ? data_color : " ",
"-", /* commands_allowed */
0, /* split_newline */
0); /* user_data */
@@ -666,6 +724,8 @@ gui_chat_pipe_send_buffer_input (struct t_gui_buffer *buffer, const char *data)
/* restore pipe redirection */
gui_chat_pipe_buffer = buffer_saved;
gui_chat_pipe_send_to_buffer = send_to_buffer_saved;
free (data_color);
}
/*
@@ -819,6 +879,7 @@ gui_chat_pipe_end ()
string_dyn_free (gui_chat_pipe_concat_tags, 1);
gui_chat_pipe_concat_tags = NULL;
}
gui_chat_pipe_color = GUI_CHAT_PIPE_COLOR_STRIP;
}
/*
@@ -839,6 +900,7 @@ gui_chat_printf_datetime_tags_internal (struct t_gui_buffer *buffer,
int display_time;
char *ptr_msg, *pos_prefix, *pos_tab;
char *modifier_data, *string, *new_string, *pos_newline;
char *prefix_color, *message_color;
struct t_gui_line *new_line;
if (!buffer)
@@ -999,6 +1061,19 @@ gui_chat_printf_datetime_tags_internal (struct t_gui_buffer *buffer,
/* line was handled with /pipe command, do NOT display it */
goto no_print;
}
else if (gui_chat_pipe_buffer)
{
prefix_color = gui_chat_pipe_convert_color (new_line->data->prefix);
string_shared_free (new_line->data->prefix);
new_line->data->prefix = (prefix_color) ?
(char *)string_shared_get (prefix_color) : NULL;
new_line->data->prefix_length = (prefix_color) ?
gui_chat_strlen_screen (prefix_color) : 0;
message_color = gui_chat_pipe_convert_color (new_line->data->message);
free (new_line->data->message);
new_line->data->message = message_color;
}
/* add line in the buffer */
gui_line_add (new_line);
+11
View File
@@ -66,6 +66,15 @@ enum t_gui_chat_mute
GUI_CHAT_MUTE_ALL_BUFFERS,
};
enum t_gui_chat_pipe_color
{
GUI_CHAT_PIPE_COLOR_STRIP = 0,
GUI_CHAT_PIPE_COLOR_KEEP,
GUI_CHAT_PIPE_COLOR_ANSI,
/* number of color actions */
GUI_CHAT_PIPE_NUM_COLORS,
};
extern char *gui_chat_prefix[GUI_CHAT_NUM_PREFIXES];
extern char gui_chat_prefix_empty[];
extern int gui_chat_time_length;
@@ -82,6 +91,7 @@ extern char **gui_chat_pipe_concat_lines;
extern char **gui_chat_pipe_concat_tags;
extern char *gui_chat_pipe_strip_chars;
extern int gui_chat_pipe_skip_empty_lines;
extern enum t_gui_chat_pipe_color gui_chat_pipe_color;
extern int gui_chat_display_tags;
@@ -108,6 +118,7 @@ extern int gui_chat_get_time_length ();
extern void gui_chat_change_time_format ();
extern int gui_chat_buffer_valid (struct t_gui_buffer *buffer,
int buffer_type);
extern int gui_chat_pipe_search_color (const char *color);
extern void gui_chat_pipe_end ();
extern void gui_chat_printf_datetime_tags (struct t_gui_buffer *buffer,
time_t date, int date_usec,