Skip to content

Commit

Permalink
merge: improve ANSI color support
Browse files Browse the repository at this point in the history
 * color: add ANSI RGB support
 * color: reset simple attributes
 * color: Support ANSI 2x clear sequences
  • Loading branch information
flatcap committed Oct 28, 2023
2 parents b6e3e40 + f5a14b1 commit f9c11d6
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 6 deletions.
97 changes: 91 additions & 6 deletions color/parse_ansi.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,31 @@ int ansi_color_parse_single(const char *buf, struct AnsiColor *ansi, bool dry_ru
ansi->attrs |= A_BOLD;
pos += 2;
}
else if ((buf[pos] == '2') && isdigit(buf[pos + 1]) && ansi_is_end_char(buf[pos + 2]))
{
char digit = buf[pos + 1];
pos += 3;
if (digit == '2')
{
ansi->attrs &= ~A_BOLD; // Clear the flag
}
else if (digit == '3')
{
ansi->attrs &= ~A_ITALIC; // Clear the flag
}
else if (digit == '4')
{
ansi->attrs &= ~A_UNDERLINE; // Clear the flag
}
else if (digit == '5')
{
ansi->attrs &= ~A_BLINK; // Clear the flag
}
else if (digit == '7')
{
ansi->attrs &= ~A_REVERSE; // Clear the flag
}
}
else if ((buf[pos] == '3') && ansi_is_end_char(buf[pos + 1]))
{
ansi->attrs |= A_ITALIC;
Expand Down Expand Up @@ -190,9 +215,39 @@ int ansi_color_parse_single(const char *buf, struct AnsiColor *ansi, bool dry_ru
else if (mutt_str_startswith(buf + pos, "38;2;") && isdigit(buf[pos + 5]))
{
// 38;2;R;G;B true colour foreground
pos += ansi_skip_sequence(buf + pos + 5);
pos += ansi_skip_sequence(buf + pos);
pos += ansi_skip_sequence(buf + pos);
long r = -1;
long g = -1;
long b = -1;
char *end = NULL;
unsigned long value = 0;
pos += 5; // Skip 38;2;

value = strtoul(buf + pos, &end, 10);
if ((value < 0) || (value > 255) || !end || (end[0] != ';'))
{
return 0;
}
r = value;
pos += end - &buf[pos] + 1;

value = strtoul(buf + pos, &end, 10);
if ((value < 0) || (value > 255) || !end || (end[0] != ';'))
{
return 0;
}
g = value;
pos += end - &buf[pos] + 1;

value = strtoul(buf + pos, &end, 10);
if ((value < 0) || (value > 255) || !end || (end[0] != 'm'))
{
return 0;
}
b = value;
pos += end - &buf[pos] + 1;

elem->color = (r << 16) + (g << 8) + (b << 0);
elem->type = CT_RGB;
}
else
{
Expand Down Expand Up @@ -248,9 +303,39 @@ int ansi_color_parse_single(const char *buf, struct AnsiColor *ansi, bool dry_ru
else if (mutt_str_startswith(buf + pos, "48;2;") && isdigit(buf[pos + 5]))
{
// 48;2;R;G;B true colour background
pos += ansi_skip_sequence(buf + pos + 5);
pos += ansi_skip_sequence(buf + pos);
pos += ansi_skip_sequence(buf + pos);
long r = -1;
long g = -1;
long b = -1;
char *end = NULL;
unsigned long value = 0;
pos += 5; // Skip 48;2;

value = strtoul(buf + pos, &end, 10);
if ((value < 0) || (value > 255) || !end || (end[0] != ';'))
{
return 0;
}
r = value;
pos += end - &buf[pos] + 1;

value = strtoul(buf + pos, &end, 10);
if ((value < 0) || (value > 255) || !end || (end[0] != ';'))
{
return 0;
}
g = value;
pos += end - &buf[pos] + 1;

value = strtoul(buf + pos, &end, 10);
if ((value < 0) || (value > 255) || !end || (end[0] != 'm'))
{
return 0;
}
b = value;
pos += end - &buf[pos] + 1;

elem->color = (r << 16) + (g << 8) + (b << 0);
elem->type = CT_RGB;
}
else
{
Expand Down
4 changes: 4 additions & 0 deletions color/simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,17 @@ void simple_colors_init(void)

// Set some defaults
color_debug(LL_DEBUG5, "init indicator, markers, etc\n");
SimpleColors[MT_COLOR_BOLD].attrs = A_BOLD;
SimpleColors[MT_COLOR_INDICATOR].attrs = A_REVERSE;
SimpleColors[MT_COLOR_ITALIC].attrs = A_ITALIC;
SimpleColors[MT_COLOR_MARKERS].attrs = A_REVERSE;
SimpleColors[MT_COLOR_SEARCH].attrs = A_REVERSE;
#ifdef USE_SIDEBAR
SimpleColors[MT_COLOR_SIDEBAR_HIGHLIGHT].attrs = A_UNDERLINE;
#endif
SimpleColors[MT_COLOR_STATUS].attrs = A_REVERSE;
SimpleColors[MT_COLOR_STRIPE_EVEN].attrs = A_BOLD;
SimpleColors[MT_COLOR_UNDERLINE].attrs = A_UNDERLINE;
}

/**
Expand All @@ -74,6 +77,7 @@ void simple_colors_cleanup(void)
{
attr_color_clear(&SimpleColors[i]);
}
simple_colors_init();
}

/**
Expand Down

0 comments on commit f9c11d6

Please sign in to comment.