Mercurial > hg > index.fcgi > rcg > rcg-1
changeset 9:a367f80b847b
add color and bold options
author | paulo@hit-nxdomain.opendns.com |
---|---|
date | Fri, 07 May 2010 01:02:18 -0700 |
parents | 5ab8d6c1a37c |
children | 4ef47c3bdce6 |
files | rcg.c |
diffstat | 1 files changed, 95 insertions(+), 5 deletions(-) [+] |
line diff
1.1 --- a/rcg.c Thu May 06 23:21:30 2010 -0700 1.2 +++ b/rcg.c Fri May 07 01:02:18 2010 -0700 1.3 @@ -12,11 +12,61 @@ 1.4 1.5 const size_t BUFSIZE = 5000; 1.6 1.7 -const char *CLR_START = "\x1B[31"; 1.8 +const char *_CLR_START = "\x1B["; 1.9 +const char *_CLR_BOLD = "1;"; 1.10 +char *CLR_START; 1.11 const char *CLR_END = "m"; 1.12 const char *CLR_CLEAR = "\x1B[0m"; 1.13 1.14 +char *CLR_CLR; 1.15 + 1.16 +const char *CLR_FG_RED = "31"; 1.17 + 1.18 +typedef struct _colors { 1.19 + char *name; 1.20 + char *fg; 1.21 + char *bg; 1.22 +} Colors; 1.23 + 1.24 +const Colors COLORS[] = { 1.25 + { "black", "30", "40" }, 1.26 + { "red", "31", "41" }, 1.27 + { "green", "32", "42" }, 1.28 + { "brown", "33", "43" }, 1.29 + { "blue", "34", "44" }, 1.30 + { "magenta", "35", "45" }, 1.31 + { "cyan", "36", "46" }, 1.32 + { "white", "37", "47" }, 1.33 + { "default", "39", "49" }, 1.34 + { NULL }, 1.35 +}; 1.36 + 1.37 +char *getFGColor(char *cname) 1.38 +{ 1.39 + Colors *c; 1.40 + for (c = COLORS; c->name; c++) 1.41 + { 1.42 + if (strncmp(cname, c->name, strlen(c->name)) == 0) 1.43 + return c->fg; 1.44 + } 1.45 + return NULL; 1.46 +} 1.47 + 1.48 +char *getBGColor(char *cname) 1.49 +{ 1.50 + Colors *c; 1.51 + for (c = COLORS; c->name; c++) 1.52 + { 1.53 + if (strncmp(cname, c->name, strlen(c->name)) == 0) 1.54 + return c->bg; 1.55 + } 1.56 + return NULL; 1.57 +} 1.58 + 1.59 int colorLine = 0; 1.60 +int embolden = 0; 1.61 +char *g_fg = "red"; 1.62 +char *g_bg = ""; 1.63 1.64 typedef enum _exit_code { 1.65 EXIT_OK, 1.66 @@ -51,15 +101,22 @@ 1.67 static struct option long_options[] = 1.68 { 1.69 { "line", 0, 0, 'l' }, 1.70 + { "bold", 0, 0, 'B' }, 1.71 { 0, 0, 0, 0 }, 1.72 }; 1.73 1.74 int c; 1.75 int l; 1.76 - while ((c = getopt_long(argc, argv, "l", long_options, &l)) >= 0) 1.77 + while ((c = getopt_long(argc, argv, "lBf:b:", long_options, &l)) >= 0) 1.78 { 1.79 if (c == 'l') 1.80 colorLine = 1; 1.81 + else if (c == 'B') 1.82 + embolden = 1; 1.83 + else if (c == 'f') 1.84 + g_fg = strdup(optarg); 1.85 + else if (c == 'b') 1.86 + g_bg = strdup(optarg); 1.87 } 1.88 1.89 if (optind >= argc) 1.90 @@ -84,7 +141,38 @@ 1.91 1.92 regmatch_t *rem = calloc(1, sizeof(regmatch_t)); 1.93 1.94 + if (embolden) 1.95 + { 1.96 + CLR_START = calloc(strlen(_CLR_START) + strlen(_CLR_BOLD) + 1, sizeof(char)); 1.97 + strcpy(CLR_START, _CLR_START); 1.98 + strcat(CLR_START, _CLR_BOLD); 1.99 + } 1.100 + else 1.101 + CLR_START = strdup(_CLR_START); 1.102 + 1.103 + CLR_CLR = getFGColor(g_fg); 1.104 + if (!CLR_CLR) 1.105 + exit(args_error()); 1.106 + if (strlen(g_bg) > 0) 1.107 + { 1.108 + char *fgcolor = getFGColor(g_fg); 1.109 + char *bgcolor = getBGColor(g_bg); 1.110 + if (bgcolor) 1.111 + { 1.112 + size_t l = strlen(fgcolor) + 1 + strlen(bgcolor); 1.113 + CLR_CLR = calloc((l + 1), sizeof(char)); 1.114 + if (!CLR_CLR) 1.115 + exit(realloc_error()); 1.116 + strcpy(CLR_CLR, fgcolor); 1.117 + strcat(CLR_CLR, ";"); 1.118 + strcat(CLR_CLR, bgcolor); 1.119 + } 1.120 + else 1.121 + exit(args_error()); 1.122 + } 1.123 + 1.124 size_t CLR_START_len = strlen(CLR_START); 1.125 + size_t CLR_CLR_len = strlen(CLR_CLR); 1.126 size_t CLR_END_len = strlen(CLR_END); 1.127 size_t CLR_CLEAR_len = strlen(CLR_CLEAR); 1.128 1.129 @@ -108,7 +196,7 @@ 1.130 if (out_len >= (BUFSIZE - buf_pos)) 1.131 out_len = (BUFSIZE - buf_pos) - 1; 1.132 1.133 - out = (char *)realloc(out, out_len*sizeof(char)); 1.134 + out = realloc(out, out_len*sizeof(char)); 1.135 if (!out) 1.136 exit(realloc_error()); 1.137 1.138 @@ -129,8 +217,8 @@ 1.139 1.140 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0) 1.141 { 1.142 - out_len += CLR_START_len + CLR_END_len + CLR_CLEAR_len; 1.143 - out = (char *)realloc(out, out_len*sizeof(char)); 1.144 + out_len += CLR_START_len + CLR_CLR_len + CLR_END_len + CLR_CLEAR_len; 1.145 + out = realloc(out, out_len*sizeof(char)); 1.146 if (!out) 1.147 exit(realloc_error()); 1.148 strncpy(&out[out_pos], &buf[buf_pos], so); 1.149 @@ -138,6 +226,8 @@ 1.150 buf_pos += so; 1.151 strncpy(&out[out_pos], CLR_START, CLR_START_len); 1.152 out_pos += CLR_START_len; 1.153 + strncpy(&out[out_pos], CLR_CLR, CLR_CLR_len); 1.154 + out_pos += CLR_CLR_len; 1.155 strncpy(&out[out_pos], CLR_END, CLR_END_len); 1.156 out_pos += CLR_END_len; 1.157 strncpy(&out[out_pos], &buf[buf_pos], match_len);