6 static int git_use_color_default
= GIT_COLOR_AUTO
;
7 int color_stdout_is_tty
= -1;
10 * The list of available column colors.
12 const char *column_colors_ansi
[] = {
21 GIT_COLOR_BOLD_YELLOW
,
23 GIT_COLOR_BOLD_MAGENTA
,
29 COLOR_BACKGROUND_OFFSET
= 10,
30 COLOR_FOREGROUND_ANSI
= 30,
31 COLOR_FOREGROUND_RGB
= 38,
32 COLOR_FOREGROUND_256
= 38,
33 COLOR_FOREGROUND_BRIGHT_ANSI
= 90,
36 /* Ignore the RESET at the end when giving the size */
37 const int column_colors_ansi_max
= ARRAY_SIZE(column_colors_ansi
) - 1;
39 /* An individual foreground or background color. */
42 COLOR_UNSPECIFIED
= 0,
44 COLOR_ANSI
, /* basic 0-7 ANSI colors + "default" (value = 9) */
48 /* The numeric value for ANSI and 256-color modes */
50 /* 24-bit RGB color values */
51 unsigned char red
, green
, blue
;
55 * "word" is a buffer of length "len"; does it match the NUL-terminated
58 static int match_word(const char *word
, int len
, const char *match
)
60 return !strncasecmp(word
, match
, len
) && !match
[len
];
63 static int get_hex_color(const char *in
, unsigned char *out
)
66 val
= (hexval(in
[0]) << 4) | hexval(in
[1]);
74 * If an ANSI color is recognized in "name", fill "out" and return 0.
75 * Otherwise, leave out unchanged and return -1.
77 static int parse_ansi_color(struct color
*out
, const char *name
, int len
)
79 /* Positions in array must match ANSI color codes */
80 static const char * const color_names
[] = {
81 "black", "red", "green", "yellow",
82 "blue", "magenta", "cyan", "white"
85 int color_offset
= COLOR_FOREGROUND_ANSI
;
87 if (match_word(name
, len
, "default")) {
89 * Restores to the terminal's default color, which may not be
90 * the same as explicitly setting "white" or "black".
92 * ECMA-48 - Control Functions \
93 * for Coded Character Sets, 5th edition (June 1991):
94 * > 39 default display colour (implementation-defined)
95 * > 49 default background colour (implementation-defined)
97 * Although not supported /everywhere/--according to terminfo,
98 * some terminals define "op" (original pair) as a blunt
99 * "set to white on black", or even "send full SGR reset"--
100 * it's standard and well-supported enough that if a user
101 * asks for it in their config this will do the right thing.
103 out
->type
= COLOR_ANSI
;
104 out
->value
= 9 + color_offset
;
108 if (strncasecmp(name
, "bright", 6) == 0) {
109 color_offset
= COLOR_FOREGROUND_BRIGHT_ANSI
;
113 for (i
= 0; i
< ARRAY_SIZE(color_names
); i
++) {
114 if (match_word(name
, len
, color_names
[i
])) {
115 out
->type
= COLOR_ANSI
;
116 out
->value
= i
+ color_offset
;
123 static int parse_color(struct color
*out
, const char *name
, int len
)
128 /* First try the special word "normal"... */
129 if (match_word(name
, len
, "normal")) {
130 out
->type
= COLOR_NORMAL
;
134 /* Try a 24-bit RGB value */
135 if (len
== 7 && name
[0] == '#') {
136 if (!get_hex_color(name
+ 1, &out
->red
) &&
137 !get_hex_color(name
+ 3, &out
->green
) &&
138 !get_hex_color(name
+ 5, &out
->blue
)) {
139 out
->type
= COLOR_RGB
;
144 /* Then pick from our human-readable color names... */
145 if (parse_ansi_color(out
, name
, len
) == 0) {
149 /* And finally try a literal 256-color-mode number */
150 val
= strtol(name
, &end
, 10);
151 if (end
- name
== len
) {
153 * Allow "-1" as an alias for "normal", but other negative
157 ; /* fall through to error */
159 out
->type
= COLOR_NORMAL
;
161 /* Rewrite 0-7 as more-portable standard colors. */
162 } else if (val
< 8) {
163 out
->type
= COLOR_ANSI
;
164 out
->value
= val
+ COLOR_FOREGROUND_ANSI
;
166 /* Rewrite 8-15 as more-portable aixterm colors. */
167 } else if (val
< 16) {
168 out
->type
= COLOR_ANSI
;
169 out
->value
= val
- 8 + COLOR_FOREGROUND_BRIGHT_ANSI
;
171 } else if (val
< 256) {
172 out
->type
= COLOR_256
;
181 static int parse_attr(const char *name
, size_t len
)
183 static const struct {
188 #define ATTR(x, val, neg) { (x), sizeof(x)-1, (val), (neg) }
191 ATTR("italic", 3, 23),
193 ATTR("blink", 5, 25),
194 ATTR("reverse", 7, 27),
195 ATTR("strike", 9, 29)
201 if (skip_prefix_mem(name
, len
, "no", &name
, &len
)) {
202 skip_prefix_mem(name
, len
, "-", &name
, &len
);
206 for (i
= 0; i
< ARRAY_SIZE(attrs
); i
++) {
207 if (attrs
[i
].len
== len
&& !memcmp(attrs
[i
].name
, name
, len
))
208 return negate
? attrs
[i
].neg
: attrs
[i
].val
;
213 int color_parse(const char *value
, char *dst
)
215 return color_parse_mem(value
, strlen(value
), dst
);
219 * Write the ANSI color codes for "c" to "out"; the string should
220 * already have the ANSI escape code in it. "out" should have enough
221 * space in it to fit any color.
223 static char *color_output(char *out
, int len
, const struct color
*c
, int background
)
228 offset
= COLOR_BACKGROUND_OFFSET
;
230 case COLOR_UNSPECIFIED
:
234 out
+= xsnprintf(out
, len
, "%d", c
->value
+ offset
);
237 out
+= xsnprintf(out
, len
, "%d;5;%d", COLOR_FOREGROUND_256
+ offset
,
241 out
+= xsnprintf(out
, len
, "%d;2;%d;%d;%d",
242 COLOR_FOREGROUND_RGB
+ offset
,
243 c
->red
, c
->green
, c
->blue
);
249 static int color_empty(const struct color
*c
)
251 return c
->type
<= COLOR_NORMAL
;
254 int color_parse_mem(const char *value
, int value_len
, char *dst
)
256 const char *ptr
= value
;
258 char *end
= dst
+ COLOR_MAXLEN
;
259 unsigned int has_reset
= 0;
260 unsigned int attr
= 0;
261 struct color fg
= { COLOR_UNSPECIFIED
};
262 struct color bg
= { COLOR_UNSPECIFIED
};
264 while (len
> 0 && isspace(*ptr
)) {
274 /* [reset] [fg [bg]] [attr]... */
276 const char *word
= ptr
;
277 struct color c
= { COLOR_UNSPECIFIED
};
278 int val
, wordlen
= 0;
280 while (len
> 0 && !isspace(word
[wordlen
])) {
285 ptr
= word
+ wordlen
;
286 while (len
> 0 && isspace(*ptr
)) {
291 if (match_word(word
, wordlen
, "reset")) {
296 if (!parse_color(&c
, word
, wordlen
)) {
297 if (fg
.type
== COLOR_UNSPECIFIED
) {
301 if (bg
.type
== COLOR_UNSPECIFIED
) {
307 val
= parse_attr(word
, wordlen
);
315 #define OUT(x) do { \
317 BUG("color parsing ran out of space"); \
321 if (has_reset
|| attr
|| !color_empty(&fg
) || !color_empty(&bg
)) {
331 for (i
= 0; attr
; i
++) {
332 unsigned bit
= (1 << i
);
338 dst
+= xsnprintf(dst
, end
- dst
, "%d", i
);
340 if (!color_empty(&fg
)) {
343 dst
= color_output(dst
, end
- dst
, &fg
, 0);
345 if (!color_empty(&bg
)) {
348 dst
= color_output(dst
, end
- dst
, &bg
, 1);
355 return error(_("invalid color value: %.*s"), value_len
, value
);
359 int git_config_colorbool(const char *var
, const char *value
)
362 if (!strcasecmp(value
, "never"))
364 if (!strcasecmp(value
, "always"))
366 if (!strcasecmp(value
, "auto"))
367 return GIT_COLOR_AUTO
;
373 /* Missing or explicit false to turn off colorization */
374 if (!git_config_bool(var
, value
))
377 /* any normal truth value defaults to 'auto' */
378 return GIT_COLOR_AUTO
;
381 static int check_auto_color(int fd
)
383 static int color_stderr_is_tty
= -1;
384 int *is_tty_p
= fd
== 1 ? &color_stdout_is_tty
: &color_stderr_is_tty
;
386 *is_tty_p
= isatty(fd
);
387 if (*is_tty_p
|| (fd
== 1 && pager_in_use() && pager_use_color
)) {
388 if (!is_terminal_dumb())
394 int want_color_fd(int fd
, int var
)
397 * NEEDSWORK: This function is sometimes used from multiple threads, and
398 * we end up using want_auto racily. That "should not matter" since
399 * we always write the same value, but it's still wrong. This function
400 * is listed in .tsan-suppressions for the time being.
403 static int want_auto
[3] = { -1, -1, -1 };
405 if (fd
< 1 || fd
>= ARRAY_SIZE(want_auto
))
406 BUG("file descriptor out of range: %d", fd
);
409 var
= git_use_color_default
;
411 if (var
== GIT_COLOR_AUTO
) {
412 if (want_auto
[fd
] < 0)
413 want_auto
[fd
] = check_auto_color(fd
);
414 return want_auto
[fd
];
419 int git_color_config(const char *var
, const char *value
, void *cb UNUSED
)
421 if (!strcmp(var
, "color.ui")) {
422 git_use_color_default
= git_config_colorbool(var
, value
);
429 int git_color_default_config(const char *var
, const char *value
, void *cb
)
431 if (git_color_config(var
, value
, cb
) < 0)
434 return git_default_config(var
, value
, cb
);
437 void color_print_strbuf(FILE *fp
, const char *color
, const struct strbuf
*sb
)
440 fprintf(fp
, "%s", color
);
441 fprintf(fp
, "%s", sb
->buf
);
443 fprintf(fp
, "%s", GIT_COLOR_RESET
);
446 static int color_vfprintf(FILE *fp
, const char *color
, const char *fmt
,
447 va_list args
, const char *trail
)
452 r
+= fprintf(fp
, "%s", color
);
453 r
+= vfprintf(fp
, fmt
, args
);
455 r
+= fprintf(fp
, "%s", GIT_COLOR_RESET
);
457 r
+= fprintf(fp
, "%s", trail
);
461 int color_fprintf(FILE *fp
, const char *color
, const char *fmt
, ...)
466 r
= color_vfprintf(fp
, color
, fmt
, args
, NULL
);
471 int color_fprintf_ln(FILE *fp
, const char *color
, const char *fmt
, ...)
476 r
= color_vfprintf(fp
, color
, fmt
, args
, "\n");
481 int color_is_nil(const char *c
)
483 return !strcmp(c
, "NIL");