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 */
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 (strncasecmp(name
, "bright", 6) == 0) {
87 color_offset
= COLOR_FOREGROUND_BRIGHT_ANSI
;
91 for (i
= 0; i
< ARRAY_SIZE(color_names
); i
++) {
92 if (match_word(name
, len
, color_names
[i
])) {
93 out
->type
= COLOR_ANSI
;
94 out
->value
= i
+ color_offset
;
101 static int parse_color(struct color
*out
, const char *name
, int len
)
106 /* First try the special word "normal"... */
107 if (match_word(name
, len
, "normal")) {
108 out
->type
= COLOR_NORMAL
;
112 /* Try a 24-bit RGB value */
113 if (len
== 7 && name
[0] == '#') {
114 if (!get_hex_color(name
+ 1, &out
->red
) &&
115 !get_hex_color(name
+ 3, &out
->green
) &&
116 !get_hex_color(name
+ 5, &out
->blue
)) {
117 out
->type
= COLOR_RGB
;
122 /* Then pick from our human-readable color names... */
123 if (parse_ansi_color(out
, name
, len
) == 0) {
127 /* And finally try a literal 256-color-mode number */
128 val
= strtol(name
, &end
, 10);
129 if (end
- name
== len
) {
131 * Allow "-1" as an alias for "normal", but other negative
135 ; /* fall through to error */
137 out
->type
= COLOR_NORMAL
;
139 /* Rewrite 0-7 as more-portable standard colors. */
140 } else if (val
< 8) {
141 out
->type
= COLOR_ANSI
;
142 out
->value
= val
+ COLOR_FOREGROUND_ANSI
;
144 /* Rewrite 8-15 as more-portable aixterm colors. */
145 } else if (val
< 16) {
146 out
->type
= COLOR_ANSI
;
147 out
->value
= val
- 8 + COLOR_FOREGROUND_BRIGHT_ANSI
;
149 } else if (val
< 256) {
150 out
->type
= COLOR_256
;
159 static int parse_attr(const char *name
, size_t len
)
161 static const struct {
166 #define ATTR(x, val, neg) { (x), sizeof(x)-1, (val), (neg) }
169 ATTR("italic", 3, 23),
171 ATTR("blink", 5, 25),
172 ATTR("reverse", 7, 27),
173 ATTR("strike", 9, 29)
179 if (skip_prefix_mem(name
, len
, "no", &name
, &len
)) {
180 skip_prefix_mem(name
, len
, "-", &name
, &len
);
184 for (i
= 0; i
< ARRAY_SIZE(attrs
); i
++) {
185 if (attrs
[i
].len
== len
&& !memcmp(attrs
[i
].name
, name
, len
))
186 return negate
? attrs
[i
].neg
: attrs
[i
].val
;
191 int color_parse(const char *value
, char *dst
)
193 return color_parse_mem(value
, strlen(value
), dst
);
197 * Write the ANSI color codes for "c" to "out"; the string should
198 * already have the ANSI escape code in it. "out" should have enough
199 * space in it to fit any color.
201 static char *color_output(char *out
, int len
, const struct color
*c
, int background
)
206 offset
= COLOR_BACKGROUND_OFFSET
;
208 case COLOR_UNSPECIFIED
:
212 out
+= xsnprintf(out
, len
, "%d", c
->value
+ offset
);
215 out
+= xsnprintf(out
, len
, "%d;5;%d", COLOR_FOREGROUND_256
+ offset
,
219 out
+= xsnprintf(out
, len
, "%d;2;%d;%d;%d",
220 COLOR_FOREGROUND_RGB
+ offset
,
221 c
->red
, c
->green
, c
->blue
);
227 static int color_empty(const struct color
*c
)
229 return c
->type
<= COLOR_NORMAL
;
232 int color_parse_mem(const char *value
, int value_len
, char *dst
)
234 const char *ptr
= value
;
236 char *end
= dst
+ COLOR_MAXLEN
;
237 unsigned int attr
= 0;
238 struct color fg
= { COLOR_UNSPECIFIED
};
239 struct color bg
= { COLOR_UNSPECIFIED
};
241 while (len
> 0 && isspace(*ptr
)) {
251 if (!strncasecmp(ptr
, "reset", len
)) {
252 xsnprintf(dst
, end
- dst
, GIT_COLOR_RESET
);
256 /* [fg [bg]] [attr]... */
258 const char *word
= ptr
;
259 struct color c
= { COLOR_UNSPECIFIED
};
260 int val
, wordlen
= 0;
262 while (len
> 0 && !isspace(word
[wordlen
])) {
267 ptr
= word
+ wordlen
;
268 while (len
> 0 && isspace(*ptr
)) {
273 if (!parse_color(&c
, word
, wordlen
)) {
274 if (fg
.type
== COLOR_UNSPECIFIED
) {
278 if (bg
.type
== COLOR_UNSPECIFIED
) {
284 val
= parse_attr(word
, wordlen
);
292 #define OUT(x) do { \
294 BUG("color parsing ran out of space"); \
298 if (attr
|| !color_empty(&fg
) || !color_empty(&bg
)) {
305 for (i
= 0; attr
; i
++) {
306 unsigned bit
= (1 << i
);
312 dst
+= xsnprintf(dst
, end
- dst
, "%d", i
);
314 if (!color_empty(&fg
)) {
317 dst
= color_output(dst
, end
- dst
, &fg
, 0);
319 if (!color_empty(&bg
)) {
322 dst
= color_output(dst
, end
- dst
, &bg
, 1);
329 return error(_("invalid color value: %.*s"), value_len
, value
);
333 int git_config_colorbool(const char *var
, const char *value
)
336 if (!strcasecmp(value
, "never"))
338 if (!strcasecmp(value
, "always"))
340 if (!strcasecmp(value
, "auto"))
341 return GIT_COLOR_AUTO
;
347 /* Missing or explicit false to turn off colorization */
348 if (!git_config_bool(var
, value
))
351 /* any normal truth value defaults to 'auto' */
352 return GIT_COLOR_AUTO
;
355 static int check_auto_color(int fd
)
357 static int color_stderr_is_tty
= -1;
358 int *is_tty_p
= fd
== 1 ? &color_stdout_is_tty
: &color_stderr_is_tty
;
360 *is_tty_p
= isatty(fd
);
361 if (*is_tty_p
|| (fd
== 1 && pager_in_use() && pager_use_color
)) {
362 if (!is_terminal_dumb())
368 int want_color_fd(int fd
, int var
)
371 * NEEDSWORK: This function is sometimes used from multiple threads, and
372 * we end up using want_auto racily. That "should not matter" since
373 * we always write the same value, but it's still wrong. This function
374 * is listed in .tsan-suppressions for the time being.
377 static int want_auto
[3] = { -1, -1, -1 };
379 if (fd
< 1 || fd
>= ARRAY_SIZE(want_auto
))
380 BUG("file descriptor out of range: %d", fd
);
383 var
= git_use_color_default
;
385 if (var
== GIT_COLOR_AUTO
) {
386 if (want_auto
[fd
] < 0)
387 want_auto
[fd
] = check_auto_color(fd
);
388 return want_auto
[fd
];
393 int git_color_config(const char *var
, const char *value
, void *cb
)
395 if (!strcmp(var
, "color.ui")) {
396 git_use_color_default
= git_config_colorbool(var
, value
);
403 int git_color_default_config(const char *var
, const char *value
, void *cb
)
405 if (git_color_config(var
, value
, cb
) < 0)
408 return git_default_config(var
, value
, cb
);
411 void color_print_strbuf(FILE *fp
, const char *color
, const struct strbuf
*sb
)
414 fprintf(fp
, "%s", color
);
415 fprintf(fp
, "%s", sb
->buf
);
417 fprintf(fp
, "%s", GIT_COLOR_RESET
);
420 static int color_vfprintf(FILE *fp
, const char *color
, const char *fmt
,
421 va_list args
, const char *trail
)
426 r
+= fprintf(fp
, "%s", color
);
427 r
+= vfprintf(fp
, fmt
, args
);
429 r
+= fprintf(fp
, "%s", GIT_COLOR_RESET
);
431 r
+= fprintf(fp
, "%s", trail
);
435 int color_fprintf(FILE *fp
, const char *color
, const char *fmt
, ...)
440 r
= color_vfprintf(fp
, color
, fmt
, args
, NULL
);
445 int color_fprintf_ln(FILE *fp
, const char *color
, const char *fmt
, ...)
450 r
= color_vfprintf(fp
, color
, fmt
, args
, "\n");
455 int color_is_nil(const char *c
)
457 return !strcmp(c
, "NIL");