Skip to content

Commit

Permalink
Initial support for truecolor (xterm only)
Browse files Browse the repository at this point in the history
Add e scr.truecolor variable
Add !! command to save history
Add ec: command to randomize palette
Minor rgb trash fix,
  • Loading branch information
radare committed Jul 17, 2013
1 parent a1123b6 commit be46b9d
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 91 deletions.
1 change: 1 addition & 0 deletions libr/cons/cons.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ R_API RCons *r_cons_new () {
eprintf ("r_cons: Cannot set control console handler\n");
#endif
I.pager = NULL; /* no pager by default */
I.truecolor = 0;
r_cons_pal_init (NULL);
r_cons_rgb_init ();
r_cons_reset ();
Expand Down
1 change: 1 addition & 0 deletions libr/cons/d/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ all clean:
.PHONY: all clean install install-symlink symstall

install: ${F_SDB}
rm -rf $P
mkdir -p $P
cp -f * $P

Expand Down
File renamed without changes.
34 changes: 25 additions & 9 deletions libr/cons/pal.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ R_API void r_cons_pal_init(const char *foo) {
cons->pal.flag = Color_CYAN;
cons->pal.label = Color_CYAN;
cons->pal.flow = Color_CYAN;
cons->pal.math = Color_YELLOW;
cons->pal.bin = Color_RED;
cons->pal.b0x00 = Color_GREEN;
cons->pal.b0x7f = Color_CYAN;
cons->pal.b0xff = Color_RED;
Expand Down Expand Up @@ -48,6 +50,7 @@ struct {
{ "cyan", Color_CYAN, Color_BGCYAN },
{ "blue", Color_BLUE, Color_BGBLUE },
{ "gray", Color_GRAY, Color_BGGRAY },
{ "none", Color_RESET, Color_RESET },
{ NULL, NULL, NULL }
};

Expand All @@ -57,6 +60,22 @@ static inline ut8 rgbnum (const char ch) {
return r*16;
}

R_API void r_cons_pal_random() {
ut8 r, g, b;
char val[32];
const char *k;
int i;
for (i=0;;i++) {
k = r_cons_pal_get_i (i);
if (!k) break;
r = r_num_rand (0xf);
g = r_num_rand (0xf);
b = r_num_rand (0xf);
sprintf (val, "rgb:%x%x%x", r, g, b);
r_cons_pal_set (k, val);
}
}

R_API char *r_cons_pal_parse(const char *str) {
int i;
ut8 r, g, b;
Expand Down Expand Up @@ -107,8 +126,6 @@ static struct {
{ "math", r_offsetof (RConsPalette, math) },
{ "bin", r_offsetof (RConsPalette, bin) },
{ "btext", r_offsetof (RConsPalette, btext) },
{ "math", r_offsetof (RConsPalette, math) },
{ "bin", r_offsetof (RConsPalette, bin) },
{ "push", r_offsetof (RConsPalette, push) },
{ "pop", r_offsetof (RConsPalette, pop) },
{ "jmp", r_offsetof (RConsPalette, jmp) },
Expand Down Expand Up @@ -203,17 +220,16 @@ R_API void r_cons_pal_list (int rad) {
int i;
for (i=0; keys[i].name; i++) {
color = (char**)(p + keys[i].off);
color = (char**)*color;
if (rad) {
r = g = b = 0;
r_cons_rgb_parse (color, &r, &g, &b, NULL);
r_cons_rgb_parse (*color, &r, &g, &b, NULL);
rgbstr[0] = 0;
r_cons_rgb_str (rgbstr, r, g, b, 0);
r >>=4;
g >>=4;
b >>=4;
r_cons_printf ("ec %s %srgb:%x%x%x"Color_RESET " # vs %sTEST"Color_RESET"\n",
keys[i].name, color, r, g, b, rgbstr);
r >>= 4;
g >>= 4;
b >>= 4;
r_cons_printf ("ec %s rgb:%x%x%x\n", //Color_RESET " # vs %sTEST"Color_RESET"\n",
keys[i].name, r, g, b);
} else
r_cons_printf (" %s##"Color_RESET" %s\n",
(color)? (char*)color: "", keys[i].name);
Expand Down
65 changes: 48 additions & 17 deletions libr/cons/rgb.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* radare - LGPL - Copyright 2013 - pancake */
/* ansi 256 color extension for r_cons */
/* https://en.wikipedia.org/wiki/ANSI_color */

#include <r_cons.h>

Expand Down Expand Up @@ -39,36 +40,66 @@ R_API void r_cons_rgb_init (void) {
}

R_API int r_cons_rgb_parse (const char *p, ut8 *r, ut8 *g, ut8 *b, int *is_bg) {
const char *q = 0;
int isbg = 0, bold=127;
//const double k = (256/6);
if (!p) return 0;
if (*p==0x1b) p++;
if (*p!='[') return 0;
if (p[3]==';') {
int R,G,B,n = atoi (p+6);
n-=16;
B = (n&3) * 2.3;
G = ((n>>3)&3) * 2.3;
R = ((n>>6)&3) * 2.3;

if (r) *r = R* 42.2;
if (g) *g = G*42.2;
if (b) *b = B*42.2;
//n = (r*36) + (g*6) + b;
// b = n - (r*36) - (g*6)
// r = (n-b-(g*6))/36
if (is_bg) *is_bg = (atoi (p+1) == 48)?1:0;
switch (p[1]) {
case '1': bold=255; p+=2; break;
case '3': isbg=0; break;
case '4': isbg=1; break;
}
#define SETRGB(x,y,z) if(r)*r=x;if(g)*g=y;if(b)*b=z
if (bold != 255 && strchr (p, ';')) {
if (p[4]=='5') {
/* indexed rgb cube */
int x, y, z;
// TODO :Implement colors.txt
SETRGB (0,0,0);
} else {
/* truecolor */
p += 6;
/* complex rgb */
if (r) *r = atoi (p);
q = strchr (p, ';');
if (!q) return 0;
if (g) *g = atoi (q+1);
q = strchr (q+1, ';');
if (!q) return 0;
if (b) *b = atoi (q+1);
}
return 1;
} else {
/* plain ansi */
if (is_bg) is_bg = isbg;
switch (p[2]) {
case '0': SETRGB (0,0,0); break;
case '1': SETRGB (bold,0,0); break;
case '2': SETRGB (0,bold,0); break;
case '3': SETRGB (bold,bold,0); break;
case '4': SETRGB (0,0,bold); break;
case '5': SETRGB (bold,0,bold); break;
case '6': SETRGB (0,bold,bold); break;
case '7': SETRGB (bold,bold,bold); break;
}
}
// TODO support ansi
return 0;
return 1;
}

R_API char *r_cons_rgb_str (char *outstr, ut8 r, ut8 g, ut8 b, int is_bg) {
int k, fgbg = is_bg? 48: 38;
k = (r == g && g == b)? gs (r): rgb (r, g, b);
//k = rgb (r, g, b);
if (!outstr) outstr = malloc (32);
sprintf (outstr, "\x1b[%d;5;%dm", fgbg, k);

if (r_cons_singleton()->truecolor) // only for xterm
sprintf (outstr, "\x1b[%d;2;%d;%d;%dm", fgbg,
R_DIM (r, 0, 255),
R_DIM (g, 0, 255),
R_DIM (b, 0, 255));
else sprintf (outstr, "\x1b[%d;5;%dm", fgbg, k);
return outstr;
}

Expand Down
64 changes: 29 additions & 35 deletions libr/core/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,6 @@ static int cmd_resize(void *data, const char *input) {
oldsize < core->offset+core->blocksize) {
r_core_block_read (core, 0);
}

return R_TRUE;
}

Expand All @@ -455,15 +454,20 @@ static int cmd_eval(void *data, const char *input) {
case 'c':
switch (input[1]) {
case '?':
r_cons_printf ("Usage: ec[s?] [key][[=| ]fg] [bg]\n");
r_cons_printf (" ec list all color keys\n");
r_cons_printf (" ec* (TODO) same as above, but using r2 commands\n");
r_cons_printf (" ecs show a colorful palette\n");
r_cons_printf (" ecf white load white color scheme from $DATADIR/radare2/cons\n");
r_cons_printf (" ec prompt red change coloro of prompt\n");
r_cons_printf ("Available colors:\n");
r_cons_printf (" rgb:000 24 bit hexadecimal rgb color\n");
r_cons_printf (" red|green|blue|. well known ansi colors\n");
r_cons_printf ("Usage: ec[s?] [key][[=| ]fg] [bg]\n"
" ec list all color keys\n"
" ec* (TODO) same as above, but using r2 commands\n"
" ec: set random palete\n"
" ecs show a colorful palette\n"
" ecf dark|white load white color scheme template\n"
" ec prompt red change coloro of prompt\n"
"Available colors:\n"
" rgb:000 24 bit hexadecimal rgb color\n"
" red|green|blue|. well known ansi colors\n"
"See:\n"
" e scr.rgbcolor = true|false for 256 color cube\n"
" e scr.truecolor = true|false for 256*256*256 colors\n"
" $DATADIR/radare2/cons ~/.config/radare2/cons ./\n");
break;
case 'f':
if (input[2] == ' ') {
Expand All @@ -472,20 +476,20 @@ static int cmd_eval(void *data, const char *input) {
home = r_str_home (path);
snprintf (path, sizeof (path), R2_DATDIR"/radare2/"
R2_VERSION"/cons/%s", input+3);
if (!r_core_cmd_file (core, home)) {
if (!r_core_cmd_file (core, path)) {
eprintf ("Oops. Cannot find cons %s\n", path);
}
}
if (!r_core_cmd_file (core, home))
if (!r_core_cmd_file (core, path))
if (!r_core_cmd_file (core, input+3))
eprintf ("ecf: cannot open colorscheme profile\n");
free (home);
} else {
// TODO: lof stuff
eprintf ("Invalid argument.\n");
eprintf ("Usage: ecf [themename].\n");
}
break;
case 's': r_cons_pal_show (); break;
case '*': r_cons_pal_list (1); break;
case '\0': r_cons_pal_list (0); break;
case ':': r_cons_pal_random (); break;
default:{
char *p = strdup (input+2);
char *q = strchr (p, '=');
Expand All @@ -495,8 +499,8 @@ static int cmd_eval(void *data, const char *input) {
*q++ = 0;
r_cons_pal_set (p, q);
} else {
// get
eprintf ("(%s)(%sCOLOR"Color_RESET")\n", p, r_cons_pal_get (p));
const char *k = r_cons_pal_get (p);
if (k) eprintf ("(%s)(%sCOLOR"Color_RESET")\n", p, k);
}
}
}
Expand All @@ -522,20 +526,12 @@ static int cmd_eval(void *data, const char *input) {
r_core_config_init (core);
eprintf ("BUG: 'e-' command locks the eval hashtable. patches are welcome :)\n");
break;
case 'v':
eprintf ("Invalid command '%s'. Use 'e?'\n", input);
break;
case '*':
r_config_list (core->config, NULL, 1);
break;
case 'v': eprintf ("Invalid command '%s'. Use 'e?'\n", input); break;
case '*': r_config_list (core->config, NULL, 1); break;
case '?':
switch (input[1]) {
case '?':
r_config_list (core->config, input+2, 2);
break;
default:
r_config_list (core->config, input+1, 2);
break;
case '?': r_config_list (core->config, input+2, 2); break;
default: r_config_list (core->config, input+1, 2); break;
case 0:
r_cons_printf (
"Usage: e[?] [var[=value]]\n"
Expand All @@ -559,11 +555,8 @@ static int cmd_eval(void *data, const char *input) {
eprintf ("cannot find key '%s'\n", key);
} else eprintf ("Usage: er [key]\n");
break;
case ' ':
r_config_eval (core->config, input+1);
break;
default:
r_config_eval (core->config, input);
case ' ': r_config_eval (core->config, input+1); break;
default: r_config_eval (core->config, input); break;
}
return 0;
}
Expand Down Expand Up @@ -599,6 +592,7 @@ static int cmd_system(void *data, const char *input) {
free (cmd);
} //else eprintf ("Error setting up system environment\n");
} else {
eprintf ("History saved to "R2_HOMEDIR"/history\n");
r_line_hist_save (R2_HOMEDIR"/history");
}
break;
Expand Down
7 changes: 7 additions & 0 deletions libr/core/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,12 @@ static int config_asmbits_callback(void *user, void *data) {
return ret;
}

static int config_truecolor_callback(void *user, void *data) {
RConfigNode *node = (RConfigNode *) data;
r_cons_singleton()->truecolor = (node->i_value)? 1: 0;
return R_TRUE;
}

static int config_color_callback(void *user, void *data) {
RCore *core = (RCore *) user;
RConfigNode *node = (RConfigNode *) data;
Expand Down Expand Up @@ -761,6 +767,7 @@ r_config_set (cfg, "asm.arch", R_SYS_ARCH);
r_config_set_cb (cfg, "scr.prompt", "true", &config_scrprompt_callback);
r_config_set (cfg, "scr.pipecolor", "false");
r_config_desc (cfg, "scr.pipecolor", "enable colors when using pipes if true");
r_config_set_cb (cfg, "scr.truecolor", "false", &config_truecolor_callback);
r_config_set_cb (cfg, "scr.color",
(core->print->flags&R_PRINT_FLAGS_COLOR)?"true":"false",
&config_color_callback);
Expand Down
5 changes: 4 additions & 1 deletion libr/core/disasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,10 @@ R_API int r_core_print_disasm(RPrint *p, RCore *core, ut64 addr, ut8 *buf, int l
if (show_lines && analop.type == R_ANAL_OP_TYPE_RET) {
if (strchr (line, '>'))
memset (line, ' ', strlen (line));
r_cons_printf (" %s; --\n", line);
if (show_color) {
r_cons_printf (" %s%s"Color_RESET"; --\n", color_flow, line);
} else
r_cons_printf (" %s; --\n", line);
}
free (line);
free (refline);
Expand Down
3 changes: 2 additions & 1 deletion libr/core/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ R_API void r_core_sysenv_help() {
"Usage: !<cmd>\n"
" ! list all historic commands\n"
" !ls execute 'ls' in shell\n"
" !!ls~txt printo utput of 'ls' and grep for 'txt'\n"
" !! save command history to hist file\n"
" !!ls~txt print output of 'ls' and grep for 'txt'\n"
" .!rabin2 -rvi ${FILE} run each output line as a r2 cmd\n"
" !echo $SIZE display file size\n"
"Environment:\n"
Expand Down
Loading

0 comments on commit be46b9d

Please sign in to comment.