]>
Commit | Line | Data |
---|---|---|
7c92fe0e | 1 | #include "cache.h" |
85023577 | 2 | #include "color.h" |
7c92fe0e | 3 | |
4c7f1819 | 4 | static int git_use_color_default = GIT_COLOR_AUTO; |
e269eb79 | 5 | int color_stdout_is_tty = -1; |
6b2f2d98 | 6 | |
7cd52b5b DM |
7 | /* |
8 | * The list of available column colors. | |
9 | */ | |
10 | const char *column_colors_ansi[] = { | |
11 | GIT_COLOR_RED, | |
12 | GIT_COLOR_GREEN, | |
13 | GIT_COLOR_YELLOW, | |
14 | GIT_COLOR_BLUE, | |
15 | GIT_COLOR_MAGENTA, | |
16 | GIT_COLOR_CYAN, | |
17 | GIT_COLOR_BOLD_RED, | |
18 | GIT_COLOR_BOLD_GREEN, | |
19 | GIT_COLOR_BOLD_YELLOW, | |
20 | GIT_COLOR_BOLD_BLUE, | |
21 | GIT_COLOR_BOLD_MAGENTA, | |
22 | GIT_COLOR_BOLD_CYAN, | |
23 | GIT_COLOR_RESET, | |
24 | }; | |
25 | ||
26 | /* Ignore the RESET at the end when giving the size */ | |
27 | const int column_colors_ansi_max = ARRAY_SIZE(column_colors_ansi) - 1; | |
28 | ||
695d95df JK |
29 | /* An individual foreground or background color. */ |
30 | struct color { | |
31 | enum { | |
32 | COLOR_UNSPECIFIED = 0, | |
33 | COLOR_NORMAL, | |
34 | COLOR_ANSI, /* basic 0-7 ANSI colors */ | |
35 | COLOR_256 | |
36 | } type; | |
37 | /* The numeric value for ANSI and 256-color modes */ | |
38 | unsigned char value; | |
39 | }; | |
40 | ||
41 | /* | |
42 | * "word" is a buffer of length "len"; does it match the NUL-terminated | |
43 | * "match" exactly? | |
44 | */ | |
45 | static int match_word(const char *word, int len, const char *match) | |
7c92fe0e | 46 | { |
695d95df JK |
47 | return !strncasecmp(word, match, len) && !match[len]; |
48 | } | |
49 | ||
50 | static int parse_color(struct color *out, const char *name, int len) | |
51 | { | |
52 | /* Positions in array must match ANSI color codes */ | |
7c92fe0e | 53 | static const char * const color_names[] = { |
695d95df | 54 | "black", "red", "green", "yellow", |
7c92fe0e JK |
55 | "blue", "magenta", "cyan", "white" |
56 | }; | |
57 | char *end; | |
58 | int i; | |
695d95df JK |
59 | long val; |
60 | ||
61 | /* First try the special word "normal"... */ | |
62 | if (match_word(name, len, "normal")) { | |
63 | out->type = COLOR_NORMAL; | |
64 | return 0; | |
65 | } | |
66 | ||
67 | /* Then pick from our human-readable color names... */ | |
7c92fe0e | 68 | for (i = 0; i < ARRAY_SIZE(color_names); i++) { |
695d95df JK |
69 | if (match_word(name, len, color_names[i])) { |
70 | out->type = COLOR_ANSI; | |
71 | out->value = i; | |
72 | return 0; | |
73 | } | |
7c92fe0e | 74 | } |
695d95df JK |
75 | |
76 | /* And finally try a literal 256-color-mode number */ | |
77 | val = strtol(name, &end, 10); | |
78 | if (end - name == len) { | |
79 | /* | |
80 | * Allow "-1" as an alias for "normal", but other negative | |
81 | * numbers are bogus. | |
82 | */ | |
83 | if (val < -1) | |
84 | ; /* fall through to error */ | |
85 | else if (val < 0) { | |
86 | out->type = COLOR_NORMAL; | |
87 | return 0; | |
88 | /* Rewrite low numbers as more-portable standard colors. */ | |
89 | } else if (val < 8) { | |
90 | out->type = COLOR_ANSI; | |
91 | out->value = val; | |
92 | } else if (val < 256) { | |
93 | out->type = COLOR_256; | |
94 | out->value = val; | |
95 | return 0; | |
96 | } | |
97 | } | |
98 | ||
99 | return -1; | |
7c92fe0e JK |
100 | } |
101 | ||
102 | static int parse_attr(const char *name, int len) | |
103 | { | |
104 | static const int attr_values[] = { 1, 2, 4, 5, 7 }; | |
105 | static const char * const attr_names[] = { | |
106 | "bold", "dim", "ul", "blink", "reverse" | |
107 | }; | |
108 | int i; | |
109 | for (i = 0; i < ARRAY_SIZE(attr_names); i++) { | |
110 | const char *str = attr_names[i]; | |
111 | if (!strncasecmp(name, str, len) && !str[len]) | |
112 | return attr_values[i]; | |
113 | } | |
114 | return -1; | |
115 | } | |
116 | ||
f6c5a296 | 117 | int color_parse(const char *value, char *dst) |
2c2dc7c8 | 118 | { |
f6c5a296 | 119 | return color_parse_mem(value, strlen(value), dst); |
2c2dc7c8 RS |
120 | } |
121 | ||
695d95df JK |
122 | #define COLOR_FOREGROUND '3' |
123 | #define COLOR_BACKGROUND '4' | |
124 | ||
125 | /* | |
126 | * Write the ANSI color codes for "c" to "out"; the string should | |
127 | * already have the ANSI escape code in it. "out" should have enough | |
128 | * space in it to fit any color. | |
129 | */ | |
130 | static char *color_output(char *out, const struct color *c, char type) | |
131 | { | |
132 | switch (c->type) { | |
133 | case COLOR_UNSPECIFIED: | |
134 | case COLOR_NORMAL: | |
135 | break; | |
136 | case COLOR_ANSI: | |
137 | *out++ = type; | |
138 | *out++ = '0' + c->value; | |
139 | break; | |
140 | case COLOR_256: | |
141 | out += sprintf(out, "%c8;5;%d", type, c->value); | |
142 | break; | |
143 | } | |
144 | return out; | |
145 | } | |
146 | ||
147 | static int color_empty(const struct color *c) | |
148 | { | |
149 | return c->type <= COLOR_NORMAL; | |
150 | } | |
151 | ||
f6c5a296 | 152 | int color_parse_mem(const char *value, int value_len, char *dst) |
7c92fe0e JK |
153 | { |
154 | const char *ptr = value; | |
2c2dc7c8 | 155 | int len = value_len; |
8b124135 | 156 | unsigned int attr = 0; |
695d95df JK |
157 | struct color fg = { COLOR_UNSPECIFIED }; |
158 | struct color bg = { COLOR_UNSPECIFIED }; | |
7c92fe0e | 159 | |
2c2dc7c8 | 160 | if (!strncasecmp(value, "reset", len)) { |
dc6ebd4c | 161 | strcpy(dst, GIT_COLOR_RESET); |
f6c5a296 | 162 | return 0; |
7c92fe0e JK |
163 | } |
164 | ||
8b124135 | 165 | /* [fg [bg]] [attr]... */ |
2c2dc7c8 | 166 | while (len > 0) { |
7c92fe0e | 167 | const char *word = ptr; |
695d95df | 168 | struct color c; |
2c2dc7c8 | 169 | int val, wordlen = 0; |
7c92fe0e | 170 | |
2c2dc7c8 RS |
171 | while (len > 0 && !isspace(word[wordlen])) { |
172 | wordlen++; | |
173 | len--; | |
174 | } | |
7c92fe0e | 175 | |
2c2dc7c8 RS |
176 | ptr = word + wordlen; |
177 | while (len > 0 && isspace(*ptr)) { | |
7c92fe0e | 178 | ptr++; |
2c2dc7c8 RS |
179 | len--; |
180 | } | |
7c92fe0e | 181 | |
695d95df JK |
182 | if (!parse_color(&c, word, wordlen)) { |
183 | if (fg.type == COLOR_UNSPECIFIED) { | |
184 | fg = c; | |
7c92fe0e JK |
185 | continue; |
186 | } | |
695d95df JK |
187 | if (bg.type == COLOR_UNSPECIFIED) { |
188 | bg = c; | |
7c92fe0e JK |
189 | continue; |
190 | } | |
191 | goto bad; | |
192 | } | |
2c2dc7c8 | 193 | val = parse_attr(word, wordlen); |
8b124135 JH |
194 | if (0 <= val) |
195 | attr |= (1 << val); | |
196 | else | |
7c92fe0e | 197 | goto bad; |
7c92fe0e JK |
198 | } |
199 | ||
695d95df | 200 | if (attr || !color_empty(&fg) || !color_empty(&bg)) { |
7c92fe0e | 201 | int sep = 0; |
8b124135 | 202 | int i; |
7c92fe0e JK |
203 | |
204 | *dst++ = '\033'; | |
205 | *dst++ = '['; | |
8b124135 JH |
206 | |
207 | for (i = 0; attr; i++) { | |
208 | unsigned bit = (1 << i); | |
209 | if (!(attr & bit)) | |
210 | continue; | |
211 | attr &= ~bit; | |
212 | if (sep++) | |
213 | *dst++ = ';'; | |
214 | *dst++ = '0' + i; | |
7c92fe0e | 215 | } |
695d95df | 216 | if (!color_empty(&fg)) { |
7c92fe0e JK |
217 | if (sep++) |
218 | *dst++ = ';'; | |
695d95df | 219 | dst = color_output(dst, &fg, COLOR_FOREGROUND); |
7c92fe0e | 220 | } |
695d95df | 221 | if (!color_empty(&bg)) { |
7c92fe0e JK |
222 | if (sep++) |
223 | *dst++ = ';'; | |
695d95df | 224 | dst = color_output(dst, &bg, COLOR_BACKGROUND); |
7c92fe0e JK |
225 | } |
226 | *dst++ = 'm'; | |
227 | } | |
228 | *dst = 0; | |
f6c5a296 | 229 | return 0; |
7c92fe0e | 230 | bad: |
f6c5a296 | 231 | return error(_("invalid color value: %.*s"), value_len, value); |
7c92fe0e JK |
232 | } |
233 | ||
e269eb79 | 234 | int git_config_colorbool(const char *var, const char *value) |
7c92fe0e | 235 | { |
57f2b842 JH |
236 | if (value) { |
237 | if (!strcasecmp(value, "never")) | |
238 | return 0; | |
239 | if (!strcasecmp(value, "always")) | |
240 | return 1; | |
241 | if (!strcasecmp(value, "auto")) | |
daa0c3d9 | 242 | return GIT_COLOR_AUTO; |
7c92fe0e | 243 | } |
57f2b842 | 244 | |
73e9da01 ML |
245 | if (!var) |
246 | return -1; | |
247 | ||
57f2b842 JH |
248 | /* Missing or explicit false to turn off colorization */ |
249 | if (!git_config_bool(var, value)) | |
7c92fe0e | 250 | return 0; |
57f2b842 JH |
251 | |
252 | /* any normal truth value defaults to 'auto' */ | |
daa0c3d9 JK |
253 | return GIT_COLOR_AUTO; |
254 | } | |
255 | ||
256 | static int check_auto_color(void) | |
257 | { | |
e269eb79 JK |
258 | if (color_stdout_is_tty < 0) |
259 | color_stdout_is_tty = isatty(1); | |
260 | if (color_stdout_is_tty || (pager_in_use() && pager_use_color)) { | |
57f2b842 JH |
261 | char *term = getenv("TERM"); |
262 | if (term && strcmp(term, "dumb")) | |
263 | return 1; | |
264 | } | |
265 | return 0; | |
7c92fe0e JK |
266 | } |
267 | ||
daa0c3d9 JK |
268 | int want_color(int var) |
269 | { | |
270 | static int want_auto = -1; | |
271 | ||
c9bfb953 JK |
272 | if (var < 0) |
273 | var = git_use_color_default; | |
274 | ||
daa0c3d9 JK |
275 | if (var == GIT_COLOR_AUTO) { |
276 | if (want_auto < 0) | |
277 | want_auto = check_auto_color(); | |
278 | return want_auto; | |
279 | } | |
c9bfb953 | 280 | return var; |
daa0c3d9 JK |
281 | } |
282 | ||
3e1dd17a | 283 | int git_color_config(const char *var, const char *value, void *cb) |
6b2f2d98 MK |
284 | { |
285 | if (!strcmp(var, "color.ui")) { | |
e269eb79 | 286 | git_use_color_default = git_config_colorbool(var, value); |
6b2f2d98 MK |
287 | return 0; |
288 | } | |
289 | ||
3e1dd17a JK |
290 | return 0; |
291 | } | |
292 | ||
293 | int git_color_default_config(const char *var, const char *value, void *cb) | |
294 | { | |
295 | if (git_color_config(var, value, cb) < 0) | |
296 | return -1; | |
297 | ||
ef90d6d4 | 298 | return git_default_config(var, value, cb); |
6b2f2d98 MK |
299 | } |
300 | ||
becbdae8 JN |
301 | void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb) |
302 | { | |
303 | if (*color) | |
304 | fprintf(fp, "%s", color); | |
305 | fprintf(fp, "%s", sb->buf); | |
306 | if (*color) | |
307 | fprintf(fp, "%s", GIT_COLOR_RESET); | |
308 | } | |
309 | ||
f26a0012 | 310 | static int color_vfprintf(FILE *fp, const char *color, const char *fmt, |
7c92fe0e JK |
311 | va_list args, const char *trail) |
312 | { | |
313 | int r = 0; | |
314 | ||
315 | if (*color) | |
f26a0012 KH |
316 | r += fprintf(fp, "%s", color); |
317 | r += vfprintf(fp, fmt, args); | |
7c92fe0e | 318 | if (*color) |
dc6ebd4c | 319 | r += fprintf(fp, "%s", GIT_COLOR_RESET); |
7c92fe0e | 320 | if (trail) |
f26a0012 | 321 | r += fprintf(fp, "%s", trail); |
7c92fe0e JK |
322 | return r; |
323 | } | |
324 | ||
325 | ||
326 | ||
f26a0012 | 327 | int color_fprintf(FILE *fp, const char *color, const char *fmt, ...) |
7c92fe0e JK |
328 | { |
329 | va_list args; | |
330 | int r; | |
331 | va_start(args, fmt); | |
f26a0012 | 332 | r = color_vfprintf(fp, color, fmt, args, NULL); |
7c92fe0e JK |
333 | va_end(args); |
334 | return r; | |
335 | } | |
336 | ||
f26a0012 | 337 | int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...) |
7c92fe0e JK |
338 | { |
339 | va_list args; | |
340 | int r; | |
341 | va_start(args, fmt); | |
f26a0012 | 342 | r = color_vfprintf(fp, color, fmt, args, "\n"); |
7c92fe0e JK |
343 | va_end(args); |
344 | return r; | |
345 | } | |
148135fc JK |
346 | |
347 | int color_is_nil(const char *c) | |
348 | { | |
349 | return !strcmp(c, "NIL"); | |
350 | } |