@matheusbsbs / matheus-bagliano / commits / 7c92fe0eaa
Fork 0

Move color option parsing out of diff.c and into color.[ch]

The intent is to lib-ify colorizing code so it can be reused. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <junkio@cox.net>

Jeff King committed Sep 8, 2006 at 04:03 UTC 7c92fe0eaa4fb89e27fa3617b9ae52f20b511573
4 files changed +194 -133
Makefile
+2 -1
@@ -251,7 +251,8 @@ LIB_OBJS = \
251 tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
252 fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
253 write_or_die.o trace.o \
254 - alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS)
254 + alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
255 + color.o
256
257 BUILTIN_OBJS = \
258 builtin-add.o \
color.c new
+176
@@ -0,0 +1,176 @@
1 +#include "color.h"
2 +#include "cache.h"
3 +#include "git-compat-util.h"
4 +
5 +#include <stdarg.h>
6 +
7 +#define COLOR_RESET "\033[m"
8 +
9 +static int parse_color(const char *name, int len)
10 +{
11 + static const char * const color_names[] = {
12 + "normal", "black", "red", "green", "yellow",
13 + "blue", "magenta", "cyan", "white"
14 + };
15 + char *end;
16 + int i;
17 + for (i = 0; i < ARRAY_SIZE(color_names); i++) {
18 + const char *str = color_names[i];
19 + if (!strncasecmp(name, str, len) && !str[len])
20 + return i - 1;
21 + }
22 + i = strtol(name, &end, 10);
23 + if (*name && !*end && i >= -1 && i <= 255)
24 + return i;
25 + return -2;
26 +}
27 +
28 +static int parse_attr(const char *name, int len)
29 +{
30 + static const int attr_values[] = { 1, 2, 4, 5, 7 };
31 + static const char * const attr_names[] = {
32 + "bold", "dim", "ul", "blink", "reverse"
33 + };
34 + int i;
35 + for (i = 0; i < ARRAY_SIZE(attr_names); i++) {
36 + const char *str = attr_names[i];
37 + if (!strncasecmp(name, str, len) && !str[len])
38 + return attr_values[i];
39 + }
40 + return -1;
41 +}
42 +
43 +void color_parse(const char *value, const char *var, char *dst)
44 +{
45 + const char *ptr = value;
46 + int attr = -1;
47 + int fg = -2;
48 + int bg = -2;
49 +
50 + if (!strcasecmp(value, "reset")) {
51 + strcpy(dst, "\033[m");
52 + return;
53 + }
54 +
55 + /* [fg [bg]] [attr] */
56 + while (*ptr) {
57 + const char *word = ptr;
58 + int val, len = 0;
59 +
60 + while (word[len] && !isspace(word[len]))
61 + len++;
62 +
63 + ptr = word + len;
64 + while (*ptr && isspace(*ptr))
65 + ptr++;
66 +
67 + val = parse_color(word, len);
68 + if (val >= -1) {
69 + if (fg == -2) {
70 + fg = val;
71 + continue;
72 + }
73 + if (bg == -2) {
74 + bg = val;
75 + continue;
76 + }
77 + goto bad;
78 + }
79 + val = parse_attr(word, len);
80 + if (val < 0 || attr != -1)
81 + goto bad;
82 + attr = val;
83 + }
84 +
85 + if (attr >= 0 || fg >= 0 || bg >= 0) {
86 + int sep = 0;
87 +
88 + *dst++ = '\033';
89 + *dst++ = '[';
90 + if (attr >= 0) {
91 + *dst++ = '0' + attr;
92 + sep++;
93 + }
94 + if (fg >= 0) {
95 + if (sep++)
96 + *dst++ = ';';
97 + if (fg < 8) {
98 + *dst++ = '3';
99 + *dst++ = '0' + fg;
100 + } else {
101 + dst += sprintf(dst, "38;5;%d", fg);
102 + }
103 + }
104 + if (bg >= 0) {
105 + if (sep++)
106 + *dst++ = ';';
107 + if (bg < 8) {
108 + *dst++ = '4';
109 + *dst++ = '0' + bg;
110 + } else {
111 + dst += sprintf(dst, "48;5;%d", bg);
112 + }
113 + }
114 + *dst++ = 'm';
115 + }
116 + *dst = 0;
117 + return;
118 +bad:
119 + die("bad config value '%s' for variable '%s'", value, var);
120 +}
121 +
122 +int git_config_colorbool(const char *var, const char *value)
123 +{
124 + if (!value)
125 + return 1;
126 + if (!strcasecmp(value, "auto")) {
127 + if (isatty(1) || (pager_in_use && pager_use_color)) {
128 + char *term = getenv("TERM");
129 + if (term && strcmp(term, "dumb"))
130 + return 1;
131 + }
132 + return 0;
133 + }
134 + if (!strcasecmp(value, "never"))
135 + return 0;
136 + if (!strcasecmp(value, "always"))
137 + return 1;
138 + return git_config_bool(var, value);
139 +}
140 +
141 +static int color_vprintf(const char *color, const char *fmt,
142 + va_list args, const char *trail)
143 +{
144 + int r = 0;
145 +
146 + if (*color)
147 + r += printf("%s", color);
148 + r += vprintf(fmt, args);
149 + if (*color)
150 + r += printf("%s", COLOR_RESET);
151 + if (trail)
152 + r += printf("%s", trail);
153 + return r;
154 +}
155 +
156 +
157 +
158 +int color_printf(const char *color, const char *fmt, ...)
159 +{
160 + va_list args;
161 + int r;
162 + va_start(args, fmt);
163 + r = color_vprintf(color, fmt, args, NULL);
164 + va_end(args);
165 + return r;
166 +}
167 +
168 +int color_printf_ln(const char *color, const char *fmt, ...)
169 +{
170 + va_list args;
171 + int r;
172 + va_start(args, fmt);
173 + r = color_vprintf(color, fmt, args, "\n");
174 + va_end(args);
175 + return r;
176 +}
color.h new
+12
@@ -0,0 +1,12 @@
1 +#ifndef COLOR_H
2 +#define COLOR_H
3 +
4 +/* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */
5 +#define COLOR_MAXLEN 24
6 +
7 +int git_config_colorbool(const char *var, const char *value);
8 +void color_parse(const char *var, const char *value, char *dst);
9 +int color_printf(const char *color, const char *fmt, ...);
10 +int color_printf_ln(const char *color, const char *fmt, ...);
11 +
12 +#endif /* COLOR_H */
diff.c
+4 -132
@@ -10,6 +10,7 @@
10 #include "diffcore.h"
11 #include "delta.h"
12 #include "xdiff-interface.h"
13 +#include "color.h"
14
15 static int use_size_cache;
16
@@ -17,8 +18,7 @@ static int diff_detect_rename_default;
18 static int diff_rename_limit_default = -1;
19 static int diff_use_color_default;
20
20 -/* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */
21 -static char diff_colors[][24] = {
21 +static char diff_colors[][COLOR_MAXLEN] = {
22 "\033[m", /* reset */
23 "", /* normal */
24 "\033[1m", /* bold */
@@ -45,119 +45,6 @@ static int parse_diff_color_slot(const char *var, int ofs)
45 die("bad config variable '%s'", var);
46 }
47
48 -static int parse_color(const char *name, int len)
49 -{
50 - static const char * const color_names[] = {
51 - "normal", "black", "red", "green", "yellow",
52 - "blue", "magenta", "cyan", "white"
53 - };
54 - char *end;
55 - int i;
56 - for (i = 0; i < ARRAY_SIZE(color_names); i++) {
57 - const char *str = color_names[i];
58 - if (!strncasecmp(name, str, len) && !str[len])
59 - return i - 1;
60 - }
61 - i = strtol(name, &end, 10);
62 - if (*name && !*end && i >= -1 && i <= 255)
63 - return i;
64 - return -2;
65 -}
66 -
67 -static int parse_attr(const char *name, int len)
68 -{
69 - static const int attr_values[] = { 1, 2, 4, 5, 7 };
70 - static const char * const attr_names[] = {
71 - "bold", "dim", "ul", "blink", "reverse"
72 - };
73 - int i;
74 - for (i = 0; i < ARRAY_SIZE(attr_names); i++) {
75 - const char *str = attr_names[i];
76 - if (!strncasecmp(name, str, len) && !str[len])
77 - return attr_values[i];
78 - }
79 - return -1;
80 -}
81 -
82 -static void parse_diff_color_value(const char *value, const char *var, char *dst)
83 -{
84 - const char *ptr = value;
85 - int attr = -1;
86 - int fg = -2;
87 - int bg = -2;
88 -
89 - if (!strcasecmp(value, "reset")) {
90 - strcpy(dst, "\033[m");
91 - return;
92 - }
93 -
94 - /* [fg [bg]] [attr] */
95 - while (*ptr) {
96 - const char *word = ptr;
97 - int val, len = 0;
98 -
99 - while (word[len] && !isspace(word[len]))
100 - len++;
101 -
102 - ptr = word + len;
103 - while (*ptr && isspace(*ptr))
104 - ptr++;
105 -
106 - val = parse_color(word, len);
107 - if (val >= -1) {
108 - if (fg == -2) {
109 - fg = val;
110 - continue;
111 - }
112 - if (bg == -2) {
113 - bg = val;
114 - continue;
115 - }
116 - goto bad;
117 - }
118 - val = parse_attr(word, len);
119 - if (val < 0 || attr != -1)
120 - goto bad;
121 - attr = val;
122 - }
123 -
124 - if (attr >= 0 || fg >= 0 || bg >= 0) {
125 - int sep = 0;
126 -
127 - *dst++ = '\033';
128 - *dst++ = '[';
129 - if (attr >= 0) {
130 - *dst++ = '0' + attr;
131 - sep++;
132 - }
133 - if (fg >= 0) {
134 - if (sep++)
135 - *dst++ = ';';
136 - if (fg < 8) {
137 - *dst++ = '3';
138 - *dst++ = '0' + fg;
139 - } else {
140 - dst += sprintf(dst, "38;5;%d", fg);
141 - }
142 - }
143 - if (bg >= 0) {
144 - if (sep++)
145 - *dst++ = ';';
146 - if (bg < 8) {
147 - *dst++ = '4';
148 - *dst++ = '0' + bg;
149 - } else {
150 - dst += sprintf(dst, "48;5;%d", bg);
151 - }
152 - }
153 - *dst++ = 'm';
154 - }
155 - *dst = 0;
156 - return;
157 -bad:
158 - die("bad config value '%s' for variable '%s'", value, var);
159 -}
160 -
48 /*
49 * These are to give UI layer defaults.
50 * The core-level commands such as git-diff-files should
@@ -171,22 +58,7 @@ int git_diff_ui_config(const char *var, const char *value)
58 return 0;
59 }
60 if (!strcmp(var, "diff.color")) {
174 - if (!value)
175 - diff_use_color_default = 1; /* bool */
176 - else if (!strcasecmp(value, "auto")) {
177 - diff_use_color_default = 0;
178 - if (isatty(1) || (pager_in_use && pager_use_color)) {
179 - char *term = getenv("TERM");
180 - if (term && strcmp(term, "dumb"))
181 - diff_use_color_default = 1;
182 - }
183 - }
184 - else if (!strcasecmp(value, "never"))
185 - diff_use_color_default = 0;
186 - else if (!strcasecmp(value, "always"))
187 - diff_use_color_default = 1;
188 - else
189 - diff_use_color_default = git_config_bool(var, value);
61 + diff_use_color_default = git_config_colorbool(var, value);
62 return 0;
63 }
64 if (!strcmp(var, "diff.renames")) {
@@ -201,7 +73,7 @@ int git_diff_ui_config(const char *var, const char *value)
73 }
74 if (!strncmp(var, "diff.color.", 11)) {
75 int slot = parse_diff_color_slot(var, 11);
204 - parse_diff_color_value(value, var, diff_colors[slot]);
76 + color_parse(value, var, diff_colors[slot]);
77 return 0;
78 }
79 return git_default_config(var, value);