Merge branch 'fc/doc-checkout-markup-updates'
[git/gitster.git] / color.c
blob6031998d3ea0bc729b048b908fd5344d2e764329
1 #include "cache.h"
2 #include "config.h"
3 #include "color.h"
4 #include "editor.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "pager.h"
9 static int git_use_color_default = GIT_COLOR_AUTO;
10 int color_stdout_is_tty = -1;
13 * The list of available column colors.
15 const char *column_colors_ansi[] = {
16 GIT_COLOR_RED,
17 GIT_COLOR_GREEN,
18 GIT_COLOR_YELLOW,
19 GIT_COLOR_BLUE,
20 GIT_COLOR_MAGENTA,
21 GIT_COLOR_CYAN,
22 GIT_COLOR_BOLD_RED,
23 GIT_COLOR_BOLD_GREEN,
24 GIT_COLOR_BOLD_YELLOW,
25 GIT_COLOR_BOLD_BLUE,
26 GIT_COLOR_BOLD_MAGENTA,
27 GIT_COLOR_BOLD_CYAN,
28 GIT_COLOR_RESET,
31 enum {
32 COLOR_BACKGROUND_OFFSET = 10,
33 COLOR_FOREGROUND_ANSI = 30,
34 COLOR_FOREGROUND_RGB = 38,
35 COLOR_FOREGROUND_256 = 38,
36 COLOR_FOREGROUND_BRIGHT_ANSI = 90,
39 /* Ignore the RESET at the end when giving the size */
40 const int column_colors_ansi_max = ARRAY_SIZE(column_colors_ansi) - 1;
42 /* An individual foreground or background color. */
43 struct color {
44 enum {
45 COLOR_UNSPECIFIED = 0,
46 COLOR_NORMAL,
47 COLOR_ANSI, /* basic 0-7 ANSI colors + "default" (value = 9) */
48 COLOR_256,
49 COLOR_RGB
50 } type;
51 /* The numeric value for ANSI and 256-color modes */
52 unsigned char value;
53 /* 24-bit RGB color values */
54 unsigned char red, green, blue;
58 * "word" is a buffer of length "len"; does it match the NUL-terminated
59 * "match" exactly?
61 static int match_word(const char *word, int len, const char *match)
63 return !strncasecmp(word, match, len) && !match[len];
66 static int get_hex_color(const char *in, unsigned char *out)
68 unsigned int val;
69 val = (hexval(in[0]) << 4) | hexval(in[1]);
70 if (val & ~0xff)
71 return -1;
72 *out = val;
73 return 0;
77 * If an ANSI color is recognized in "name", fill "out" and return 0.
78 * Otherwise, leave out unchanged and return -1.
80 static int parse_ansi_color(struct color *out, const char *name, int len)
82 /* Positions in array must match ANSI color codes */
83 static const char * const color_names[] = {
84 "black", "red", "green", "yellow",
85 "blue", "magenta", "cyan", "white"
87 int i;
88 int color_offset = COLOR_FOREGROUND_ANSI;
90 if (match_word(name, len, "default")) {
92 * Restores to the terminal's default color, which may not be
93 * the same as explicitly setting "white" or "black".
95 * ECMA-48 - Control Functions \
96 * for Coded Character Sets, 5th edition (June 1991):
97 * > 39 default display colour (implementation-defined)
98 * > 49 default background colour (implementation-defined)
100 * Although not supported /everywhere/--according to terminfo,
101 * some terminals define "op" (original pair) as a blunt
102 * "set to white on black", or even "send full SGR reset"--
103 * it's standard and well-supported enough that if a user
104 * asks for it in their config this will do the right thing.
106 out->type = COLOR_ANSI;
107 out->value = 9 + color_offset;
108 return 0;
111 if (strncasecmp(name, "bright", 6) == 0) {
112 color_offset = COLOR_FOREGROUND_BRIGHT_ANSI;
113 name += 6;
114 len -= 6;
116 for (i = 0; i < ARRAY_SIZE(color_names); i++) {
117 if (match_word(name, len, color_names[i])) {
118 out->type = COLOR_ANSI;
119 out->value = i + color_offset;
120 return 0;
123 return -1;
126 static int parse_color(struct color *out, const char *name, int len)
128 char *end;
129 long val;
131 /* First try the special word "normal"... */
132 if (match_word(name, len, "normal")) {
133 out->type = COLOR_NORMAL;
134 return 0;
137 /* Try a 24-bit RGB value */
138 if (len == 7 && name[0] == '#') {
139 if (!get_hex_color(name + 1, &out->red) &&
140 !get_hex_color(name + 3, &out->green) &&
141 !get_hex_color(name + 5, &out->blue)) {
142 out->type = COLOR_RGB;
143 return 0;
147 /* Then pick from our human-readable color names... */
148 if (parse_ansi_color(out, name, len) == 0) {
149 return 0;
152 /* And finally try a literal 256-color-mode number */
153 val = strtol(name, &end, 10);
154 if (end - name == len) {
156 * Allow "-1" as an alias for "normal", but other negative
157 * numbers are bogus.
159 if (val < -1)
160 ; /* fall through to error */
161 else if (val < 0) {
162 out->type = COLOR_NORMAL;
163 return 0;
164 /* Rewrite 0-7 as more-portable standard colors. */
165 } else if (val < 8) {
166 out->type = COLOR_ANSI;
167 out->value = val + COLOR_FOREGROUND_ANSI;
168 return 0;
169 /* Rewrite 8-15 as more-portable aixterm colors. */
170 } else if (val < 16) {
171 out->type = COLOR_ANSI;
172 out->value = val - 8 + COLOR_FOREGROUND_BRIGHT_ANSI;
173 return 0;
174 } else if (val < 256) {
175 out->type = COLOR_256;
176 out->value = val;
177 return 0;
181 return -1;
184 static int parse_attr(const char *name, size_t len)
186 static const struct {
187 const char *name;
188 size_t len;
189 int val, neg;
190 } attrs[] = {
191 #define ATTR(x, val, neg) { (x), sizeof(x)-1, (val), (neg) }
192 ATTR("bold", 1, 22),
193 ATTR("dim", 2, 22),
194 ATTR("italic", 3, 23),
195 ATTR("ul", 4, 24),
196 ATTR("blink", 5, 25),
197 ATTR("reverse", 7, 27),
198 ATTR("strike", 9, 29)
199 #undef ATTR
201 int negate = 0;
202 int i;
204 if (skip_prefix_mem(name, len, "no", &name, &len)) {
205 skip_prefix_mem(name, len, "-", &name, &len);
206 negate = 1;
209 for (i = 0; i < ARRAY_SIZE(attrs); i++) {
210 if (attrs[i].len == len && !memcmp(attrs[i].name, name, len))
211 return negate ? attrs[i].neg : attrs[i].val;
213 return -1;
216 int color_parse(const char *value, char *dst)
218 return color_parse_mem(value, strlen(value), dst);
222 * Write the ANSI color codes for "c" to "out"; the string should
223 * already have the ANSI escape code in it. "out" should have enough
224 * space in it to fit any color.
226 static char *color_output(char *out, int len, const struct color *c, int background)
228 int offset = 0;
230 if (background)
231 offset = COLOR_BACKGROUND_OFFSET;
232 switch (c->type) {
233 case COLOR_UNSPECIFIED:
234 case COLOR_NORMAL:
235 break;
236 case COLOR_ANSI:
237 out += xsnprintf(out, len, "%d", c->value + offset);
238 break;
239 case COLOR_256:
240 out += xsnprintf(out, len, "%d;5;%d", COLOR_FOREGROUND_256 + offset,
241 c->value);
242 break;
243 case COLOR_RGB:
244 out += xsnprintf(out, len, "%d;2;%d;%d;%d",
245 COLOR_FOREGROUND_RGB + offset,
246 c->red, c->green, c->blue);
247 break;
249 return out;
252 static int color_empty(const struct color *c)
254 return c->type <= COLOR_NORMAL;
257 int color_parse_mem(const char *value, int value_len, char *dst)
259 const char *ptr = value;
260 int len = value_len;
261 char *end = dst + COLOR_MAXLEN;
262 unsigned int has_reset = 0;
263 unsigned int attr = 0;
264 struct color fg = { COLOR_UNSPECIFIED };
265 struct color bg = { COLOR_UNSPECIFIED };
267 while (len > 0 && isspace(*ptr)) {
268 ptr++;
269 len--;
272 if (!len) {
273 dst[0] = '\0';
274 return 0;
277 /* [reset] [fg [bg]] [attr]... */
278 while (len > 0) {
279 const char *word = ptr;
280 struct color c = { COLOR_UNSPECIFIED };
281 int val, wordlen = 0;
283 while (len > 0 && !isspace(word[wordlen])) {
284 wordlen++;
285 len--;
288 ptr = word + wordlen;
289 while (len > 0 && isspace(*ptr)) {
290 ptr++;
291 len--;
294 if (match_word(word, wordlen, "reset")) {
295 has_reset = 1;
296 continue;
299 if (!parse_color(&c, word, wordlen)) {
300 if (fg.type == COLOR_UNSPECIFIED) {
301 fg = c;
302 continue;
304 if (bg.type == COLOR_UNSPECIFIED) {
305 bg = c;
306 continue;
308 goto bad;
310 val = parse_attr(word, wordlen);
311 if (0 <= val)
312 attr |= (1 << val);
313 else
314 goto bad;
317 #undef OUT
318 #define OUT(x) do { \
319 if (dst == end) \
320 BUG("color parsing ran out of space"); \
321 *dst++ = (x); \
322 } while(0)
324 if (has_reset || attr || !color_empty(&fg) || !color_empty(&bg)) {
325 int sep = 0;
326 int i;
328 OUT('\033');
329 OUT('[');
331 if (has_reset)
332 sep++;
334 for (i = 0; attr; i++) {
335 unsigned bit = (1 << i);
336 if (!(attr & bit))
337 continue;
338 attr &= ~bit;
339 if (sep++)
340 OUT(';');
341 dst += xsnprintf(dst, end - dst, "%d", i);
343 if (!color_empty(&fg)) {
344 if (sep++)
345 OUT(';');
346 dst = color_output(dst, end - dst, &fg, 0);
348 if (!color_empty(&bg)) {
349 if (sep++)
350 OUT(';');
351 dst = color_output(dst, end - dst, &bg, 1);
353 OUT('m');
355 OUT(0);
356 return 0;
357 bad:
358 return error(_("invalid color value: %.*s"), value_len, value);
359 #undef OUT
362 int git_config_colorbool(const char *var, const char *value)
364 if (value) {
365 if (!strcasecmp(value, "never"))
366 return 0;
367 if (!strcasecmp(value, "always"))
368 return 1;
369 if (!strcasecmp(value, "auto"))
370 return GIT_COLOR_AUTO;
373 if (!var)
374 return -1;
376 /* Missing or explicit false to turn off colorization */
377 if (!git_config_bool(var, value))
378 return 0;
380 /* any normal truth value defaults to 'auto' */
381 return GIT_COLOR_AUTO;
384 static int check_auto_color(int fd)
386 static int color_stderr_is_tty = -1;
387 int *is_tty_p = fd == 1 ? &color_stdout_is_tty : &color_stderr_is_tty;
388 if (*is_tty_p < 0)
389 *is_tty_p = isatty(fd);
390 if (*is_tty_p || (fd == 1 && pager_in_use() && pager_use_color)) {
391 if (!is_terminal_dumb())
392 return 1;
394 return 0;
397 int want_color_fd(int fd, int var)
400 * NEEDSWORK: This function is sometimes used from multiple threads, and
401 * we end up using want_auto racily. That "should not matter" since
402 * we always write the same value, but it's still wrong. This function
403 * is listed in .tsan-suppressions for the time being.
406 static int want_auto[3] = { -1, -1, -1 };
408 if (fd < 1 || fd >= ARRAY_SIZE(want_auto))
409 BUG("file descriptor out of range: %d", fd);
411 if (var < 0)
412 var = git_use_color_default;
414 if (var == GIT_COLOR_AUTO) {
415 if (want_auto[fd] < 0)
416 want_auto[fd] = check_auto_color(fd);
417 return want_auto[fd];
419 return var;
422 int git_color_config(const char *var, const char *value, void *cb UNUSED)
424 if (!strcmp(var, "color.ui")) {
425 git_use_color_default = git_config_colorbool(var, value);
426 return 0;
429 return 0;
432 int git_color_default_config(const char *var, const char *value, void *cb)
434 if (git_color_config(var, value, cb) < 0)
435 return -1;
437 return git_default_config(var, value, cb);
440 void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
442 if (*color)
443 fprintf(fp, "%s", color);
444 fprintf(fp, "%s", sb->buf);
445 if (*color)
446 fprintf(fp, "%s", GIT_COLOR_RESET);
449 static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
450 va_list args, const char *trail)
452 int r = 0;
454 if (*color)
455 r += fprintf(fp, "%s", color);
456 r += vfprintf(fp, fmt, args);
457 if (*color)
458 r += fprintf(fp, "%s", GIT_COLOR_RESET);
459 if (trail)
460 r += fprintf(fp, "%s", trail);
461 return r;
464 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
466 va_list args;
467 int r;
468 va_start(args, fmt);
469 r = color_vfprintf(fp, color, fmt, args, NULL);
470 va_end(args);
471 return r;
474 int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
476 va_list args;
477 int r;
478 va_start(args, fmt);
479 r = color_vfprintf(fp, color, fmt, args, "\n");
480 va_end(args);
481 return r;
484 int color_is_nil(const char *c)
486 return !strcmp(c, "NIL");