Initial import of r_cons_rgb API (ansi 256 color)

This commit is contained in:
pancake 2013-04-01 04:42:14 +02:00
parent d5f3ddaaf0
commit 99040c9ad7
8 changed files with 138 additions and 7 deletions

View File

@ -13,6 +13,9 @@ Broken stuff to fixe before release
0.9.4
=====
* scr.cols -> hex.cols
* scr.width
* use __unused if available
* rafind2 : add support for unicode/widestring search
* e dbg.hwbp para evitar q use hwbps
* .dr- # documented... but not working

View File

@ -1,7 +1,7 @@
include ../config.mk
NAME=r_cons
OBJS=cons.o pipe.o output.o grep.o input.o hud.o
OBJS=cons.o pipe.o output.o grep.o input.o hud.o rgb.o
OBJS+=line.o
DEPS=r_util

View File

@ -208,6 +208,10 @@ R_API void r_cons_clear00() {
r_cons_gotoxy (0, 0);
}
R_API void r_cons_reset_colors() {
r_cons_strcat (Color_RESET);
}
R_API void r_cons_clear() {
r_cons_strcat (Color_RESET"\x1b[2J");
r_cons_gotoxy (0, 0);

61
libr/cons/rgb.c Normal file
View File

@ -0,0 +1,61 @@
/* radare - LGPL - Copyright 2013 - pancake */
/* ansi 256 color extension for r_cons */
#include <r_cons.h>
// TODO: move to r_num_round
static inline int cast(double d) {
return (int)d + ((((int)((d - (int)d)*10))>5)? 1: 0);
}
static int gs (int rgb) {
return 232 + (double)rgb/10.6;
}
static int rgb(int r, int g, int b) {
const double k = (255/6);
r = R_DIM (r/k, 0, 5);
g = R_DIM (g/k, 0, 5);
b = R_DIM (b/k, 0, 5);
return 16 + r*36 + g*6 +b;
}
static inline void rgbinit(int r, int g, int b) {
#if __UNIX__
r_cons_printf ("\x1b]4;%d;rgb:%2.2x/%2.2x/%2.2x\x1b\\",
16 + (r* 36) + (g* 6) + b,
(r ? (r * 40 + 55) : 0),
(g ? (g* 40 + 55) : 0),
(b ? (b* 40 + 55) : 0));
#endif
}
R_API void r_cons_rgb_init (void) {
int r, g, b;
for (r = 0; r < 6; r++)
for (g = 0; g < 6; g++)
for (b = 0; b < 6; b++)
rgbinit (r, g, b);
}
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);
if (!outstr) outstr = malloc (32);
sprintf (outstr, "\x1b[%d;5;%dm", fgbg, k);
return outstr;
}
R_API void r_cons_rgb (ut8 r, ut8 g, ut8 b, int is_bg) {
#if __WINDOWS__
#warning r_cons_rgb not yet supported on windows
#else
char outstr[64];
r_cons_strcat (r_cons_rgb_str (outstr, r, g, b, is_bg));
#endif
}
R_API void r_cons_rgb_fgbg (ut8 r, ut8 g, ut8 b, ut8 R, ut8 G, ut8 B) {
r_cons_rgb (r, g, b, 0);
r_cons_rgb (R, G, B, 1);
}

30
libr/cons/test-rgb.c Normal file
View File

@ -0,0 +1,30 @@
#include <r_cons.h>
int main() {
int i,j ,k;
//char *str = "\x1b[38;5;231mpop\x1b[0m";
//char *str ="\x1b]4;%d;rgb:30/20/24pop\x1b[0m";
char *str ="\x1b\\pop\x1b[0m";
i=j=k =0;
r_cons_new ();
// r_cons_rgb_init ();
printf ("3 == %d\n", r_str_ansi_len (str));
for (i=0;i<255;i+=40) {
for (j=0;j<255;j+=40) { for (k=0;k<255;k+=40) {
r_cons_rgb (i, j, k, 0);
r_cons_rgb (i, j, k, 1);
r_cons_printf ("__");
r_cons_reset_colors ();
r_cons_rgb (i, j, k, 0);
// r_cons_rgb (155, 200, 200, 1);
r_cons_printf (" RGB %d %d %d", i, j, k);
r_cons_reset_colors ();
r_cons_newline ();
}
}}
r_cons_flush ();
return 0;
}

View File

@ -189,6 +189,7 @@ R_API int r_cons_w32_print(ut8 *ptr, int empty);
/* control */
R_API void r_cons_reset();
R_API void r_cons_reset_colors();
R_API void r_cons_print_clear();
R_API void r_cons_clear();
R_API void r_cons_clear00();
@ -237,12 +238,16 @@ R_API void r_cons_grep(const char *str);
R_API int r_cons_grep_line(char *buf, int len); // must be static
R_API int r_cons_grepbuf(char *buf, int len);
R_API void r_cons_rgb (ut8 r, ut8 g, ut8 b, int is_bg);
R_API void r_cons_rgb_fgbg (ut8 r, ut8 g, ut8 b, ut8 R, ut8 G, ut8 B);
R_API void r_cons_rgb_init (void);
R_API char *r_cons_rgb_str (char *outstr, ut8 r, ut8 g, ut8 b, int is_bg);
R_API void r_cons_color (int fg, int r, int g, int b);
R_API void r_cons_invert(int set, int color);
R_API int r_cons_yesno(int def, const char *fmt, ...);
R_API void r_cons_set_cup(int enable);
R_API void r_cons_column(int c);
R_API int r_cons_get_column();
R_API int r_cons_get_column (void);
R_API char *r_cons_message(const char *msg);
#endif

View File

@ -59,6 +59,18 @@
#define R_SYS_HOME "HOME"
#endif
#ifndef __packed
#define __packed __attribute__((__packed__))
#endif
#ifndef __unused
#ifdef __GNUC__
#define __unused __attribute__((unused))
#else
#define __unused
#endif
#endif
/* provide a per-module debug-enabled feature */
// TODO NOT USED. DEPREACATE
#if R_DEBUG
@ -87,7 +99,11 @@ typedef void (*PrintfCallback)(const char *str, ...);
#elif R_INLINE
#define R_API inline
#else
#define R_API
#if defined(__GNUC__)
#define R_API __attribute__((visibility("default")))
#else
#define R_API
#endif
#endif
#define BITS2BYTES(x) ((x/8)+((x%8)?1:0))

View File

@ -700,11 +700,23 @@ R_API char *r_str_unscape(char *buf) {
/* ansi helpers */
R_API int r_str_ansi_len(const char *str) {
int i=0, len = 0;
int ch, ch2, i=0, len = 0;
while (str[i]) {
if (str[i]==0x1b && str[i+1]=='[')
for (++i;str[i]&&str[i]!='J'&&str[i]!='m'&&str[i]!='H';i++);
else len++;
ch = str[i];
ch2 = str[i+1];
if (ch == 0x1b) {
if (ch2 == '\\') {
i++;
} else
if (ch2 == ']') {
if (!strncmp (str+2+5, "rgb:", 4))
i += 13 +5;
} else if (ch2 == '[') {
for (++i;
str[i]&&str[i]!='J'&&
str[i]!='m'&&str[i]!='H';i++);
}
} else len++;
i++;
}
return len;