Support more terminal attributes other than bold. (#9535)

This commit is contained in:
Fangrui Song 2018-02-28 08:54:39 -08:00 committed by radare
parent 9a3fe466fd
commit a244064276
4 changed files with 94 additions and 69 deletions

View File

@ -82,22 +82,17 @@ R_API RColor r_cons_color_random(ut8 alpha) {
}
int r = r_num_rand (16);
switch (r) {
case 0: rcolor = (RColor) RColor_RED; break;
case 1: rcolor = (RColor) RColor_BRED; break;
case 2: rcolor = (RColor) RColor_WHITE; break;
case 3: rcolor = (RColor) RColor_BWHITE; break;
case 4: rcolor = (RColor) RColor_GREEN; break;
case 5: rcolor = (RColor) RColor_BGREEN; break;
case 6: rcolor = (RColor) RColor_MAGENTA; break;
case 7: rcolor = (RColor) RColor_BMAGENTA; break;
case 8: rcolor = (RColor) RColor_YELLOW; break;
case 9: rcolor = (RColor) RColor_BYELLOW; break;
case 10: rcolor = (RColor) RColor_CYAN; break;
case 11: rcolor = (RColor) RColor_BCYAN; break;
case 12: rcolor = (RColor) RColor_BLUE; break;
case 13: rcolor = (RColor) RColor_BBLUE; break;
case 14: rcolor = (RColor) RColor_GRAY; break;
case 15: rcolor = (RColor) RColor_BGRAY; break;
case 0: case 1: rcolor = (RColor) RColor_RED; break;
case 2: case 3: rcolor = (RColor) RColor_WHITE; break;
case 4: case 5: rcolor = (RColor) RColor_GREEN; break;
case 6: case 7: rcolor = (RColor) RColor_MAGENTA; break;
case 8: case 9: rcolor = (RColor) RColor_YELLOW; break;
case 10: case 11: rcolor = (RColor) RColor_CYAN; break;
case 12: case 13: rcolor = (RColor) RColor_BLUE; break;
case 14: case 15: rcolor = (RColor) RColor_GRAY; break;
}
if (r & 1) {
rcolor.attr = R_CONS_ATTR_BOLD;
}
return rcolor;
}

View File

