Skip to content

Commit

Permalink
Added default color paletes for rcons
Browse files Browse the repository at this point in the history
Added 'ecf':load colorschemes
Added VE visual menu to configure colors
r_cons_rgb_parse() needs a rewrite
  • Loading branch information
radare committed Jul 17, 2013
1 parent e81d4e9 commit a1123b6
Show file tree
Hide file tree
Showing 15 changed files with 280 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ include global.mk

DLIBDIR=$(DESTDIR)/$(LIBDIR)
R2BINS=$(shell cd binr ; echo r*2)
DATADIRS=libr/asm/d libr/syscall/d libr/magic/d
DATADIRS=libr/cons/d libr/asm/d libr/syscall/d libr/magic/d
#binr/ragg2/d
STRIP?=strip
#ifneq ($(shell bsdtar -h 2>/dev/null|grep bsdtar),)
Expand Down
19 changes: 17 additions & 2 deletions libr/cons/cons.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@ R_API void r_cons_print_clear() {
//r_cons_memcat ("\x1b[2J", 4);
}

R_API void r_cons_fill_line() {
char *p, white[1024];
int cols = I.columns-1;
if (cols<1) return;
if (cols>sizeof (white)) {
p = malloc (cols+1);
} else p = white;
memset (p, ' ', cols);
p[cols] = 0;
r_cons_strcat (p);
if (white != p) free (p);
}

