5 static int git_use_color_default
= GIT_COLOR_AUTO
;
6 int color_stdout_is_tty
= -1;
9 * The list of available column colors.
11 const char *column_colors_ansi
[] = {
20 GIT_COLOR_BOLD_YELLOW
,
22 GIT_COLOR_BOLD_MAGENTA
,
28 COLOR_BACKGROUND_OFFSET
= 10,
29 COLOR_FOREGROUND_ANSI
= 30,
30 COLOR_FOREGROUND_RGB
= 38,
31 COLOR_FOREGROUND_256
= 38,
32 COLOR_FOREGROUND_BRIGHT_ANSI
= 90,
35 /* Ignore the RESET at the end when giving the size */
36 const int column_colors_ansi_max
= ARRAY_SIZE(column_colors_ansi
) - 1;
38 /* An individual foreground or background color. */
41 COLOR_UNSPECIFIED
= 0,
43 COLOR_ANSI
, /* basic 0-7 ANSI colors + "default" (value = 9) */
47 /* The numeric value for ANSI and 256-color modes */
49 /* 24-bit RGB color values */
50 unsigned char red
, green
, blue
;
54 * "word" is a buffer of length "len"; does it match the NUL-terminated
57 static int match_word(const char *word
, int len
, const char *match
)
59 return !strncasecmp(word
, match
, len
) && !match
[len
];
62 static int get_hex_color(const char *in
, unsigned char *out
)
65 val
= (hexval(in
[0]) << 4) | hexval(in
[1]);
73 * If an ANSI color is recognized in "name", fill "out" and return 0.
74 * Otherwise, leave out unchanged and return -1.
76 static int parse_ansi_color(struct color
*out
, const char *name
, int len
)
78 /* Positions in array must match ANSI color codes */
79 static const char * const color_names
[] = {
80 "black", "red", "green", "yellow",
81 "blue", "magenta", "cyan", "white"
84 int color_offset
= COLOR_FOREGROUND_ANSI
;
86 if (match_word(name
, len
, "default")) {
88 * Restores to the terminal's default color, which may not be
89 * the same as explicitly setting "white" or "black".
91 * ECMA-48 - Control Functions \
92 * for Coded Character Sets, 5th edition (June 1991):
93 * > 39 default display colour (implementation-defined)
94 * > 49 default background colour (implementation-defined)
96 * Although not supported /everywhere/--according to terminfo,
97 * some terminals define "op" (original pair) as a blunt
98 * "set to white on black", or even "send full SGR reset"--
99 * it's standard and well-supported enough that if a user
100 * asks for it in their config this will do the right thing.
102 out
->type
= COLOR_ANSI
;
103 out
->value
= 9 + color_offset
;
107 if (strncasecmp(name
, "bright", 6) == 0) {
108 color_offset
= COLOR_FOREGROUND_BRIGHT_ANSI
;
112 for (i
= 0; i
< ARRAY_SIZE(color_names
); i
++) {
113 if (match_word(name
, len
, color_names
[i
])) {
114 out
->type
= COLOR_ANSI
;
115 out
->value
= i
+ color_offset
;
122 static int parse_color(struct color
*out
, const char *name
, int len
)
127 /* First try the special word "normal"... */
128 if (match_word(name
, len
, "normal")) {
129 out
->type
= COLOR_NORMAL
;
133 /* Try a 24-bit RGB value */
134 if (len
== 7 && name
[0] == '#') {
135 if (!get_hex_color(name
+ 1, &out
->red
) &&
136 !get_hex_color(name
+ 3, &out
->green
) &&
137 !get_hex_color(name
+ 5, &out
->blue
)) {
138 out
->type
= COLOR_RGB
;
143 /* Then pick from our human-readable color names... */
144 if (parse_ansi_color(out
, name
, len
) == 0) {
148 /* And finally try a literal 256-color-mode number */
149 val
= strtol(name
, &end
, 10);
150 if (end
- name
== len
) {
152 * Allow "-1" as an alias for "normal", but other negative
156 ; /* fall through to error */
158 out
->type
= COLOR_NORMAL
;
160 /* Rewrite 0-7 as more-portable standard colors. */
161 } else if (val
< 8) {
162 out
->type
= COLOR_ANSI
;
163 out
->value
= val
+ COLOR_FOREGROUND_ANSI
;
165 /* Rewrite 8-15 as more-portable aixterm colors. */
166 } else if (val
< 16) {
167 out
->type
= COLOR_ANSI
;
168 out
->value
= val
- 8 + COLOR_FOREGROUND_BRIGHT_ANSI
;
170 } else if (val
< 256) {
171 out
->type
= COLOR_256
;
180 static int parse_attr(const char *name
, size_t len
)
182 static const struct {
187 #define ATTR(x, val, neg) { (x), sizeof(x)-1, (val), (neg) }
190 ATTR("italic", 3, 23),
192 ATTR("blink", 5, 25),
193 ATTR("reverse", 7, 27),
194 ATTR("strike", 9, 29)
200 if (skip_prefix_mem(name
, len
, "no", &name
, &len
)) {
201 skip_prefix_mem(name
, len
, "-", &name
, &len
);
205 for (i
= 0; i
< ARRAY_SIZE(attrs
); i
++) {
206 if (attrs
[i
].len
== len
&& !memcmp(attrs
[i
].name
, name
, len
))
207 return negate
? attrs
[i
].neg
: attrs
[i
].val
;
212 int color_parse(const char *value
, char *dst
)
214 return color_parse_mem(value
, strlen(value
), dst
);
218 * Write the ANSI color codes for "c" to "out"; the string should
219 * already have the ANSI escape code in it. "out" should have enough
220 * space in it to fit any color.
222 static char *color_output(char *out
, int len
, const struct color
*c
, int background
)
227 offset
= COLOR_BACKGROUND_OFFSET
;
229 case COLOR_UNSPECIFIED
:
233 out
+= xsnprintf(out
, len
, "%d", c
->value
+ offset
);
236 out
+= xsnprintf(out
, len
, "%d;5;%d", COLOR_FOREGROUND_256
+ offset
,
240 out
+= xsnprintf(out
, len
, "%d;2;%d;%d;%d",
241 COLOR_FOREGROUND_RGB
+ offset
,
242 c
->red
, c
->green
, c
->blue
);
248 static int color_empty(const struct color
*c
)
250 return c
->type
<= COLOR_NORMAL
;
253 int color_parse_mem(const char *value
, int value_len
, char *dst
)
255 const char *ptr
= value
;
257 char *end
= dst
+ COLOR_MAXLEN
;
258 unsigned int has_reset
= 0;
259 unsigned int attr
= 0;
260 struct color fg
= { COLOR_UNSPECIFIED
};
261 struct color bg
= { COLOR_UNSPECIFIED
};
263 while (len
> 0 && isspace(*ptr
)) {
273 /* [reset] [fg [bg]] [attr]... */
275 const char *word
= ptr
;
276 struct color c
= { COLOR_UNSPECIFIED
};
277 int val
, wordlen
= 0;
279 while (len
> 0 && !isspace(word
[wordlen
])) {
284 ptr
= word
+ wordlen
;
285 while (len
> 0 && isspace(*ptr
)) {
290 if (match_word(word
, wordlen
, "reset")) {
295 if (!parse_color(&c
, word
, wordlen
)) {
296 if (fg
.type
== COLOR_UNSPECIFIED
) {
300 if (bg
.type
== COLOR_UNSPECIFIED
) {
306 val
= parse_attr(word
, wordlen
);
314 #define OUT(x) do { \
316 BUG("color parsing ran out of space"); \
320 if (has_reset
|| attr
|| !color_empty(&fg
) || !color_empty(&bg
)) {
330 for (i
= 0; attr
; i
++) {
331 unsigned bit
= (1 << i
);
337 dst
+= xsnprintf(dst
, end
- dst
, "%d", i
);
339 if (!color_empty(&fg
)) {
342 dst
= color_output(dst
, end
- dst
, &fg
, 0);
344 if (!color_empty(&bg
)) {
347 dst
= color_output(dst
, end
- dst
, &bg
, 1);
354 return error(_("invalid color value: %.*s"), value_len
, value
);
358 int git_config_colorbool(const char *var
, const char *value
)
361 if (!strcasecmp(value
, "never"))
363 if (!strcasecmp(value
, "always"))
365 if (!strcasecmp(value
, "auto"))
366 return GIT_COLOR_AUTO
;
372 /* Missing or explicit false to turn off colorization */
373 if (!git_config_bool(var
, value
))
376 /* any normal truth value defaults to 'auto' */
377 return GIT_COLOR_AUTO
;
380 static int check_auto_color(int fd
)
382 static int color_stderr_is_tty
= -1;
383 int *is_tty_p
= fd
== 1 ? &color_stdout_is_tty
: &color_stderr_is_tty
;
385 *is_tty_p
= isatty(fd
);
386 if (*is_tty_p
|| (fd
== 1 && pager_in_use() && pager_use_color
)) {
387 if (!is_terminal_dumb())
393 int want_color_fd(int fd
, int var
)
396 * NEEDSWORK: This function is sometimes used from multiple threads, and
397 * we end up using want_auto racily. That "should not matter" since
398 * we always write the same value, but it's still wrong. This function
399 * is listed in .tsan-suppressions for the time being.
402 static int want_auto
[3] = { -1, -1, -1 };
404 if (fd
< 1 || fd
>= ARRAY_SIZE(want_auto
))
405 BUG("file descriptor out of range: %d", fd
);
408 var
= git_use_color_default
;
410 if (var
== GIT_COLOR_AUTO
) {
411 if (want_auto
[fd
] < 0)
412 want_auto
[fd
] = check_auto_color(fd
);
413 return want_auto
[fd
];
418 int git_color_config(const char *var
, const char *value
, void *UNUSED(cb
))
420 if (!strcmp(var
, "color.ui")) {
421 git_use_color_default
= git_config_colorbool(var
, value
);
428 int git_color_default_config(const char *var
, const char *value
, void *cb
)
430 if (git_color_config(var
, value
, cb
) < 0)
433 return git_default_config(var
, value
, cb
);
436 void color_print_strbuf(FILE *fp
, const char *color
, const struct strbuf
*sb
)
439 fprintf(fp
, "%s", color
);
440 fprintf(fp
, "%s", sb
->buf
);
442 fprintf(fp
, "%s", GIT_COLOR_RESET
);
445 static int color_vfprintf(FILE *fp
, const char *color
, const char *fmt
,
446 va_list args
, const char *trail
)
451 r
+= fprintf(fp
, "%s", color
);
452 r
+= vfprintf(fp
, fmt
, args
);
454 r
+= fprintf(fp
, "%s", GIT_COLOR_RESET
);
456 r
+= fprintf(fp
, "%s", trail
);
460 int color_fprintf(FILE *fp
, const char *color
, const char *fmt
, ...)
465 r
= color_vfprintf(fp
, color
, fmt
, args
, NULL
);
470 int color_fprintf_ln(FILE *fp
, const char *color
, const char *fmt
, ...)
475 r
= color_vfprintf(fp
, color
, fmt
, args
, "\n");
480 int color_is_nil(const char *c
)
482 return !strcmp(c
, "NIL");