@ -81,20 +81,13 @@ struct {
} colors[] = {
{ "black", RColor_BLACK, Color_BLACK, Color_BGBLACK },
{ "red", RColor_RED, Color_RED, Color_BGRED },
{ "bred", RColor_BRED, Color_BRED, Color_BGRED },
{ "white", RColor_WHITE, Color_WHITE, Color_BGWHITE },
{ "green", RColor_GREEN, Color_GREEN, Color_BGGREEN },
{ "bgreen", RColor_BGREEN, Color_BGREEN, Color_BGGREEN },
{ "magenta", RColor_MAGENTA, Color_MAGENTA, Color_BGMAGENTA },
{ "bmagenta", RColor_BMAGENTA, Color_BMAGENTA, Color_BGMAGENTA },
{ "yellow", RColor_YELLOW, Color_YELLOW, Color_BGYELLOW },
{ "byellow", RColor_BYELLOW, Color_BYELLOW, Color_BGBYELLOW },
{ "cyan", RColor_CYAN, Color_CYAN, Color_BGCYAN },
{ "bcyan", RColor_BCYAN, Color_BCYAN, Color_BGCYAN },
{ "blue", RColor_BLUE, Color_BLUE, Color_BGBLUE },
{ "bblue", RColor_BBLUE, Color_BBLUE, Color_BGBLUE },
{ "gray", RColor_GRAY, Color_GRAY, Color_BGGRAY },
{ "bgray", RColor_BGRAY, Color_BGRAY, Color_BGGRAY },
{ "none", RColor_NULL, Color_RESET, Color_RESET },
{ NULL, RColor_NULL, NULL, NULL }
};
@ -118,7 +111,8 @@ R_API void r_cons_pal_init () {
cons->cpal.args = (RColor) RColor_YELLOW;
cons->cpal.bin = (RColor) RColor_CYAN;
cons->cpal.btext = (RColor) RColor_YELLOW;
cons->cpal.call = (RColor) RColor_BGREEN;
cons->cpal.call = (RColor) RColor_GREEN;
cons->cpal.call.attr = R_CONS_ATTR_BOLD;
cons->cpal.cjmp = (RColor) RColor_GREEN;
cons->cpal.cmp = (RColor) RColor_CYAN;
cons->cpal.comment = (RColor) RColor_RED;
@ -132,7 +126,8 @@ R_API void r_cons_pal_init () {
cons->cpal.fname = (RColor) RColor_RED;
cons->cpal.help = (RColor) RColor_GREEN;
cons->cpal.input = (RColor) RColor_WHITE;
cons->cpal.invalid = (RColor) RColor_BRED;
cons->cpal.invalid = (RColor) RColor_RED;
cons->cpal.invalid.attr = R_CONS_ATTR_BOLD;
cons->cpal.jmp = (RColor) RColor_GREEN;
cons->cpal.label = (RColor) RColor_CYAN;
cons->cpal.math = (RColor) RColor_YELLOW;
@ -141,14 +136,16 @@ R_API void r_cons_pal_init () {
cons->cpal.num = (RColor) RColor_YELLOW;
cons->cpal.offset = (RColor) RColor_GREEN;
cons->cpal.other = (RColor) RColor_WHITE;
cons->cpal.pop = (RColor) RColor_BMAGENTA;
cons->cpal.pop = (RColor) RColor_MAGENTA;
cons->cpal.pop.attr = R_CONS_ATTR_BOLD;
cons->cpal.prompt = (RColor) RColor_YELLOW;
cons->cpal.push = (RColor) RColor_MAGENTA;
cons->cpal.crypto = (RColor) RColor_BGBLUE;
cons->cpal.reg = (RColor) RColor_CYAN;
cons->cpal.ret = (RColor) RColor_RED;
cons->cpal.swi = (RColor) RColor_MAGENTA;
cons->cpal.trap = (RColor) RColor_BRED;
cons->cpal.trap = (RColor) RColor_RED;
cons->cpal.trap.attr = R_CONS_ATTR_BOLD;
cons->cpal.ai_read = (RColor) RColor_GREEN;
cons->cpal.ai_write = (RColor) RColor_BLUE;
@ -207,6 +204,7 @@ R_API char *r_cons_pal_parse (const char *str, RColor *outcol) {
RColor rcolor = (RColor) RColor_BLACK;
char *fgcolor;
char *bgcolor;
char *attr = NULL;
char out[128];
if (!str) {
return NULL;
@ -218,7 +216,11 @@ R_API char *r_cons_pal_parse (const char *str, RColor *outcol) {
bgcolor = strchr (fgcolor + 1, ' ');
out[0] = 0;
if (bgcolor) {
*bgcolor++ = 0;
*bgcolor++ = '\0';
attr = strchr (bgcolor, ' ');
if (attr) {
*attr++ = '\0';
}
}
// Handle first color (fgcolor)
@ -298,6 +300,30 @@ R_API char *r_cons_pal_parse (const char *str, RColor *outcol) {
}
}
}
if (attr) {
// Parse extra attributes.
const char *p = attr;
while (p) {
if (!strncmp(p, "bold", 4)) {
rcolor.attr |= R_CONS_ATTR_BOLD;
} else if (!strncmp(p, "dim", 3)) {
rcolor.attr |= 1u << 2;
} else if (!strncmp(p, "italic", 6)) {
rcolor.attr |= 1u << 3;
} else if (!strncmp(p, "underline", 9)) {
rcolor.attr |= 1u << 4;
} else if (!strncmp(p, "blink", 5)) {
rcolor.attr |= 1u << 5;
} else {
eprintf ("Failed to parse terminal attributes: %s\n", p);
break;
}
p = strchr (p, ' ');
if (p) {
p++;
}
}
}
if (outcol) {
*outcol = rcolor;
}

View File

@ -164,15 +164,28 @@ R_API char *r_cons_rgb_str_off(char *outstr, ut64 off) {
}
/* Compute color string depending on cons->color */
static void r_cons_rgb_gen (char *outstr, ut8 a, ut8 r, ut8 g, ut8 b) {
static void r_cons_rgb_gen (char *outstr, ut8 attr, ut8 a, ut8 r, ut8 g, ut8 b) {
ut8 fgbg = (a == ALPHA_BG)? 48: 38; // ANSI codes for Background/Foreground
const char *bold = (a & ALPHA_BOLD)? "1;": "";
int i = 2;
outstr[0] = '\x1b';
outstr[1] = '[';
for (; attr; attr &= attr - 1) {
switch (attr & -attr) {
case 1u << 1: outstr[i] = '1'; break;
case 1u << 2: outstr[i] = '2'; break;
case 1u << 3: outstr[i] = '3'; break;
case 1u << 4: outstr[i] = '4'; break;
case 1u << 5: outstr[i] = '5'; break;
}
outstr[i + 1] = ';';
i += 2;
}
switch (r_cons_singleton ()->color) {
case COLOR_MODE_256: // 256 color palette
sprintf (outstr, "\x1b[%s%d;5;%dm", bold, fgbg, rgb (r, g, b));
sprintf (outstr + i, "%d;5;%dm", fgbg, rgb (r, g, b));
break;
case COLOR_MODE_16M: // 16M (truecolor)
sprintf (outstr, "\x1b[%s%d;2;%d;%d;%dm", bold, fgbg, r, g, b);
sprintf (outstr + i, "%d;2;%d;%d;%dm", fgbg, r, g, b);
break;
case COLOR_MODE_16: // ansi 16 colors
{
@ -182,7 +195,7 @@ static void r_cons_rgb_gen (char *outstr, ut8 a, ut8 r, ut8 g, ut8 b) {
g = (g >= k) ? 1 : 0;
b = (b >= k) ? 1 : 0;
k = (r ? 1 : 0) + (g ? (b ? 6 : 2) : (b ? 4 : 0));
sprintf (outstr, "\x1b[%s%dm", bold, fgbg + k);
sprintf (outstr + i, "%dm", fgbg + k);
}
break;
}
@ -198,12 +211,11 @@ R_API char *r_cons_rgb_str (char *outstr, RColor *rcolor) {
sprintf (outstr, "%s", Color_RESET);
return outstr;
}
r_cons_rgb_gen (outstr, rcolor->a, rcolor->r, rcolor->g, rcolor->b);
// If the color handles both foreground and background, also add background
if (rcolor->a == ALPHA_FGBG) {
ut8 length = strlen (outstr);
r_cons_rgb_gen (outstr + length, ALPHA_BG, rcolor->r2, rcolor->g2, rcolor->b2);
r_cons_rgb_gen (outstr, 0, ALPHA_BG, rcolor->r2, rcolor->g2, rcolor->b2);
}
r_cons_rgb_gen (outstr + strlen (outstr), rcolor->attr, rcolor->a, rcolor->r, rcolor->g, rcolor->b);
return outstr;
}

View File

@ -142,9 +142,12 @@ enum {
enum { COLOR_MODE_DISABLED = 0, COLOR_MODE_16, COLOR_MODE_256, COLOR_MODE_16M };
enum { ALPHA_RESET = 0x00, ALPHA_FG = 0x01, ALPHA_BG = 0x02, ALPHA_FGBG = 0x03, ALPHA_BOLD = 0x04 };
enum { ALPHA_RESET = 0x00, ALPHA_FG = 0x01, ALPHA_BG = 0x02, ALPHA_FGBG = 0x03 };
enum { R_CONS_ATTR_BOLD = 1 << 1 };
typedef struct rcolor_t {
// bold, italic, underline, ...
ut8 attr;
ut8 a;
ut8 r;
ut8 g;
@ -483,7 +486,6 @@ typedef struct r_cons_t {
#define Color_BGMAGENTA "\x1b[45m"
#define Color_YELLOW "\x1b[33m"
#define Color_BGYELLOW "\x1b[43m"
#define Color_BGBYELLOW "\x1b[1;43m"
#define Color_CYAN "\x1b[36m"
#define Color_BGCYAN "\x1b[46m"
#define Color_BLUE "\x1b[34m"
@ -503,39 +505,29 @@ typedef struct r_cons_t {
#define Color_BGRAY "\x1b[1;38m"
#ifdef _MSC_VER
#define RCOLOR(a, r, g, b, bgr, bgg, bgb) {a, r, g, b, bgr, bgg, bgb}
#define RCOLOR(a, r, g, b, bgr, bgg, bgb) {0, a, r, g, b, bgr, bgg, bgb}
#else
#define RCOLOR(a, r, g, b, bgr, bgg, bgb) (RColor) {a, r, g, b, bgr, bgg, bgb}
#define RCOLOR(a, r, g, b, bgr, bgg, bgb) (RColor) {0, a, r, g, b, bgr, bgg, bgb}
#endif
#define RColor_NULL RCOLOR(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
#define RColor_BLACK RCOLOR(ALPHA_FG, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
#define RColor_BGBLACK RCOLOR(ALPHA_BG, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
#define RColor_RED RCOLOR(ALPHA_FG, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00)
#define RColor_BGRED RCOLOR(ALPHA_BG, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00)
#define RColor_WHITE RCOLOR(ALPHA_FG, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00)
#define RColor_BGWHITE RCOLOR(ALPHA_BG, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00)
#define RColor_GREEN RCOLOR(ALPHA_FG, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00)
#define RColor_BGGREEN RCOLOR(ALPHA_BG, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00)
#define RColor_MAGENTA RCOLOR(ALPHA_FG, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00)
#define RColor_BGMAGENTA RCOLOR(ALPHA_BG, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00)
#define RColor_YELLOW RCOLOR(ALPHA_FG, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00)
#define RColor_BGYELLOW RCOLOR(ALPHA_BG, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00)
#define RColor_CYAN RCOLOR(ALPHA_FG, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00)
#define RColor_BGCYAN RCOLOR(ALPHA_BG, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00)
#define RColor_BLUE RCOLOR(ALPHA_FG, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00)
#define RColor_BGBLUE RCOLOR(ALPHA_BG, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00)
#define RColor_GRAY RCOLOR(ALPHA_FG, 0x7f, 0x7f, 0x7f, 0x00, 0x00, 0x00)
#define RColor_BGGRAY RCOLOR(ALPHA_BG, 0x7f, 0x7f, 0x7f, 0x00, 0x00, 0x00)
#define RColor_BBLACK RCOLOR(ALPHA_BOLD, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
#define RColor_BRED RCOLOR(ALPHA_BOLD, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00)
#define RColor_BGBYELLOW RCOLOR(ALPHA_BG, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00)
#define RColor_BWHITE RCOLOR(ALPHA_BOLD, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00)
#define RColor_BGREEN RCOLOR(ALPHA_BOLD, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00)
#define RColor_BMAGENTA RCOLOR(ALPHA_BOLD, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00)
#define RColor_BYELLOW RCOLOR(ALPHA_BOLD, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00)
#define RColor_BCYAN RCOLOR(ALPHA_BOLD, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00)
#define RColor_BBLUE RCOLOR(ALPHA_BOLD, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00)
#define RColor_BGRAY RCOLOR(ALPHA_BOLD, 0x7f, 0x7f, 0x7f, 0x00, 0x00, 0x00)
#define RColor_NULL RCOLOR(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
#define RColor_BLACK RCOLOR(ALPHA_FG, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
#define RColor_BGBLACK RCOLOR(ALPHA_BG, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
#define RColor_RED RCOLOR(ALPHA_FG, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00)
#define RColor_BGRED RCOLOR(ALPHA_BG, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00)
#define RColor_WHITE RCOLOR(ALPHA_FG, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00)
#define RColor_BGWHITE RCOLOR(ALPHA_BG, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00)
#define RColor_GREEN RCOLOR(ALPHA_FG, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00)
#define RColor_BGGREEN RCOLOR(ALPHA_BG, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00)
#define RColor_MAGENTA RCOLOR(ALPHA_FG, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00)
#define RColor_BGMAGENTA RCOLOR(ALPHA_BG, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00)
#define RColor_YELLOW RCOLOR(ALPHA_FG, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00)
#define RColor_BGYELLOW RCOLOR(ALPHA_BG, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00)
#define RColor_CYAN RCOLOR(ALPHA_FG, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00)
#define RColor_BGCYAN RCOLOR(ALPHA_BG, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00)
#define RColor_BLUE RCOLOR(ALPHA_FG, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00)
#define RColor_BGBLUE RCOLOR(ALPHA_BG, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00)
#define RColor_GRAY RCOLOR(ALPHA_FG, 0x7f, 0x7f, 0x7f, 0x00, 0x00, 0x00)
#define RColor_BGGRAY RCOLOR(ALPHA_BG, 0x7f, 0x7f, 0x7f, 0x00, 0x00, 0x00)
#define Colors_PLAIN { \
Color_BLACK, Color_RED, Color_WHITE, \