R_API void r_cons_clear_line() {
#if __WINDOWS__
char white[1024];
Expand Down Expand Up @@ -302,7 +315,7 @@ R_API void r_cons_visual_flush() {
}

R_API void r_cons_visual_write (char *buffer) {
const char white[1024];
char white[1024];
int cols = I.columns;
int alen, lines = I.rows;
const char *endptr;
Expand Down Expand Up @@ -401,7 +414,9 @@ R_API void r_cons_memset(char ch, int len) {
}

R_API void r_cons_strcat(const char *str) {
int len = strlen (str);
int len;
if (!str) return;
len = strlen (str);
if (len>0)
r_cons_memcat (str, len);
}
Expand Down
22 changes: 22 additions & 0 deletions libr/cons/d/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
include ../../../config-user.mk
P=${DESTDIR}${DATADIR}/radare2/${VERSION}/cons

all clean:

.PHONY: all clean install install-symlink symstall

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

CWD=$(shell pwd)
symstall install-symlink:
mkdir -p $P
for a in * ; do \
if [ $$a != Makefile ]; then \
ln -fs ${CWD}/$$a $P/$$a ; \
fi ; \
done

uninstall:
rm -rf $P
27 changes: 27 additions & 0 deletions libr/cons/d/white
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
ec label blue
ec math rgb:c60
ec bin rgb:f90
ec other rgb:bbb
ec push rgb:0c0
ec pop rgb:0c0
ec cmp rgb:060
ec nop rgb:000
ec jmp green
ec cjmp green
ec call green
ec input black
ec fline red
ec prompt blue
ec flag blue
ec fname blue
ec comment blue
ec reg blue
ec num magenta
ec b0x7f rgb:555
ec b0xff rgb:666
ec b0x00 rgb:aaa
ec btext rgb:888
ec offset blue
ec flow red
ec fline green
ec prompt red
20 changes: 20 additions & 0 deletions libr/cons/d/xvilka
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
ec fname rgb:0cf
ec label rgb:0f3
ec math rgb:660
ec bin rgb:f90
ec call rgb:f00
ec jmp rgb:03f
ec cjmp rgb:33c
ec offset rgb:366
ec comment rgb:0cf
ec push rgb:0c0
ec pop rgb:0c0
ec cmp rgb:060
ec nop rgb:000
ec b0x00 rgb:444
ec b0x7f rgb:555
ec b0xff rgb:666
ec btext rgb:777
ec other rgb:bbb
ec num rgb:f03
ec reg rgb:6f0
46 changes: 41 additions & 5 deletions libr/cons/pal.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ R_API void r_cons_pal_init(const char *foo) {
cons->pal.b0x00 = Color_GREEN;
cons->pal.b0x7f = Color_CYAN;
cons->pal.b0xff = Color_RED;
cons->pal.other = Color_YELLOW;
cons->pal.btext = Color_YELLOW;
cons->pal.push = Color_MAGENTA;
cons->pal.pop = Color_BMAGENTA;
Expand Down Expand Up @@ -86,7 +87,7 @@ R_API char *r_cons_pal_parse(const char *str) {
return *out? strdup (out): NULL;
}

struct {
static struct {
const char *name;
int off;
} keys[] = {
Expand All @@ -103,6 +104,8 @@ struct {
{ "b0x00", r_offsetof (RConsPalette, b0x00) },
{ "b0x7f", r_offsetof (RConsPalette, b0x7f) },
{ "b0xff", r_offsetof (RConsPalette, b0xff) },
{ "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) },
Expand Down Expand Up @@ -155,8 +158,8 @@ R_API void r_cons_pal_show () {
}
r_cons_printf ("\n\nRGB:\n");
for (i=n=0; i<=0xf; i+=inc) {
for (k=0; k<=0xf; k+=inc) {
for (j=0; j<=0xf; j+=inc) {
for (k=0; k<=0xf; k+=inc) {
for (j=0; j<=0xf; j+=inc) {
char fg[32], bg[32];
int r = i*16;
int g = j*16;
Expand All @@ -178,14 +181,40 @@ R_API void r_cons_pal_show () {
}
}

R_API void r_cons_pal_list () {
R_API const char *r_cons_pal_get_color(int n) {
RConsPalette *pal = &(r_cons_singleton ()->pal);
ut8 *p = (ut8*)pal;
const char **color = NULL;
int i;
for (i=0; keys[i].name; i++) {
if (i<n) continue;
color = (const char**)(p + keys[i].off);
color = (const char**)*color;
return (const char *)color;
}
return NULL;
}

R_API void r_cons_pal_list (int rad) {
RConsPalette *pal = &(r_cons_singleton ()->pal);
ut8 *p = (ut8*)pal;
char **color;
ut8 r, g, b;
char **color, rgbstr[32];;
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);
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);
} else
r_cons_printf (" %s##"Color_RESET" %s\n",
(color)? (char*)color: "", keys[i].name);
}
Expand All @@ -205,6 +234,13 @@ R_API int r_cons_pal_set (const char *key, const char *val) {
return R_FALSE;
}

R_API const char *r_cons_pal_get_i (int n) {
int i;
for (i=0; i<n && keys[i].name; i++) {}
if (i==n) return keys[n].name;
return NULL;
}

R_API const char *r_cons_pal_get (const char *key) {
int i;
char **p;
Expand Down
31 changes: 28 additions & 3 deletions libr/cons/rgb.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ static inline int cast(double d) {
}

static int gs (int rgb) {
return 232 + (double)rgb/(255/24.16);
return 232 + (double)rgb/(255/24.1);
}

static int rgb(int r, int g, int b) {
const double k = (255/6);
const double k = (256/6);
r = R_DIM (r/k, 0, 6);
g = R_DIM (g/k, 0, 6);
b = R_DIM (b/k, 0, 6);
return 16 + (r*36) + (g*6) + b;

}

static inline void rgbinit(int r, int g, int b) {
Expand All @@ -39,9 +38,35 @@ R_API void r_cons_rgb_init (void) {
rgbinit (r, g, b);
}

R_API int r_cons_rgb_parse (const char *p, ut8 *r, ut8 *g, ut8 *b, int *is_bg) {
//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;
return 1;
}
// TODO support ansi
return 0;
}

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);
return outstr;
Expand Down
59 changes: 42 additions & 17 deletions libr/core/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,19 +453,40 @@ static int cmd_eval(void *data, const char *input) {
r_config_list (core->config, NULL, 0);
break;
case 'c':
if (input[1] == '?') {
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");
} else if (input[1] == 's') {
r_cons_pal_show ();
} else if (input[1] == '\0') {
r_cons_pal_list ();
} else {
break;
case 'f':
if (input[2] == ' ') {
char *home, path[512];
snprintf (path, sizeof (path), ".config/radare2/cons/%s", input+3);
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);
}
}
free (home);
} else {
// TODO: lof stuff
eprintf ("Invalid argument.\n");
}
break;
case 's': r_cons_pal_show (); break;
case '*': r_cons_pal_list (1); break;
case '\0': r_cons_pal_list (0); break;
default:{
char *p = strdup (input+2);
char *q = strchr (p, '=');
if (!q) q = strchr (p, ' ');
Expand All @@ -478,6 +499,7 @@ static int cmd_eval(void *data, const char *input) {
eprintf ("(%s)(%sCOLOR"Color_RESET")\n", p, r_cons_pal_get (p));
}
}
}
break;
case 'e':
if (input[1]==' ') {
Expand Down Expand Up @@ -564,17 +586,20 @@ static int cmd_system(void *data, const char *input) {
ut64 n;
int ret = 0;
switch (*input) {
case '!': {
int olen;
char *out = NULL;
char *cmd = r_core_sysenv_begin (core, input);
if (cmd) {
ret = r_sys_cmd_str_full (cmd+1, NULL, &out, &olen, NULL);
r_core_sysenv_end (core, input);
r_cons_memcat (out, olen);
free (out);
free (cmd);
} //else eprintf ("Error setting up system environment\n");
case '!':
if (input[1]) {
int olen;
char *out = NULL;
char *cmd = r_core_sysenv_begin (core, input);
if (cmd) {
ret = r_sys_cmd_str_full (cmd+1, NULL, &out, &olen, NULL);
r_core_sysenv_end (core, input);
r_cons_memcat (out, olen);
free (out);
free (cmd);
} //else eprintf ("Error setting up system environment\n");
} else {
r_line_hist_save (R2_HOMEDIR"/history");
}
break;
case '\0':
Expand Down
2 changes: 2 additions & 0 deletions libr/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,8 @@ R_API int r_core_prompt(RCore *r, int sync) {
const char *BEGIN = r->cons->pal.prompt;
const char *END = r->cons->pal.reset;

if (!BEGIN) BEGIN = "";
if (!END) END = "";
// hacky fix fo rio
r_core_block_read (r, 0);
if (cmdprompt && *cmdprompt)
Expand Down
7 changes: 6 additions & 1 deletion libr/core/visual.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@ R_API int r_core_visual_cmd(RCore *core, int ch) {
case 'e':
r_core_visual_config (core);
break;
case 'E':
r_core_visual_colors (core);
break;
case 'M':
r_core_visual_mounts (core);
break;
Expand Down Expand Up @@ -845,7 +848,7 @@ R_API void r_core_visual_title (RCore *core, int color) {
break;
case 1: // pd
case 2: // pd+dbg
r_core_block_size (core, core->cons->rows *5); // this is hacky
r_core_block_size (core, core->cons->rows * 5); // this is hacky
break;
}

Expand Down Expand Up @@ -893,6 +896,8 @@ static void r_core_visual_refresh (RCore *core) {
cons = r_cons_singleton ();
cons->blankline = R_TRUE;

/* hack to blank last line. move prompt here? */
r_cons_fill_line ();
if (autoblocksize) {
r_cons_gotoxy (0, 0);
r_cons_flush ();
Expand Down
Loading

0 comments on commit a1123b6

Please sign in to comment.