7 static int git_use_color_default
= GIT_COLOR_AUTO
;
8 int color_stdout_is_tty
= -1;
11 * The list of available column colors.
13 const char *column_colors_ansi
[] = {
22 GIT_COLOR_BOLD_YELLOW
,
24 GIT_COLOR_BOLD_MAGENTA
,
30 COLOR_BACKGROUND_OFFSET
= 10,
31 COLOR_FOREGROUND_ANSI
= 30,
32 COLOR_FOREGROUND_RGB
= 38,
33 COLOR_FOREGROUND_256
= 38,
34 COLOR_FOREGROUND_BRIGHT_ANSI
= 90,
37 /* Ignore the RESET at the end when giving the size */
38 const int column_colors_ansi_max
= ARRAY_SIZE(column_colors_ansi
) - 1;
40 /* An individual foreground or background color. */
43 COLOR_UNSPECIFIED
= 0,
45 COLOR_ANSI
, /* basic 0-7 ANSI colors + "default" (value = 9) */
49 /* The numeric value for ANSI and 256-color modes */
51 /* 24-bit RGB color values */
52 unsigned char red
, green
, blue
;
56 * "word" is a buffer of length "len"; does it match the NUL-terminated
59 static int match_word(const char *word
, int len
, const char *match
)
61 return !strncasecmp(word
, match
, len
) && !match
[len
];
64 static int get_hex_color(const char *in
, unsigned char *out
)
67 val
= (hexval(in
[0]) << 4) | hexval(in
[1]);
75 * If an ANSI color is recognized in "name", fill "out" and return 0.
76 * Otherwise, leave out unchanged and return -1.
78 static int parse_ansi_color(struct color
*out
, const char *name
, int len
)
80 /* Positions in array must match ANSI color codes */
81 static const char * const color_names
[] = {
82 "black", "red", "green", "yellow",
83 "blue", "magenta", "cyan", "white"
86 int color_offset
= COLOR_FOREGROUND_ANSI
;
88 if (match_word(name
, len
, "default")) {
90 * Restores to the terminal's default color, which may not be
91 * the same as explicitly setting "white" or "black".
93 * ECMA-48 - Control Functions \
94 * for Coded Character Sets, 5th edition (June 1991):
95 * > 39 default display colour (implementation-defined)
96 * > 49 default background colour (implementation-defined)
98 * Although not supported /everywhere/--according to terminfo,
99 * some terminals define "op" (original pair) as a blunt
100 * "set to white on black", or even "send full SGR reset"--
101 * it's standard and well-supported enough that if a user
102 * asks for it in their config this will do the right thing.
104 out
->type
= COLOR_ANSI
;
105 out
->value
= 9 + color_offset
;
109 if (strncasecmp(name
, "bright", 6) == 0) {
110 color_offset
= COLOR_FOREGROUND_BRIGHT_ANSI
;
114 for (i
= 0; i
< ARRAY_SIZE(color_names
); i
++) {
115 if (match_word(name
, len
, color_names
[i
])) {
116 out
->type
= COLOR_ANSI
;
117 out
->value
= i
+ color_offset
;
124 static int parse_color(struct color
*out
, const char *name
, int len
)
129 /* First try the special word "normal"... */
130 if (match_word(name
, len
, "normal")) {
131 out
->type
= COLOR_NORMAL
;
135 /* Try a 24-bit RGB value */
136 if (len
== 7 && name
[0] == '#') {
137 if (!get_hex_color(name
+ 1, &out
->red
) &&
138 !get_hex_color(name
+ 3, &out
->green
) &&
139 !get_hex_color(name
+ 5, &out
->blue
)) {
140 out
->type
= COLOR_RGB
;
145 /* Then pick from our human-readable color names... */
146 if (parse_ansi_color(out
, name
, len
) == 0) {
150 /* And finally try a literal 256-color-mode number */
151 val
= strtol(name
, &end
, 10);
152 if (end
- name
== len
) {
154 * Allow "-1" as an alias for "normal", but other negative
158 ; /* fall through to error */
160 out
->type
= COLOR_NORMAL
;
162 /* Rewrite 0-7 as more-portable standard colors. */
163 } else if (val
< 8) {
164 out
->type
= COLOR_ANSI
;
165 out
->value
= val
+ COLOR_FOREGROUND_ANSI
;
167 /* Rewrite 8-15 as more-portable aixterm colors. */
168 } else if (val
< 16) {
169 out
->type
= COLOR_ANSI
;
170 out
->value
= val
- 8 + COLOR_FOREGROUND_BRIGHT_ANSI
;
172 } else if (val
< 256) {
173 out
->type
= COLOR_256
;
182 static int parse_attr(const char *name
, size_t len
)
184 static const struct {
189 #define ATTR(x, val, neg) { (x), sizeof(x)-1, (val), (neg) }
192 ATTR("italic", 3, 23),
194 ATTR("blink", 5, 25),
195 ATTR("reverse", 7, 27),
196 ATTR("strike", 9, 29)
202 if (skip_prefix_mem(name
, len
, "no", &name
, &len
)) {
203 skip_prefix_mem(name
, len
, "-", &name
, &len
);
207 for (i
= 0; i
< ARRAY_SIZE(attrs
); i
++) {
208 if (attrs
[i
].len
== len
&& !memcmp(attrs
[i
].name
, name
, len
))
209 return negate
? attrs
[i
].neg
: attrs
[i
].val
;
214 int color_parse(const char *value
, char *dst
)
216 return color_parse_mem(value
, strlen(value
), dst
);
220 * Write the ANSI color codes for "c" to "out"; the string should
221 * already have the ANSI escape code in it. "out" should have enough
222 * space in it to fit any color.
224 static char *color_output(char *out
, int len
, const struct color
*c
, int background
)
229 offset
= COLOR_BACKGROUND_OFFSET
;
231 case COLOR_UNSPECIFIED
:
235 out
+= xsnprintf(out
, len
, "%d", c
->value
+ offset
);
238 out
+= xsnprintf(out
, len
, "%d;5;%d", COLOR_FOREGROUND_256
+ offset
,
242 out
+= xsnprintf(out
, len
, "%d;2;%d;%d;%d",
243 COLOR_FOREGROUND_RGB
+ offset
,
244 c
->red
, c
->green
, c
->blue
);
250 static int color_empty(const struct color
*c
)
252 return c
->type
<= COLOR_NORMAL
;
255 int color_parse_mem(const char *value
, int value_len
, char *dst
)
257 const char *ptr
= value
;
259 char *end
= dst
+ COLOR_MAXLEN
;
260 unsigned int has_reset
= 0;
261 unsigned int attr
= 0;
262 struct color fg
= { COLOR_UNSPECIFIED
};
263 struct color bg
= { COLOR_UNSPECIFIED
};
265 while (len
> 0 && isspace(*ptr
)) {
275 /* [reset] [fg [bg]] [attr]... */
277 const char *word
= ptr
;
278 struct color c
= { COLOR_UNSPECIFIED
};
279 int val
, wordlen
= 0;
281 while (len
> 0 && !isspace(word
[wordlen
])) {
286 ptr
= word
+ wordlen
;
287 while (len
> 0 && isspace(*ptr
)) {
292 if (match_word(word
, wordlen
, "reset")) {
297 if (!parse_color(&c
, word
, wordlen
)) {
298 if (fg
.type
== COLOR_UNSPECIFIED
) {
302 if (bg
.type
== COLOR_UNSPECIFIED
) {
308 val
= parse_attr(word
, wordlen
);
316 #define OUT(x) do { \
318 BUG("color parsing ran out of space"); \
322 if (has_reset
|| attr
|| !color_empty(&fg
) || !color_empty(&bg
)) {
332 for (i
= 0; attr
; i
++) {
333 unsigned bit
= (1 << i
);
339 dst
+= xsnprintf(dst
, end
- dst
, "%d", i
);
341 if (!color_empty(&fg
)) {
344 dst
= color_output(dst
, end
- dst
, &fg
, 0);
346 if (!color_empty(&bg
)) {
349 dst
= color_output(dst
, end
- dst
, &bg
, 1);
356 return error(_("invalid color value: %.*s"), value_len
, value
);
360 int git_config_colorbool(const char *var
, const char *value
)
363 if (!strcasecmp(value
, "never"))
365 if (!strcasecmp(value
, "always"))
367 if (!strcasecmp(value
, "auto"))
368 return GIT_COLOR_AUTO
;
374 /* Missing or explicit false to turn off colorization */
375 if (!git_config_bool(var
, value
))
378 /* any normal truth value defaults to 'auto' */
379 return GIT_COLOR_AUTO
;
382 static int check_auto_color(int fd
)
384 static int color_stderr_is_tty
= -1;
385 int *is_tty_p
= fd
== 1 ? &color_stdout_is_tty
: &color_stderr_is_tty
;
387 *is_tty_p
= isatty(fd
);
388 if (*is_tty_p
|| (fd
== 1 && pager_in_use() && pager_use_color
)) {
389 if (!is_terminal_dumb())
395 int want_color_fd(int fd
, int var
)
398 * NEEDSWORK: This function is sometimes used from multiple threads, and
399 * we end up using want_auto racily. That "should not matter" since
400 * we always write the same value, but it's still wrong. This function
401 * is listed in .tsan-suppressions for the time being.
404 static int want_auto
[3] = { -1, -1, -1 };
406 if (fd
< 1 || fd
>= ARRAY_SIZE(want_auto
))
407 BUG("file descriptor out of range: %d", fd
);
410 var
= git_use_color_default
;
412 if (var
== GIT_COLOR_AUTO
) {
413 if (want_auto
[fd
] < 0)
414 want_auto
[fd
] = check_auto_color(fd
);
415 return want_auto
[fd
];
420 int git_color_config(const char *var
, const char *value
, void *cb UNUSED
)
422 if (!strcmp(var
, "color.ui")) {
423 git_use_color_default
= git_config_colorbool(var
, value
);
430 int git_color_default_config(const char *var
, const char *value
, void *cb
)
432 if (git_color_config(var
, value
, cb
) < 0)
435 return git_default_config(var
, value
, cb
);
438 void color_print_strbuf(FILE *fp
, const char *color
, const struct strbuf
*sb
)
441 fprintf(fp
, "%s", color
);
442 fprintf(fp
, "%s", sb
->buf
);
444 fprintf(fp
, "%s", GIT_COLOR_RESET
);
447 static int color_vfprintf(FILE *fp
, const char *color
, const char *fmt
,
448 va_list args
, const char *trail
)
453 r
+= fprintf(fp
, "%s", color
);
454 r
+= vfprintf(fp
, fmt
, args
);
456 r
+= fprintf(fp
, "%s", GIT_COLOR_RESET
);
458 r
+= fprintf(fp
, "%s", trail
);
462 int color_fprintf(FILE *fp
, const char *color
, const char *fmt
, ...)
467 r
= color_vfprintf(fp
, color
, fmt
, args
, NULL
);
472 int color_fprintf_ln(FILE *fp
, const char *color
, const char *fmt
, ...)
477 r
= color_vfprintf(fp
, color
, fmt
, args
, "\n");
482 int color_is_nil(const char *c
)
484 return !strcmp(c
, "NIL");