1 #include "git-compat-util.h"
10 static int git_use_color_default
= GIT_COLOR_AUTO
;
11 int color_stdout_is_tty
= -1;
14 * The list of available column colors.
16 const char *column_colors_ansi
[] = {
25 GIT_COLOR_BOLD_YELLOW
,
27 GIT_COLOR_BOLD_MAGENTA
,
33 COLOR_BACKGROUND_OFFSET
= 10,
34 COLOR_FOREGROUND_ANSI
= 30,
35 COLOR_FOREGROUND_RGB
= 38,
36 COLOR_FOREGROUND_256
= 38,
37 COLOR_FOREGROUND_BRIGHT_ANSI
= 90,
40 /* Ignore the RESET at the end when giving the size */
41 const int column_colors_ansi_max
= ARRAY_SIZE(column_colors_ansi
) - 1;
43 /* An individual foreground or background color. */
46 COLOR_UNSPECIFIED
= 0,
48 COLOR_ANSI
, /* basic 0-7 ANSI colors + "default" (value = 9) */
52 /* The numeric value for ANSI and 256-color modes */
54 /* 24-bit RGB color values */
55 unsigned char red
, green
, blue
;
59 * "word" is a buffer of length "len"; does it match the NUL-terminated
62 static int match_word(const char *word
, int len
, const char *match
)
64 return !strncasecmp(word
, match
, len
) && !match
[len
];
67 static int get_hex_color(const char *in
, unsigned char *out
)
70 val
= (hexval(in
[0]) << 4) | hexval(in
[1]);
78 * If an ANSI color is recognized in "name", fill "out" and return 0.
79 * Otherwise, leave out unchanged and return -1.
81 static int parse_ansi_color(struct color
*out
, const char *name
, int len
)
83 /* Positions in array must match ANSI color codes */
84 static const char * const color_names
[] = {
85 "black", "red", "green", "yellow",
86 "blue", "magenta", "cyan", "white"
89 int color_offset
= COLOR_FOREGROUND_ANSI
;
91 if (match_word(name
, len
, "default")) {
93 * Restores to the terminal's default color, which may not be
94 * the same as explicitly setting "white" or "black".
96 * ECMA-48 - Control Functions \
97 * for Coded Character Sets, 5th edition (June 1991):
98 * > 39 default display colour (implementation-defined)
99 * > 49 default background colour (implementation-defined)
101 * Although not supported /everywhere/--according to terminfo,
102 * some terminals define "op" (original pair) as a blunt
103 * "set to white on black", or even "send full SGR reset"--
104 * it's standard and well-supported enough that if a user
105 * asks for it in their config this will do the right thing.
107 out
->type
= COLOR_ANSI
;
108 out
->value
= 9 + color_offset
;
112 if (strncasecmp(name
, "bright", 6) == 0) {
113 color_offset
= COLOR_FOREGROUND_BRIGHT_ANSI
;
117 for (i
= 0; i
< ARRAY_SIZE(color_names
); i
++) {
118 if (match_word(name
, len
, color_names
[i
])) {
119 out
->type
= COLOR_ANSI
;
120 out
->value
= i
+ color_offset
;
127 static int parse_color(struct color
*out
, const char *name
, int len
)
132 /* First try the special word "normal"... */
133 if (match_word(name
, len
, "normal")) {
134 out
->type
= COLOR_NORMAL
;
138 /* Try a 24-bit RGB value */
139 if (len
== 7 && name
[0] == '#') {
140 if (!get_hex_color(name
+ 1, &out
->red
) &&
141 !get_hex_color(name
+ 3, &out
->green
) &&
142 !get_hex_color(name
+ 5, &out
->blue
)) {
143 out
->type
= COLOR_RGB
;
148 /* Then pick from our human-readable color names... */
149 if (parse_ansi_color(out
, name
, len
) == 0) {
153 /* And finally try a literal 256-color-mode number */
154 val
= strtol(name
, &end
, 10);
155 if (end
- name
== len
) {
157 * Allow "-1" as an alias for "normal", but other negative
161 ; /* fall through to error */
163 out
->type
= COLOR_NORMAL
;
165 /* Rewrite 0-7 as more-portable standard colors. */
166 } else if (val
< 8) {
167 out
->type
= COLOR_ANSI
;
168 out
->value
= val
+ COLOR_FOREGROUND_ANSI
;
170 /* Rewrite 8-15 as more-portable aixterm colors. */
171 } else if (val
< 16) {
172 out
->type
= COLOR_ANSI
;
173 out
->value
= val
- 8 + COLOR_FOREGROUND_BRIGHT_ANSI
;
175 } else if (val
< 256) {
176 out
->type
= COLOR_256
;
185 static int parse_attr(const char *name
, size_t len
)
187 static const struct {
192 #define ATTR(x, val, neg) { (x), sizeof(x)-1, (val), (neg) }
195 ATTR("italic", 3, 23),
197 ATTR("blink", 5, 25),
198 ATTR("reverse", 7, 27),
199 ATTR("strike", 9, 29)
205 if (skip_prefix_mem(name
, len
, "no", &name
, &len
)) {
206 skip_prefix_mem(name
, len
, "-", &name
, &len
);
210 for (i
= 0; i
< ARRAY_SIZE(attrs
); i
++) {
211 if (attrs
[i
].len
== len
&& !memcmp(attrs
[i
].name
, name
, len
))
212 return negate
? attrs
[i
].neg
: attrs
[i
].val
;
217 int color_parse(const char *value
, char *dst
)
219 return color_parse_mem(value
, strlen(value
), dst
);
223 * Write the ANSI color codes for "c" to "out"; the string should
224 * already have the ANSI escape code in it. "out" should have enough
225 * space in it to fit any color.
227 static char *color_output(char *out
, int len
, const struct color
*c
, int background
)
232 offset
= COLOR_BACKGROUND_OFFSET
;
234 case COLOR_UNSPECIFIED
:
238 out
+= xsnprintf(out
, len
, "%d", c
->value
+ offset
);
241 out
+= xsnprintf(out
, len
, "%d;5;%d", COLOR_FOREGROUND_256
+ offset
,
245 out
+= xsnprintf(out
, len
, "%d;2;%d;%d;%d",
246 COLOR_FOREGROUND_RGB
+ offset
,
247 c
->red
, c
->green
, c
->blue
);
253 static int color_empty(const struct color
*c
)
255 return c
->type
<= COLOR_NORMAL
;
258 int color_parse_mem(const char *value
, int value_len
, char *dst
)
260 const char *ptr
= value
;
262 char *end
= dst
+ COLOR_MAXLEN
;
263 unsigned int has_reset
= 0;
264 unsigned int attr
= 0;
265 struct color fg
= { COLOR_UNSPECIFIED
};
266 struct color bg
= { COLOR_UNSPECIFIED
};
268 while (len
> 0 && isspace(*ptr
)) {
278 /* [reset] [fg [bg]] [attr]... */
280 const char *word
= ptr
;
281 struct color c
= { COLOR_UNSPECIFIED
};
282 int val
, wordlen
= 0;
284 while (len
> 0 && !isspace(word
[wordlen
])) {
289 ptr
= word
+ wordlen
;
290 while (len
> 0 && isspace(*ptr
)) {
295 if (match_word(word
, wordlen
, "reset")) {
300 if (!parse_color(&c
, word
, wordlen
)) {
301 if (fg
.type
== COLOR_UNSPECIFIED
) {
305 if (bg
.type
== COLOR_UNSPECIFIED
) {
311 val
= parse_attr(word
, wordlen
);
319 #define OUT(x) do { \
321 BUG("color parsing ran out of space"); \
325 if (has_reset
|| attr
|| !color_empty(&fg
) || !color_empty(&bg
)) {
335 for (i
= 0; attr
; i
++) {
336 unsigned bit
= (1 << i
);
342 dst
+= xsnprintf(dst
, end
- dst
, "%d", i
);
344 if (!color_empty(&fg
)) {
347 dst
= color_output(dst
, end
- dst
, &fg
, 0);
349 if (!color_empty(&bg
)) {
352 dst
= color_output(dst
, end
- dst
, &bg
, 1);
359 return error(_("invalid color value: %.*s"), value_len
, value
);
363 int git_config_colorbool(const char *var
, const char *value
)
366 if (!strcasecmp(value
, "never"))
368 if (!strcasecmp(value
, "always"))
370 if (!strcasecmp(value
, "auto"))
371 return GIT_COLOR_AUTO
;
377 /* Missing or explicit false to turn off colorization */
378 if (!git_config_bool(var
, value
))
381 /* any normal truth value defaults to 'auto' */
382 return GIT_COLOR_AUTO
;
385 static int check_auto_color(int fd
)
387 static int color_stderr_is_tty
= -1;
388 int *is_tty_p
= fd
== 1 ? &color_stdout_is_tty
: &color_stderr_is_tty
;
390 *is_tty_p
= isatty(fd
);
391 if (*is_tty_p
|| (fd
== 1 && pager_in_use() && pager_use_color
)) {
392 if (!is_terminal_dumb())
398 int want_color_fd(int fd
, int var
)
401 * NEEDSWORK: This function is sometimes used from multiple threads, and
402 * we end up using want_auto racily. That "should not matter" since
403 * we always write the same value, but it's still wrong. This function
404 * is listed in .tsan-suppressions for the time being.
407 static int want_auto
[3] = { -1, -1, -1 };
409 if (fd
< 1 || fd
>= ARRAY_SIZE(want_auto
))
410 BUG("file descriptor out of range: %d", fd
);
413 var
= git_use_color_default
;
415 if (var
== GIT_COLOR_AUTO
) {
416 if (want_auto
[fd
] < 0)
417 want_auto
[fd
] = check_auto_color(fd
);
418 return want_auto
[fd
];
423 int git_color_config(const char *var
, const char *value
, void *cb UNUSED
)
425 if (!strcmp(var
, "color.ui")) {
426 git_use_color_default
= git_config_colorbool(var
, value
);
433 void color_print_strbuf(FILE *fp
, const char *color
, const struct strbuf
*sb
)
436 fprintf(fp
, "%s", color
);
437 fprintf(fp
, "%s", sb
->buf
);
439 fprintf(fp
, "%s", GIT_COLOR_RESET
);
442 static int color_vfprintf(FILE *fp
, const char *color
, const char *fmt
,
443 va_list args
, const char *trail
)
448 r
+= fprintf(fp
, "%s", color
);
449 r
+= vfprintf(fp
, fmt
, args
);
451 r
+= fprintf(fp
, "%s", GIT_COLOR_RESET
);
453 r
+= fprintf(fp
, "%s", trail
);
457 int color_fprintf(FILE *fp
, const char *color
, const char *fmt
, ...)
462 r
= color_vfprintf(fp
, color
, fmt
, args
, NULL
);
467 int color_fprintf_ln(FILE *fp
, const char *color
, const char *fmt
, ...)
472 r
= color_vfprintf(fp
, color
, fmt
, args
, "\n");
477 int color_is_nil(const char *c
)
479 return !strcmp(c
, "NIL");