mirror of
https://github.com/weechat/weechat.git
synced 2026-06-12 14:14:48 +02:00
core: add function to convert WeeChat colors to ANSI colors (issue #528)
This commit is contained in:
@@ -115,7 +115,7 @@ int gui_color_timer = 0; /* timer in seconds */
|
||||
/*
|
||||
* Searches for a color by name.
|
||||
*
|
||||
* Return index of color in WeeChat colors table, -1 if not found.
|
||||
* Returns index of color in WeeChat colors table, -1 if not found.
|
||||
*/
|
||||
|
||||
int
|
||||
@@ -133,12 +133,33 @@ gui_color_search (const char *color_name)
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Searches for a color by index.
|
||||
*
|
||||
* Returns name of color in WeeChat colors table, NULL if not found.
|
||||
*/
|
||||
|
||||
const char *
|
||||
gui_color_search_index (int index)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; gui_weechat_colors[i].string; i++)
|
||||
{
|
||||
if (i == index)
|
||||
return gui_weechat_colors[i].string;
|
||||
}
|
||||
|
||||
/* color not found */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get Curses attributes corresponding to extended attributes flags in a color.
|
||||
*/
|
||||
|
||||
int
|
||||
gui_color_get_extended_attrs (int color)
|
||||
gui_color_get_gui_attrs (int color)
|
||||
{
|
||||
int attributes;
|
||||
|
||||
@@ -156,6 +177,29 @@ gui_color_get_extended_attrs (int color)
|
||||
return attributes;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get extended flags corresponding to Curses attributes in a color.
|
||||
*/
|
||||
|
||||
int
|
||||
gui_color_get_extended_flags (int attrs)
|
||||
{
|
||||
int flags;
|
||||
|
||||
flags = 0;
|
||||
|
||||
if (attrs & A_BOLD)
|
||||
flags |= GUI_COLOR_EXTENDED_BOLD_FLAG;
|
||||
if (attrs & A_REVERSE)
|
||||
flags |= GUI_COLOR_EXTENDED_REVERSE_FLAG;
|
||||
if (attrs & A_ITALIC)
|
||||
flags |= GUI_COLOR_EXTENDED_ITALIC_FLAG;
|
||||
if (attrs & A_UNDERLINE)
|
||||
flags |= GUI_COLOR_EXTENDED_UNDERLINE_FLAG;
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
/*
|
||||
* Assigns a WeeChat color (read from configuration).
|
||||
*
|
||||
@@ -546,7 +590,7 @@ gui_color_build (int number, int foreground, int background)
|
||||
gui_color[number]->foreground = gui_weechat_colors[foreground & GUI_COLOR_EXTENDED_MASK].foreground;
|
||||
gui_color[number]->attributes = gui_weechat_colors[foreground & GUI_COLOR_EXTENDED_MASK].attributes;
|
||||
}
|
||||
gui_color[number]->attributes |= gui_color_get_extended_attrs (foreground);
|
||||
gui_color[number]->attributes |= gui_color_get_gui_attrs (foreground);
|
||||
|
||||
/* set background */
|
||||
if (background & GUI_COLOR_EXTENDED_FLAG)
|
||||
|
||||
@@ -181,7 +181,7 @@ gui_window_clear (WINDOW *window, int fg, int bg)
|
||||
if (!gui_init_ok)
|
||||
return;
|
||||
|
||||
attrs = gui_color_get_extended_attrs (fg);
|
||||
attrs = gui_color_get_gui_attrs (fg);
|
||||
|
||||
if ((fg > 0) && (fg & GUI_COLOR_EXTENDED_FLAG))
|
||||
fg &= GUI_COLOR_EXTENDED_MASK;
|
||||
@@ -420,7 +420,7 @@ gui_window_set_custom_color_fg (WINDOW *window, int fg)
|
||||
{
|
||||
if (!(fg & GUI_COLOR_EXTENDED_KEEPATTR_FLAG))
|
||||
gui_window_remove_color_style (window, A_ALL_ATTR);
|
||||
attributes = gui_color_get_extended_attrs (fg) |
|
||||
attributes = gui_color_get_gui_attrs (fg) |
|
||||
gui_weechat_colors[fg & GUI_COLOR_EXTENDED_MASK].attributes;
|
||||
gui_window_set_color_style (window, attributes);
|
||||
fg = gui_weechat_colors[fg & GUI_COLOR_EXTENDED_MASK].foreground;
|
||||
@@ -505,7 +505,7 @@ gui_window_set_custom_color_fg_bg (WINDOW *window, int fg, int bg,
|
||||
{
|
||||
if (reset_attributes && !(fg & GUI_COLOR_EXTENDED_KEEPATTR_FLAG))
|
||||
gui_window_remove_color_style (window, A_ALL_ATTR);
|
||||
attributes = gui_color_get_extended_attrs (fg) |
|
||||
attributes = gui_color_get_gui_attrs (fg) |
|
||||
gui_weechat_colors[fg & GUI_COLOR_EXTENDED_MASK].attributes;
|
||||
gui_window_set_color_style (window, attributes);
|
||||
fg = gui_weechat_colors[fg & GUI_COLOR_EXTENDED_MASK].foreground;
|
||||
|
||||
@@ -91,7 +91,8 @@ extern void gui_main_init ();
|
||||
extern void gui_main_loop ();
|
||||
|
||||
/* color functions */
|
||||
extern int gui_color_get_extended_attrs (int color);
|
||||
extern int gui_color_get_gui_attrs (int color);
|
||||
extern int gui_color_get_extended_flags (int attrs);
|
||||
extern int gui_color_get_pair (int fg, int bg);
|
||||
extern int gui_color_weechat_get_pair (int weechat_color);
|
||||
extern void gui_color_alloc ();
|
||||
|
||||
@@ -45,10 +45,10 @@
|
||||
#define COLOR_WHITE 7
|
||||
|
||||
#define A_NORMAL 0
|
||||
#define A_BOLD 0
|
||||
#define A_UNDERLINE 0
|
||||
#define A_REVERSE 0
|
||||
#define A_ITALIC 0
|
||||
#define A_BOLD (1 << (13 + 8))
|
||||
#define A_UNDERLINE (1 << (9 + 8))
|
||||
#define A_REVERSE (1 << (10 + 8))
|
||||
#define A_ITALIC (1 << (23 + 8))
|
||||
|
||||
#define ACS_HLINE '-'
|
||||
#define ACS_VLINE '|'
|
||||
|
||||
+469
-10
@@ -674,25 +674,30 @@ gui_color_decode (const char *string, const char *replacement)
|
||||
{
|
||||
if (ptr_string[1] == GUI_COLOR_EXTENDED_CHAR)
|
||||
{
|
||||
if (ptr_string[2] && ptr_string[3]
|
||||
&& ptr_string[4] && ptr_string[5]
|
||||
&& ptr_string[6])
|
||||
ptr_string += 2;
|
||||
if (ptr_string[0] && ptr_string[1]
|
||||
&& ptr_string[2] && ptr_string[3]
|
||||
&& ptr_string[4])
|
||||
{
|
||||
ptr_string += 7;
|
||||
ptr_string += 5;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ptr_string[1] && ptr_string[2])
|
||||
ptr_string += 3;
|
||||
ptr_string++;
|
||||
if (ptr_string[0] && ptr_string[1])
|
||||
ptr_string += 2;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case GUI_COLOR_EXTENDED_CHAR:
|
||||
if ((isdigit (ptr_string[1])) && (isdigit (ptr_string[2]))
|
||||
&& (isdigit (ptr_string[3])) && (isdigit (ptr_string[4]))
|
||||
&& (isdigit (ptr_string[5])))
|
||||
ptr_string += 6;
|
||||
ptr_string++;
|
||||
if ((isdigit (ptr_string[0])) && (isdigit (ptr_string[1]))
|
||||
&& (isdigit (ptr_string[2])) && (isdigit (ptr_string[3]))
|
||||
&& (isdigit (ptr_string[4])))
|
||||
{
|
||||
ptr_string += 5;
|
||||
}
|
||||
break;
|
||||
case GUI_COLOR_EMPHASIS_CHAR:
|
||||
ptr_string++;
|
||||
@@ -1004,6 +1009,460 @@ gui_color_decode_ansi (const char *string, int keep_colors)
|
||||
(void *)((unsigned long)keep_colors));
|
||||
}
|
||||
|
||||
/*
|
||||
* Adds an ANSI color code with a WeeChat attribute flag.
|
||||
*/
|
||||
|
||||
void
|
||||
gui_color_add_ansi_flag (char **output, int flag)
|
||||
{
|
||||
switch (flag)
|
||||
{
|
||||
case GUI_COLOR_EXTENDED_BOLD_FLAG:
|
||||
string_dyn_concat (output, "\x1B[1m");
|
||||
break;
|
||||
case GUI_COLOR_EXTENDED_REVERSE_FLAG:
|
||||
string_dyn_concat (output, "\x1B[7m");
|
||||
break;
|
||||
case GUI_COLOR_EXTENDED_ITALIC_FLAG:
|
||||
string_dyn_concat (output, "\x1B[3m");
|
||||
break;
|
||||
case GUI_COLOR_EXTENDED_UNDERLINE_FLAG:
|
||||
string_dyn_concat (output, "\x1B[4m");
|
||||
break;
|
||||
case GUI_COLOR_EXTENDED_KEEPATTR_FLAG:
|
||||
/* nothing to do here (really? not sure) */
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Converts a WeeChat color number to an ANSI color number.
|
||||
*
|
||||
* Returns -1 if the color is not found.
|
||||
*/
|
||||
|
||||
int
|
||||
gui_color_weechat_to_ansi (int color)
|
||||
{
|
||||
const char *ptr_color_name;
|
||||
int i;
|
||||
|
||||
ptr_color_name = gui_color_search_index (color);
|
||||
if (!ptr_color_name)
|
||||
return -1;
|
||||
|
||||
for (i = 0; i < 16; i++)
|
||||
{
|
||||
if (strcmp (gui_color_ansi[i], ptr_color_name) == 0)
|
||||
return i;
|
||||
}
|
||||
|
||||
/* color not found */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Replaces WeeChat colors by ANSI colors.
|
||||
*
|
||||
* Note: result must be freed after use.
|
||||
*/
|
||||
|
||||
char *
|
||||
gui_color_encode_ansi (const char *string)
|
||||
{
|
||||
const unsigned char *ptr_string;
|
||||
char **out, str_concat[128], str_color[8], *error;
|
||||
int flag, color, length, ansi_color, fg, bg, attrs;
|
||||
|
||||
if (!string)
|
||||
return NULL;
|
||||
|
||||
out = string_dyn_alloc (((strlen (string) * 3) / 2) + 1);
|
||||
|
||||
ptr_string = (unsigned char *)string;
|
||||
while (ptr_string && ptr_string[0])
|
||||
{
|
||||
switch (ptr_string[0])
|
||||
{
|
||||
case GUI_COLOR_COLOR_CHAR:
|
||||
ptr_string++;
|
||||
switch (ptr_string[0])
|
||||
{
|
||||
case GUI_COLOR_FG_CHAR:
|
||||
ptr_string++;
|
||||
if (ptr_string[0] == GUI_COLOR_EXTENDED_CHAR)
|
||||
{
|
||||
ptr_string++;
|
||||
while ((flag = gui_color_attr_get_flag (ptr_string[0])) > 0)
|
||||
{
|
||||
gui_color_add_ansi_flag (out, flag);
|
||||
ptr_string++;
|
||||
}
|
||||
if (ptr_string[0] && ptr_string[1] && ptr_string[2]
|
||||
&& ptr_string[3] && ptr_string[4])
|
||||
{
|
||||
memcpy (str_color, ptr_string, 5);
|
||||
str_color[5] = '\0';
|
||||
error = NULL;
|
||||
color = (int)strtol (str_color, &error, 10);
|
||||
if (error && !error[0])
|
||||
{
|
||||
snprintf (str_concat, sizeof (str_concat),
|
||||
"\x1B[38;5;%dm",
|
||||
color);
|
||||
string_dyn_concat (out, str_concat);
|
||||
}
|
||||
ptr_string += 5;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while ((flag = gui_color_attr_get_flag (ptr_string[0])) > 0)
|
||||
{
|
||||
gui_color_add_ansi_flag (out, flag);
|
||||
ptr_string++;
|
||||
}
|
||||
if (ptr_string[0] && ptr_string[1])
|
||||
{
|
||||
memcpy (str_color, ptr_string, 2);
|
||||
str_color[2] = '\0';
|
||||
error = NULL;
|
||||
color = (int)strtol (str_color, &error, 10);
|
||||
if (error && !error[0])
|
||||
{
|
||||
ansi_color = gui_color_weechat_to_ansi (color);
|
||||
if (ansi_color >= 0)
|
||||
{
|
||||
snprintf (str_concat, sizeof (str_concat),
|
||||
"\x1B[%dm",
|
||||
(ansi_color < 8) ?
|
||||
ansi_color + 30 : ansi_color - 8 + 90);
|
||||
string_dyn_concat (out, str_concat);
|
||||
}
|
||||
}
|
||||
ptr_string += 2;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case GUI_COLOR_BG_CHAR:
|
||||
ptr_string++;
|
||||
if (ptr_string[0] == GUI_COLOR_EXTENDED_CHAR)
|
||||
{
|
||||
ptr_string++;
|
||||
if (ptr_string[0] && ptr_string[1] && ptr_string[2]
|
||||
&& ptr_string[3] && ptr_string[4])
|
||||
{
|
||||
memcpy (str_color, ptr_string, 5);
|
||||
str_color[5] = '\0';
|
||||
error = NULL;
|
||||
color = (int)strtol (str_color, &error, 10);
|
||||
if (error && !error[0])
|
||||
{
|
||||
snprintf (str_concat, sizeof (str_concat),
|
||||
"\x1B[48;5;%dm",
|
||||
color);
|
||||
string_dyn_concat (out, str_concat);
|
||||
}
|
||||
ptr_string += 5;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ptr_string[0] && ptr_string[1])
|
||||
{
|
||||
memcpy (str_color, ptr_string, 2);
|
||||
str_color[2] = '\0';
|
||||
error = NULL;
|
||||
color = (int)strtol (str_color, &error, 10);
|
||||
if (error && !error[0])
|
||||
{
|
||||
ansi_color = gui_color_weechat_to_ansi (color);
|
||||
if (ansi_color >= 0)
|
||||
{
|
||||
snprintf (str_concat, sizeof (str_concat),
|
||||
"\x1B[%dm",
|
||||
(ansi_color < 8) ?
|
||||
ansi_color + 40 : ansi_color - 8 + 100);
|
||||
string_dyn_concat (out, str_concat);
|
||||
}
|
||||
}
|
||||
ptr_string += 2;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case GUI_COLOR_FG_BG_CHAR:
|
||||
ptr_string++;
|
||||
if (ptr_string[0] == GUI_COLOR_EXTENDED_CHAR)
|
||||
{
|
||||
ptr_string++;
|
||||
while ((flag = gui_color_attr_get_flag (ptr_string[0])) > 0)
|
||||
{
|
||||
gui_color_add_ansi_flag (out, flag);
|
||||
ptr_string++;
|
||||
}
|
||||
if (ptr_string[0] && ptr_string[1] && ptr_string[2]
|
||||
&& ptr_string[3] && ptr_string[4])
|
||||
{
|
||||
memcpy (str_color, ptr_string, 5);
|
||||
str_color[5] = '\0';
|
||||
error = NULL;
|
||||
color = (int)strtol (str_color, &error, 10);
|
||||
if (error && !error[0])
|
||||
{
|
||||
snprintf (str_concat, sizeof (str_concat),
|
||||
"\x1B[38;5;%dm",
|
||||
color);
|
||||
string_dyn_concat (out, str_concat);
|
||||
}
|
||||
ptr_string += 5;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while ((flag = gui_color_attr_get_flag (ptr_string[0])) > 0)
|
||||
{
|
||||
gui_color_add_ansi_flag (out, flag);
|
||||
ptr_string++;
|
||||
}
|
||||
if (ptr_string[0] && ptr_string[1])
|
||||
{
|
||||
memcpy (str_color, ptr_string, 2);
|
||||
str_color[2] = '\0';
|
||||
error = NULL;
|
||||
color = (int)strtol (str_color, &error, 10);
|
||||
if (error && !error[0])
|
||||
{
|
||||
ansi_color = gui_color_weechat_to_ansi (color);
|
||||
if (ansi_color >= 0)
|
||||
{
|
||||
snprintf (str_concat, sizeof (str_concat),
|
||||
"\x1B[%dm",
|
||||
(ansi_color < 8) ?
|
||||
ansi_color + 30 : ansi_color - 8 + 90);
|
||||
string_dyn_concat (out, str_concat);
|
||||
}
|
||||
}
|
||||
ptr_string += 2;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* note: the comma is an old separator not used any
|
||||
* more (since WeeChat 2.6), but we still use it here
|
||||
* so in case of/upgrade this will not break colors in
|
||||
* old messages
|
||||
*/
|
||||
if ((ptr_string[0] == ',') || (ptr_string[0] == '~'))
|
||||
{
|
||||
if (ptr_string[1] == GUI_COLOR_EXTENDED_CHAR)
|
||||
{
|
||||
ptr_string += 2;
|
||||
if (ptr_string[0] && ptr_string[1]
|
||||
&& ptr_string[2] && ptr_string[3]
|
||||
&& ptr_string[4])
|
||||
{
|
||||
memcpy (str_color, ptr_string, 5);
|
||||
str_color[5] = '\0';
|
||||
error = NULL;
|
||||
color = (int)strtol (str_color, &error, 10);
|
||||
if (error && !error[0])
|
||||
{
|
||||
snprintf (str_concat, sizeof (str_concat),
|
||||
"\x1B[48;5;%dm",
|
||||
color);
|
||||
string_dyn_concat (out, str_concat);
|
||||
}
|
||||
ptr_string += 5;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ptr_string++;
|
||||
if (ptr_string[0] && ptr_string[1])
|
||||
{
|
||||
memcpy (str_color, ptr_string, 2);
|
||||
str_color[2] = '\0';
|
||||
error = NULL;
|
||||
color = (int)strtol (str_color, &error, 10);
|
||||
if (error && !error[0])
|
||||
{
|
||||
ansi_color = gui_color_weechat_to_ansi (color);
|
||||
if (ansi_color >= 0)
|
||||
{
|
||||
snprintf (str_concat, sizeof (str_concat),
|
||||
"\x1B[%dm",
|
||||
(ansi_color < 8) ?
|
||||
ansi_color + 40 : ansi_color - 8 + 100);
|
||||
string_dyn_concat (out, str_concat);
|
||||
}
|
||||
}
|
||||
ptr_string += 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case GUI_COLOR_EXTENDED_CHAR:
|
||||
ptr_string++;
|
||||
if ((isdigit (ptr_string[0])) && (isdigit (ptr_string[1]))
|
||||
&& (isdigit (ptr_string[2])) && (isdigit (ptr_string[3]))
|
||||
&& (isdigit (ptr_string[4])))
|
||||
{
|
||||
memcpy (str_color, ptr_string, 5);
|
||||
str_color[5] = '\0';
|
||||
error = NULL;
|
||||
color = (int)strtol (str_color, &error, 10);
|
||||
if (error && !error[0])
|
||||
{
|
||||
snprintf (str_concat, sizeof (str_concat),
|
||||
"\x1B[38;5;%dm",
|
||||
color);
|
||||
string_dyn_concat (out, str_concat);
|
||||
}
|
||||
ptr_string += 5;
|
||||
}
|
||||
break;
|
||||
case GUI_COLOR_EMPHASIS_CHAR:
|
||||
ptr_string++;
|
||||
break;
|
||||
case GUI_COLOR_BAR_CHAR:
|
||||
ptr_string++;
|
||||
switch (ptr_string[0])
|
||||
{
|
||||
case GUI_COLOR_BAR_FG_CHAR:
|
||||
case GUI_COLOR_BAR_BG_CHAR:
|
||||
case GUI_COLOR_BAR_DELIM_CHAR:
|
||||
case GUI_COLOR_BAR_START_INPUT_CHAR:
|
||||
case GUI_COLOR_BAR_START_INPUT_HIDDEN_CHAR:
|
||||
case GUI_COLOR_BAR_MOVE_CURSOR_CHAR:
|
||||
case GUI_COLOR_BAR_START_ITEM:
|
||||
case GUI_COLOR_BAR_START_LINE_ITEM:
|
||||
ptr_string++;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case GUI_COLOR_RESET_CHAR:
|
||||
ptr_string++;
|
||||
string_dyn_concat (out, "\x1B[39m\x1B[49m");
|
||||
break;
|
||||
default:
|
||||
if (isdigit (ptr_string[0]) && isdigit (ptr_string[1]))
|
||||
{
|
||||
memcpy (str_color, ptr_string, 2);
|
||||
str_color[2] = '\0';
|
||||
error = NULL;
|
||||
color = (int)strtol (str_color, &error, 10);
|
||||
if (error && !error[0]
|
||||
&& (color >= 0)
|
||||
&& (color < GUI_COLOR_NUM_COLORS))
|
||||
{
|
||||
fg = gui_color[color]->foreground;
|
||||
bg = gui_color[color]->background;
|
||||
attrs = gui_color_get_extended_flags (
|
||||
gui_color[color]->attributes);
|
||||
string_dyn_concat (out, "\x1B[0m");
|
||||
if (attrs & GUI_COLOR_EXTENDED_BOLD_FLAG)
|
||||
string_dyn_concat (out, "\x1B[1m");
|
||||
if (attrs & GUI_COLOR_EXTENDED_REVERSE_FLAG)
|
||||
string_dyn_concat (out, "\x1B[7m");
|
||||
if (attrs & GUI_COLOR_EXTENDED_ITALIC_FLAG)
|
||||
string_dyn_concat (out, "\x1B[3m");
|
||||
if (attrs & GUI_COLOR_EXTENDED_UNDERLINE_FLAG)
|
||||
string_dyn_concat (out, "\x1B[4m");
|
||||
if ((fg > 0) && (fg & GUI_COLOR_EXTENDED_FLAG))
|
||||
fg &= GUI_COLOR_EXTENDED_MASK;
|
||||
if ((bg > 0) && (bg & GUI_COLOR_EXTENDED_FLAG))
|
||||
bg &= GUI_COLOR_EXTENDED_MASK;
|
||||
if (fg < 0)
|
||||
{
|
||||
string_dyn_concat (out, "\x1B[39m");
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf (str_concat, sizeof (str_concat),
|
||||
"\x1B[38;5;%dm",
|
||||
fg);
|
||||
string_dyn_concat (out, str_concat);
|
||||
}
|
||||
if (bg < 0)
|
||||
{
|
||||
string_dyn_concat (out, "\x1B[49m");
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf (str_concat, sizeof (str_concat),
|
||||
"\x1B[48;5;%dm",
|
||||
bg);
|
||||
string_dyn_concat (out, str_concat);
|
||||
}
|
||||
}
|
||||
ptr_string += 2;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case GUI_COLOR_SET_ATTR_CHAR:
|
||||
ptr_string++;
|
||||
if (ptr_string[0])
|
||||
{
|
||||
switch (ptr_string[0])
|
||||
{
|
||||
case GUI_COLOR_ATTR_BOLD_CHAR:
|
||||
string_dyn_concat (out, "\x1B[1m");
|
||||
break;
|
||||
case GUI_COLOR_ATTR_REVERSE_CHAR:
|
||||
string_dyn_concat (out, "\x1B[7m");
|
||||
break;
|
||||
case GUI_COLOR_ATTR_ITALIC_CHAR:
|
||||
string_dyn_concat (out, "\x1B[3m");
|
||||
break;
|
||||
case GUI_COLOR_ATTR_UNDERLINE_CHAR:
|
||||
string_dyn_concat (out, "\x1B[4m");
|
||||
break;
|
||||
}
|
||||
ptr_string++;
|
||||
}
|
||||
break;
|
||||
case GUI_COLOR_REMOVE_ATTR_CHAR:
|
||||
ptr_string++;
|
||||
if (ptr_string[0])
|
||||
{
|
||||
switch (ptr_string[0])
|
||||
{
|
||||
case GUI_COLOR_ATTR_BOLD_CHAR:
|
||||
string_dyn_concat (out, "\x1B[21m");
|
||||
break;
|
||||
case GUI_COLOR_ATTR_REVERSE_CHAR:
|
||||
string_dyn_concat (out, "\x1B[27m");
|
||||
break;
|
||||
case GUI_COLOR_ATTR_ITALIC_CHAR:
|
||||
string_dyn_concat (out, "\x1B[23m");
|
||||
break;
|
||||
case GUI_COLOR_ATTR_UNDERLINE_CHAR:
|
||||
string_dyn_concat (out, "\x1B[24m");
|
||||
break;
|
||||
}
|
||||
ptr_string++;
|
||||
}
|
||||
break;
|
||||
case GUI_COLOR_RESET_CHAR:
|
||||
string_dyn_concat (out, "\x1B[0m");
|
||||
ptr_string++;
|
||||
break;
|
||||
default:
|
||||
length = utf8_char_size ((char *)ptr_string);
|
||||
if (length == 0)
|
||||
length = 1;
|
||||
memcpy (str_concat, ptr_string, length);
|
||||
str_concat[length] = '\0';
|
||||
string_dyn_concat (out, str_concat);
|
||||
ptr_string += length;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return string_dyn_free (out, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Emphasizes a string or regular expression in a string (which can contain
|
||||
* colors).
|
||||
|
||||
@@ -188,6 +188,7 @@ extern int gui_color_convert_term_to_rgb (int color);
|
||||
extern int gui_color_convert_rgb_to_term (int rgb, int limit);
|
||||
extern char *gui_color_decode (const char *string, const char *replacement);
|
||||
extern char *gui_color_decode_ansi (const char *string, int keep_colors);
|
||||
extern char *gui_color_encode_ansi (const char *string);
|
||||
extern char *gui_color_emphasize (const char *string, const char *search,
|
||||
int case_sensitive, regex_t *regex);
|
||||
extern void gui_color_free (struct t_gui_color *color);
|
||||
@@ -202,6 +203,8 @@ extern void gui_color_end ();
|
||||
/* color functions (GUI dependent) */
|
||||
|
||||
extern int gui_color_search (const char *color_name);
|
||||
extern const char *gui_color_search_index (int index);
|
||||
extern int gui_color_get_extended_flags (int attrs);
|
||||
extern int gui_color_assign (int *color, char const *color_name);
|
||||
extern int gui_color_assign_by_diff (int *color, const char *color_name,
|
||||
int diff);
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "src/core/wee-config.h"
|
||||
#include "src/core/wee-string.h"
|
||||
#include "src/gui/gui-color.h"
|
||||
}
|
||||
@@ -37,6 +38,11 @@ extern "C"
|
||||
STRCMP_EQUAL(__result, decoded); \
|
||||
free (decoded);
|
||||
|
||||
#define WEE_CHECK_ENCODE_ANSI(__result, __string) \
|
||||
encoded = gui_color_encode_ansi (__string); \
|
||||
STRCMP_EQUAL(__result, encoded); \
|
||||
free (encoded);
|
||||
|
||||
#define WEE_CHECK_EMPHASIZE(__result, __string, __search, \
|
||||
__case_sensitive, __regex) \
|
||||
emphasized = gui_color_emphasize (__string, __search, \
|
||||
@@ -388,6 +394,14 @@ TEST(GuiColor, ColorDecode)
|
||||
WEE_CHECK_DECODE("test_227,blue", string, NULL);
|
||||
WEE_CHECK_DECODE("test_227,blue", string, "");
|
||||
WEE_CHECK_DECODE("test_?227,blue", string, "?");
|
||||
|
||||
/* WeeChat color */
|
||||
snprintf (string, sizeof (string),
|
||||
"test_%soption_weechat.color.chat_host",
|
||||
GUI_COLOR(GUI_COLOR_CHAT_HOST));
|
||||
WEE_CHECK_DECODE("test_option_weechat.color.chat_host", string, NULL);
|
||||
WEE_CHECK_DECODE("test_option_weechat.color.chat_host", string, "");
|
||||
WEE_CHECK_DECODE("test_?option_weechat.color.chat_host", string, "?");
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -550,6 +564,132 @@ TEST(GuiColor, ColorDecodeAnsi)
|
||||
1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests functions:
|
||||
* gui_color_encode_ansi
|
||||
*/
|
||||
|
||||
TEST(GuiColor, ColorEncodeAnsi)
|
||||
{
|
||||
char string[256], *encoded;
|
||||
|
||||
/* NULL/empty string */
|
||||
POINTERS_EQUAL(NULL, gui_color_encode_ansi (NULL));
|
||||
WEE_CHECK_ENCODE_ANSI("", "");
|
||||
|
||||
/* reset */
|
||||
snprintf (string, sizeof (string),
|
||||
"test_%sreset", gui_color_get_custom ("reset"));
|
||||
WEE_CHECK_ENCODE_ANSI("test_\x1B[0mreset", string);
|
||||
|
||||
/* bold */
|
||||
snprintf (string, sizeof (string),
|
||||
"test_%sbold%s_end",
|
||||
gui_color_get_custom ("bold"),
|
||||
gui_color_get_custom ("-bold"));
|
||||
WEE_CHECK_ENCODE_ANSI("test_\x1B[1mbold\x1B[21m_end", string);
|
||||
|
||||
/* reverse */
|
||||
snprintf (string, sizeof (string),
|
||||
"test_%sreverse%s_end",
|
||||
gui_color_get_custom ("reverse"),
|
||||
gui_color_get_custom ("-reverse"));
|
||||
WEE_CHECK_ENCODE_ANSI("test_\x1B[7mreverse\x1B[27m_end", string);
|
||||
|
||||
/* italic */
|
||||
snprintf (string, sizeof (string),
|
||||
"test_%sitalic%s_end",
|
||||
gui_color_get_custom ("italic"),
|
||||
gui_color_get_custom ("-italic"));
|
||||
WEE_CHECK_ENCODE_ANSI("test_\x1B[3mitalic\x1B[23m_end", string);
|
||||
|
||||
/* underline */
|
||||
snprintf (string, sizeof (string),
|
||||
"test_%sunderline%s_end",
|
||||
gui_color_get_custom ("underline"),
|
||||
gui_color_get_custom ("-underline"));
|
||||
WEE_CHECK_ENCODE_ANSI("test_\x1B[4munderline\x1B[24m_end", string);
|
||||
|
||||
/* text color */
|
||||
snprintf (string, sizeof (string),
|
||||
"test_%sblue",
|
||||
gui_color_get_custom ("blue"));
|
||||
WEE_CHECK_ENCODE_ANSI("test_\x1B[34mblue", string);
|
||||
|
||||
/* bright text color */
|
||||
snprintf (string, sizeof (string),
|
||||
"test_%slightgreen",
|
||||
gui_color_get_custom ("lightgreen"));
|
||||
WEE_CHECK_ENCODE_ANSI("test_\x1B[92mlightgreen", string);
|
||||
|
||||
/* text terminal color */
|
||||
snprintf (string, sizeof (string),
|
||||
"test_%s214",
|
||||
gui_color_get_custom ("214"));
|
||||
WEE_CHECK_ENCODE_ANSI("test_\x1B[38;5;214m214", string);
|
||||
|
||||
/* background color */
|
||||
snprintf (string, sizeof (string),
|
||||
"test_%sbg_red",
|
||||
gui_color_get_custom (",red"));
|
||||
WEE_CHECK_ENCODE_ANSI("test_\x1B[41mbg_red", string);
|
||||
|
||||
/* bright background color */
|
||||
snprintf (string, sizeof (string),
|
||||
"test_%sbg_lightgreen",
|
||||
gui_color_get_custom (",lightgreen"));
|
||||
WEE_CHECK_ENCODE_ANSI("test_\x1B[102mbg_lightgreen", string);
|
||||
|
||||
/* background terminal color */
|
||||
snprintf (string, sizeof (string),
|
||||
"test_%sbg_240",
|
||||
gui_color_get_custom (",240"));
|
||||
WEE_CHECK_ENCODE_ANSI("test_\x1B[48;5;240mbg_240", string);
|
||||
|
||||
/* WeeChat color */
|
||||
snprintf (string, sizeof (string),
|
||||
"test_%soption_weechat.color.chat_host",
|
||||
GUI_COLOR(GUI_COLOR_CHAT_HOST));
|
||||
WEE_CHECK_ENCODE_ANSI(
|
||||
"test_\x1B[0m\x1B[38;5;6m\x1B[49m"
|
||||
"option_weechat.color.chat_host",
|
||||
string);
|
||||
|
||||
/* WeeChat bright color */
|
||||
snprintf (string, sizeof (string),
|
||||
"test_%soption_weechat.color.chat_nick",
|
||||
GUI_COLOR(GUI_COLOR_CHAT_NICK));
|
||||
WEE_CHECK_ENCODE_ANSI(
|
||||
"test_\x1B[0m\x1B[38;5;14m\x1B[49m"
|
||||
"option_weechat.color.chat_nick",
|
||||
string);
|
||||
|
||||
/* WeeChat color with attributes */
|
||||
config_file_option_set (config_color_chat_host, "_green", 1);
|
||||
snprintf (string, sizeof (string),
|
||||
"test_%soption_weechat.color.chat_host",
|
||||
GUI_COLOR(GUI_COLOR_CHAT_HOST));
|
||||
WEE_CHECK_ENCODE_ANSI(
|
||||
"test_\x1B[0m\x1B[4m\x1B[38;5;2m\x1B[49m"
|
||||
"option_weechat.color.chat_host",
|
||||
string);
|
||||
config_file_option_reset (config_color_chat_host, 1);
|
||||
|
||||
/* multiple colors/attributes */
|
||||
snprintf (string, sizeof (string),
|
||||
"%shello, %sthis is%s a test %sblue %sreset %syellow,red here!",
|
||||
gui_color_get_custom (",blue"),
|
||||
gui_color_get_custom ("bold"),
|
||||
gui_color_get_custom ("-bold"),
|
||||
gui_color_get_custom ("blue"),
|
||||
gui_color_get_custom ("reset"),
|
||||
gui_color_get_custom ("yellow,red"));
|
||||
WEE_CHECK_ENCODE_ANSI(
|
||||
"\x1B[44mhello, \x1B[1mthis is\x1B[21m a test \x1B[34mblue \x1B[0m"
|
||||
"reset \x1B[93m\x1B[41myellow,red here!",
|
||||
string);
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests functions:
|
||||
* gui_color_emphasize
|
||||
|
||||
Reference in New Issue
Block a user