1 #define DISABLE_SIGN_COMPARE_WARNINGS
3 #include "git-compat-util.h"
12 static int git_use_color_default
= GIT_COLOR_AUTO
;
13 int color_stdout_is_tty
= -1;
16 * The list of available column colors.
18 const char *column_colors_ansi
[] = {
27 GIT_COLOR_BOLD_YELLOW
,
29 GIT_COLOR_BOLD_MAGENTA
,
35 COLOR_BACKGROUND_OFFSET
= 10,
36 COLOR_FOREGROUND_ANSI
= 30,
37 COLOR_FOREGROUND_RGB
= 38,
38 COLOR_FOREGROUND_256
= 38,
39 COLOR_FOREGROUND_BRIGHT_ANSI
= 90,
42 /* Ignore the RESET at the end when giving the size */
43 const int column_colors_ansi_max
= ARRAY_SIZE(column_colors_ansi
) - 1;
45 /* An individual foreground or background color. */
48 COLOR_UNSPECIFIED
= 0,
50 COLOR_ANSI
, /* basic 0-7 ANSI colors + "default" (value = 9) */
54 /* The numeric value for ANSI and 256-color modes */
56 /* 24-bit RGB color values */
57 unsigned char red
, green
, blue
;
61 * "word" is a buffer of length "len"; does it match the NUL-terminated
64 static int match_word(const char *word
, int len
, const char *match
)
66 return !strncasecmp(word
, match
, len
) && !match
[len
];
69 static int get_hex_color(const char **inp
, int width
, unsigned char *out
)
71 const char *in
= *inp
;
74 assert(width
== 1 || width
== 2);
75 val
= (hexval(in
[0]) << 4) | hexval(in
[width
- 1]);
84 * If an ANSI color is recognized in "name", fill "out" and return 0.
85 * Otherwise, leave out unchanged and return -1.
87 static int parse_ansi_color(struct color
*out
, const char *name
, int len
)
89 /* Positions in array must match ANSI color codes */
90 static const char * const color_names
[] = {
91 "black", "red", "green", "yellow",
92 "blue", "magenta", "cyan", "white"
95 int color_offset
= COLOR_FOREGROUND_ANSI
;
97 if (match_word(name
, len
, "default")) {
99 * Restores to the terminal's default color, which may not be
100 * the same as explicitly setting "white" or "black".
102 * ECMA-48 - Control Functions \
103 * for Coded Character Sets, 5th edition (June 1991):
104 * > 39 default display colour (implementation-defined)
105 * > 49 default background colour (implementation-defined)
107 * Although not supported /everywhere/--according to terminfo,
108 * some terminals define "op" (original pair) as a blunt
109 * "set to white on black", or even "send full SGR reset"--
110 * it's standard and well-supported enough that if a user
111 * asks for it in their config this will do the right thing.
113 out
->type
= COLOR_ANSI
;
114 out
->value
= 9 + color_offset
;
118 if (strncasecmp(name
, "bright", 6) == 0) {
119 color_offset
= COLOR_FOREGROUND_BRIGHT_ANSI
;
123 for (i
= 0; i
< ARRAY_SIZE(color_names
); i
++) {
124 if (match_word(name
, len
, color_names
[i
])) {
125 out
->type
= COLOR_ANSI
;
126 out
->value
= i
+ color_offset
;
133 static int parse_color(struct color
*out
, const char *name
, int len
)
138 /* First try the special word "normal"... */
139 if (match_word(name
, len
, "normal")) {
140 out
->type
= COLOR_NORMAL
;
144 /* Try a 24- or 12-bit RGB value prefixed with '#' */
145 if ((len
== 7 || len
== 4) && name
[0] == '#') {
146 int width_per_color
= (len
== 7) ? 2 : 1;
147 const char *color
= name
+ 1;
149 if (!get_hex_color(&color
, width_per_color
, &out
->red
) &&
150 !get_hex_color(&color
, width_per_color
, &out
->green
) &&
151 !get_hex_color(&color
, width_per_color
, &out
->blue
)) {
152 out
->type
= COLOR_RGB
;
157 /* Then pick from our human-readable color names... */
158 if (parse_ansi_color(out
, name
, len
) == 0) {
162 /* And finally try a literal 256-color-mode number */
163 val
= strtol(name
, &end
, 10);
164 if (end
- name
== len
) {
166 * Allow "-1" as an alias for "normal", but other negative
170 ; /* fall through to error */
172 out
->type
= COLOR_NORMAL
;
174 /* Rewrite 0-7 as more-portable standard colors. */
175 } else if (val
< 8) {
176 out
->type
= COLOR_ANSI
;
177 out
->value
= val
+ COLOR_FOREGROUND_ANSI
;
179 /* Rewrite 8-15 as more-portable aixterm colors. */
180 } else if (val
< 16) {
181 out
->type
= COLOR_ANSI
;
182 out
->value
= val
- 8 + COLOR_FOREGROUND_BRIGHT_ANSI
;
184 } else if (val
< 256) {
185 out
->type
= COLOR_256
;
194 static int parse_attr(const char *name
, size_t len
)
196 static const struct {
201 #define ATTR(x, val, neg) { (x), sizeof(x)-1, (val), (neg) }
204 ATTR("italic", 3, 23),
206 ATTR("blink", 5, 25),
207 ATTR("reverse", 7, 27),
208 ATTR("strike", 9, 29)
214 if (skip_prefix_mem(name
, len
, "no", &name
, &len
)) {
215 skip_prefix_mem(name
, len
, "-", &name
, &len
);
219 for (i
= 0; i
< ARRAY_SIZE(attrs
); i
++) {
220 if (attrs
[i
].len
== len
&& !memcmp(attrs
[i
].name
, name
, len
))
221 return negate
? attrs
[i
].neg
: attrs
[i
].val
;
226 int color_parse(const char *value
, char *dst
)
228 return color_parse_mem(value
, strlen(value
), dst
);
232 * Write the ANSI color codes for "c" to "out"; the string should
233 * already have the ANSI escape code in it. "out" should have enough
234 * space in it to fit any color.
236 static char *color_output(char *out
, int len
, const struct color
*c
, int background
)
241 offset
= COLOR_BACKGROUND_OFFSET
;
243 case COLOR_UNSPECIFIED
:
247 out
+= xsnprintf(out
, len
, "%d", c
->value
+ offset
);
250 out
+= xsnprintf(out
, len
, "%d;5;%d", COLOR_FOREGROUND_256
+ offset
,
254 out
+= xsnprintf(out
, len
, "%d;2;%d;%d;%d",
255 COLOR_FOREGROUND_RGB
+ offset
,
256 c
->red
, c
->green
, c
->blue
);
262 static int color_empty(const struct color
*c
)
264 return c
->type
<= COLOR_NORMAL
;
267 int color_parse_mem(const char *value
, int value_len
, char *dst
)
269 const char *ptr
= value
;
271 char *end
= dst
+ COLOR_MAXLEN
;
272 unsigned int has_reset
= 0;
273 unsigned int attr
= 0;
274 struct color fg
= { COLOR_UNSPECIFIED
};
275 struct color bg
= { COLOR_UNSPECIFIED
};
277 while (len
> 0 && isspace(*ptr
)) {
287 /* [reset] [fg [bg]] [attr]... */
289 const char *word
= ptr
;
290 struct color c
= { COLOR_UNSPECIFIED
};
291 int val
, wordlen
= 0;
293 while (len
> 0 && !isspace(word
[wordlen
])) {
298 ptr
= word
+ wordlen
;
299 while (len
> 0 && isspace(*ptr
)) {
304 if (match_word(word
, wordlen
, "reset")) {
309 if (!parse_color(&c
, word
, wordlen
)) {
310 if (fg
.type
== COLOR_UNSPECIFIED
) {
314 if (bg
.type
== COLOR_UNSPECIFIED
) {
320 val
= parse_attr(word
, wordlen
);
328 #define OUT(x) do { \
330 BUG("color parsing ran out of space"); \
334 if (has_reset
|| attr
|| !color_empty(&fg
) || !color_empty(&bg
)) {
344 for (i
= 0; attr
; i
++) {
345 unsigned bit
= (1 << i
);
351 dst
+= xsnprintf(dst
, end
- dst
, "%d", i
);
353 if (!color_empty(&fg
)) {
356 dst
= color_output(dst
, end
- dst
, &fg
, 0);
358 if (!color_empty(&bg
)) {
361 dst
= color_output(dst
, end
- dst
, &bg
, 1);
368 return error(_("invalid color value: %.*s"), value_len
, value
);
372 int git_config_colorbool(const char *var
, const char *value
)
375 if (!strcasecmp(value
, "never"))
377 if (!strcasecmp(value
, "always"))
379 if (!strcasecmp(value
, "auto"))
380 return GIT_COLOR_AUTO
;
386 /* Missing or explicit false to turn off colorization */
387 if (!git_config_bool(var
, value
))
390 /* any normal truth value defaults to 'auto' */
391 return GIT_COLOR_AUTO
;
394 static int check_auto_color(int fd
)
396 static int color_stderr_is_tty
= -1;
397 int *is_tty_p
= fd
== 1 ? &color_stdout_is_tty
: &color_stderr_is_tty
;
399 *is_tty_p
= isatty(fd
);
400 if (*is_tty_p
|| (fd
== 1 && pager_in_use() && pager_use_color
)) {
401 if (!is_terminal_dumb())
407 int want_color_fd(int fd
, int var
)
410 * NEEDSWORK: This function is sometimes used from multiple threads, and
411 * we end up using want_auto racily. That "should not matter" since
412 * we always write the same value, but it's still wrong. This function
413 * is listed in .tsan-suppressions for the time being.
416 static int want_auto
[3] = { -1, -1, -1 };
418 if (fd
< 1 || fd
>= ARRAY_SIZE(want_auto
))
419 BUG("file descriptor out of range: %d", fd
);
422 var
= git_use_color_default
;
424 if (var
== GIT_COLOR_AUTO
) {
425 if (want_auto
[fd
] < 0)
426 want_auto
[fd
] = check_auto_color(fd
);
427 return want_auto
[fd
];
432 int git_color_config(const char *var
, const char *value
, void *cb UNUSED
)
434 if (!strcmp(var
, "color.ui")) {
435 git_use_color_default
= git_config_colorbool(var
, value
);
442 void color_print_strbuf(FILE *fp
, const char *color
, const struct strbuf
*sb
)
445 fprintf(fp
, "%s", color
);
446 fprintf(fp
, "%s", sb
->buf
);
448 fprintf(fp
, "%s", GIT_COLOR_RESET
);
451 static int color_vfprintf(FILE *fp
, const char *color
, const char *fmt
,
452 va_list args
, const char *trail
)
457 r
+= fprintf(fp
, "%s", color
);
458 r
+= vfprintf(fp
, fmt
, args
);
460 r
+= fprintf(fp
, "%s", GIT_COLOR_RESET
);
462 r
+= fprintf(fp
, "%s", trail
);
466 int color_fprintf(FILE *fp
, const char *color
, const char *fmt
, ...)
471 r
= color_vfprintf(fp
, color
, fmt
, args
, NULL
);
476 int color_fprintf_ln(FILE *fp
, const char *color
, const char *fmt
, ...)
481 r
= color_vfprintf(fp
, color
, fmt
, args
, "\n");
486 int color_is_nil(const char *c
)
488 return !strcmp(c
, "NIL");