1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-30 06:46:38 +02:00

core: add modifier and infos to decode ANSI colors

New modifier:
- color_decode_ansi: convert ANSI colors to WeeChat colors (or remove colors).

New infos:
- color_ansi_regex: regex used to parse ANSI colors in a string
- color_term2rgb: convert a terminal color (0-255) to RGB
- color_rgb2term: convert a RGB color to terminal color (0-255)
This commit is contained in:
Sebastien Helleu
2014-03-15 11:06:30 +01:00
parent 9e659d9f2e
commit e38f437ad7
9 changed files with 357 additions and 29 deletions
+68 -1
View File
@@ -277,6 +277,25 @@ plugin_api_command (struct t_weechat_plugin *plugin,
free (command2);
}
/*
* Modifier to decode ANSI colors.
*/
char *
plugin_api_modifier_color_decode_ansi (void *data,
const char *modifier,
const char *modifier_data,
const char *string)
{
/* make C compiler happy */
(void) data;
(void) modifier;
return gui_color_decode_ansi (string,
(modifier_data && (strcmp (modifier_data, "1") == 0)) ?
1: 0);
}
/*
* Gets info about WeeChat.
*/
@@ -288,10 +307,11 @@ plugin_api_info_get_internal (void *data, const char *info_name,
time_t inactivity;
static char value[32], version_number[32] = { '\0' };
static char weechat_dir_absolute_path[PATH_MAX] = { '\0' };
int rgb, limit;
char *pos, *color;
/* make C compiler happy */
(void) data;
(void) arguments;
if (!info_name)
return NULL;
@@ -397,6 +417,43 @@ plugin_api_info_get_internal (void *data, const char *info_name,
snprintf (value, sizeof (value), "%d", gui_window_get_height ());
return value;
}
else if (string_strcasecmp (info_name, "color_ansi_regex") == 0)
{
return GUI_COLOR_REGEX_ANSI_DECODE;
}
else if (string_strcasecmp (info_name, "color_term2rgb") == 0)
{
if (arguments && arguments[0])
{
snprintf (value, sizeof (value),
"%d",
gui_color_convert_term_to_rgb (atoi (arguments)));
return value;
}
}
else if (string_strcasecmp (info_name, "color_rgb2term") == 0)
{
if (arguments && arguments[0])
{
limit = 256;
pos = strchr (arguments, ',');
if (pos)
{
color = string_strndup (arguments, pos - arguments);
if (!color)
return NULL;
rgb = atoi (color);
limit = atoi (pos + 1);
free (color);
}
else
rgb = atoi (arguments);
snprintf (value, sizeof (value),
"%d",
gui_color_convert_rgb_to_term (rgb, limit));
return value;
}
}
/* info not found */
return NULL;
@@ -1097,6 +1154,10 @@ plugin_api_infolist_free (struct t_infolist *infolist)
void
plugin_api_init ()
{
/* WeeChat core modifiers */
hook_modifier (NULL, "color_decode_ansi",
&plugin_api_modifier_color_decode_ansi, NULL);
/* WeeChat core info hooks */
hook_info (NULL, "version", N_("WeeChat version"), NULL,
&plugin_api_info_get_internal, NULL);
@@ -1141,6 +1202,12 @@ plugin_api_init ()
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "term_height", N_("height of terminal"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "color_ansi_regex", N_("regular expression to match ANSI escape codes"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "color_term2rgb", N_("terminal color (0-255) converted to RGB color"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "color_rgb2term", N_("RGB color converted to terminal color (0-255)"), NULL,
&plugin_api_info_get_internal, NULL);
/* WeeChat core infolist hooks */
hook_infolist (NULL, "bar", N_("list of bars"),