blob: 64f52a4f93a21c4abe5e40e8957ec6115ca46c2a [file] [log] [blame]
Jeff King7c92fe02006-09-08 04:03:18 -04001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Junio C Hamano85023572006-12-19 14:34:12 -08003#include "color.h"
Jeff King7c92fe02006-09-08 04:03:18 -04004
Matthieu Moy4c7f1812013-06-10 16:26:09 +02005static int git_use_color_default = GIT_COLOR_AUTO;
Jeff Kinge269eb72011-08-17 22:03:48 -07006int color_stdout_is_tty = -1;
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +01007
Dan McGee7cd52b52011-04-05 00:40:23 -05008/*
9 * The list of available column colors.
10 */
11const char *column_colors_ansi[] = {
12 GIT_COLOR_RED,
13 GIT_COLOR_GREEN,
14 GIT_COLOR_YELLOW,
15 GIT_COLOR_BLUE,
16 GIT_COLOR_MAGENTA,
17 GIT_COLOR_CYAN,
18 GIT_COLOR_BOLD_RED,
19 GIT_COLOR_BOLD_GREEN,
20 GIT_COLOR_BOLD_YELLOW,
21 GIT_COLOR_BOLD_BLUE,
22 GIT_COLOR_BOLD_MAGENTA,
23 GIT_COLOR_BOLD_CYAN,
24 GIT_COLOR_RESET,
25};
26
Eyal Soha4a28eb02020-01-21 08:56:21 -080027enum {
28 COLOR_BACKGROUND_OFFSET = 10,
29 COLOR_FOREGROUND_ANSI = 30,
30 COLOR_FOREGROUND_RGB = 38,
31 COLOR_FOREGROUND_256 = 38,
Eyal Soha1751b092020-01-21 08:56:22 -080032 COLOR_FOREGROUND_BRIGHT_ANSI = 90,
Eyal Soha4a28eb02020-01-21 08:56:21 -080033};
34
Dan McGee7cd52b52011-04-05 00:40:23 -050035/* Ignore the RESET at the end when giving the size */
36const int column_colors_ansi_max = ARRAY_SIZE(column_colors_ansi) - 1;
37
Jeff King695d95d2014-11-20 10:17:05 -050038/* An individual foreground or background color. */
39struct color {
40 enum {
41 COLOR_UNSPECIFIED = 0,
42 COLOR_NORMAL,
43 COLOR_ANSI, /* basic 0-7 ANSI colors */
Jeff King17a4be22014-11-20 10:25:39 -050044 COLOR_256,
45 COLOR_RGB
Jeff King695d95d2014-11-20 10:17:05 -050046 } type;
47 /* The numeric value for ANSI and 256-color modes */
48 unsigned char value;
Jeff King17a4be22014-11-20 10:25:39 -050049 /* 24-bit RGB color values */
50 unsigned char red, green, blue;
Jeff King695d95d2014-11-20 10:17:05 -050051};
52
53/*
54 * "word" is a buffer of length "len"; does it match the NUL-terminated
55 * "match" exactly?
56 */
57static int match_word(const char *word, int len, const char *match)
Jeff King7c92fe02006-09-08 04:03:18 -040058{
Jeff King695d95d2014-11-20 10:17:05 -050059 return !strncasecmp(word, match, len) && !match[len];
60}
61
Jeff King17a4be22014-11-20 10:25:39 -050062static int get_hex_color(const char *in, unsigned char *out)
63{
64 unsigned int val;
65 val = (hexval(in[0]) << 4) | hexval(in[1]);
66 if (val & ~0xff)
67 return -1;
68 *out = val;
69 return 0;
70}
71
Eyal Soha1751b092020-01-21 08:56:22 -080072/*
73 * If an ANSI color is recognized in "name", fill "out" and return 0.
74 * Otherwise, leave out unchanged and return -1.
75 */
76static int parse_ansi_color(struct color *out, const char *name, int len)
Jeff King695d95d2014-11-20 10:17:05 -050077{
78 /* Positions in array must match ANSI color codes */
Jeff King7c92fe02006-09-08 04:03:18 -040079 static const char * const color_names[] = {
Jeff King695d95d2014-11-20 10:17:05 -050080 "black", "red", "green", "yellow",
Jeff King7c92fe02006-09-08 04:03:18 -040081 "blue", "magenta", "cyan", "white"
82 };
Jeff King7c92fe02006-09-08 04:03:18 -040083 int i;
Eyal Soha1751b092020-01-21 08:56:22 -080084 int color_offset = COLOR_FOREGROUND_ANSI;
85
86 if (strncasecmp(name, "bright", 6) == 0) {
87 color_offset = COLOR_FOREGROUND_BRIGHT_ANSI;
88 name += 6;
89 len -= 6;
90 }
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;
95 return 0;
96 }
97 }
98 return -1;
99}
100
101static int parse_color(struct color *out, const char *name, int len)
102{
103 char *end;
Jeff King695d95d2014-11-20 10:17:05 -0500104 long val;
105
106 /* First try the special word "normal"... */
107 if (match_word(name, len, "normal")) {
108 out->type = COLOR_NORMAL;
109 return 0;
Jeff King7c92fe02006-09-08 04:03:18 -0400110 }
Jeff King695d95d2014-11-20 10:17:05 -0500111
Jeff King17a4be22014-11-20 10:25:39 -0500112 /* 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;
118 return 0;
119 }
120 }
121
Jeff King695d95d2014-11-20 10:17:05 -0500122 /* Then pick from our human-readable color names... */
Eyal Soha1751b092020-01-21 08:56:22 -0800123 if (parse_ansi_color(out, name, len) == 0) {
124 return 0;
Jeff King695d95d2014-11-20 10:17:05 -0500125 }
126
127 /* And finally try a literal 256-color-mode number */
128 val = strtol(name, &end, 10);
129 if (end - name == len) {
130 /*
131 * Allow "-1" as an alias for "normal", but other negative
132 * numbers are bogus.
133 */
134 if (val < -1)
135 ; /* fall through to error */
136 else if (val < 0) {
137 out->type = COLOR_NORMAL;
138 return 0;
Eyal Sohac444f032020-01-21 08:56:23 -0800139 /* Rewrite 0-7 as more-portable standard colors. */
Jeff King695d95d2014-11-20 10:17:05 -0500140 } else if (val < 8) {
141 out->type = COLOR_ANSI;
Eyal Soha4a28eb02020-01-21 08:56:21 -0800142 out->value = val + COLOR_FOREGROUND_ANSI;
Jeff King3759d272015-01-20 17:14:48 -0500143 return 0;
Eyal Sohac444f032020-01-21 08:56:23 -0800144 /* 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;
148 return 0;
Jeff King695d95d2014-11-20 10:17:05 -0500149 } else if (val < 256) {
150 out->type = COLOR_256;
151 out->value = val;
152 return 0;
153 }
154 }
155
156 return -1;
Jeff King7c92fe02006-09-08 04:03:18 -0400157}
158
Jeff Kingdf8e4722016-06-23 13:38:13 -0400159static int parse_attr(const char *name, size_t len)
Jeff King7c92fe02006-09-08 04:03:18 -0400160{
Jeff Kingdf8e4722016-06-23 13:38:13 -0400161 static const struct {
162 const char *name;
163 size_t len;
164 int val, neg;
165 } attrs[] = {
166#define ATTR(x, val, neg) { (x), sizeof(x)-1, (val), (neg) }
167 ATTR("bold", 1, 22),
168 ATTR("dim", 2, 22),
Jeff King54590a02016-06-23 13:39:07 -0400169 ATTR("italic", 3, 23),
Jeff Kingdf8e4722016-06-23 13:38:13 -0400170 ATTR("ul", 4, 24),
171 ATTR("blink", 5, 25),
Jeff King9dc35152016-06-23 13:40:16 -0400172 ATTR("reverse", 7, 27),
173 ATTR("strike", 9, 29)
Jeff Kingdf8e4722016-06-23 13:38:13 -0400174#undef ATTR
Jeff King7c92fe02006-09-08 04:03:18 -0400175 };
Jeff Kingdf8e4722016-06-23 13:38:13 -0400176 int negate = 0;
Jeff King7c92fe02006-09-08 04:03:18 -0400177 int i;
Jeff Kingdf8e4722016-06-23 13:38:13 -0400178
Jeff King56210682016-06-23 13:38:44 -0400179 if (skip_prefix_mem(name, len, "no", &name, &len)) {
180 skip_prefix_mem(name, len, "-", &name, &len);
Jeff Kingdf8e4722016-06-23 13:38:13 -0400181 negate = 1;
Jeff King56210682016-06-23 13:38:44 -0400182 }
Jeff Kingdf8e4722016-06-23 13:38:13 -0400183
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;
Jeff King7c92fe02006-09-08 04:03:18 -0400187 }
188 return -1;
189}
190
Jeff Kingf6c5a292014-10-07 15:33:09 -0400191int color_parse(const char *value, char *dst)
Jeff King7c92fe02006-09-08 04:03:18 -0400192{
Jeff Kingf6c5a292014-10-07 15:33:09 -0400193 return color_parse_mem(value, strlen(value), dst);
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500194}
195
Jeff King695d95d2014-11-20 10:17:05 -0500196/*
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.
200 */
Eyal Soha4a28eb02020-01-21 08:56:21 -0800201static char *color_output(char *out, int len, const struct color *c, int background)
Jeff King695d95d2014-11-20 10:17:05 -0500202{
Eyal Soha4a28eb02020-01-21 08:56:21 -0800203 int offset = 0;
204
205 if (background)
206 offset = COLOR_BACKGROUND_OFFSET;
Jeff King695d95d2014-11-20 10:17:05 -0500207 switch (c->type) {
208 case COLOR_UNSPECIFIED:
209 case COLOR_NORMAL:
210 break;
211 case COLOR_ANSI:
Eyal Soha4a28eb02020-01-21 08:56:21 -0800212 out += xsnprintf(out, len, "%d", c->value + offset);
Jeff King695d95d2014-11-20 10:17:05 -0500213 break;
214 case COLOR_256:
Eyal Soha4a28eb02020-01-21 08:56:21 -0800215 out += xsnprintf(out, len, "%d;5;%d", COLOR_FOREGROUND_256 + offset,
216 c->value);
Jeff King695d95d2014-11-20 10:17:05 -0500217 break;
Jeff King17a4be22014-11-20 10:25:39 -0500218 case COLOR_RGB:
Eyal Soha4a28eb02020-01-21 08:56:21 -0800219 out += xsnprintf(out, len, "%d;2;%d;%d;%d",
220 COLOR_FOREGROUND_RGB + offset,
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400221 c->red, c->green, c->blue);
Jeff King17a4be22014-11-20 10:25:39 -0500222 break;
Jeff King695d95d2014-11-20 10:17:05 -0500223 }
224 return out;
225}
226
227static int color_empty(const struct color *c)
228{
229 return c->type <= COLOR_NORMAL;
230}
231
Jeff Kingf6c5a292014-10-07 15:33:09 -0400232int color_parse_mem(const char *value, int value_len, char *dst)
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500233{
Jeff King7c92fe02006-09-08 04:03:18 -0400234 const char *ptr = value;
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500235 int len = value_len;
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400236 char *end = dst + COLOR_MAXLEN;
Junio C Hamano8b124132010-02-27 18:56:38 -0800237 unsigned int attr = 0;
Jeff King695d95d2014-11-20 10:17:05 -0500238 struct color fg = { COLOR_UNSPECIFIED };
239 struct color bg = { COLOR_UNSPECIFIED };
Jeff King7c92fe02006-09-08 04:03:18 -0400240
Nguyễn Thái Ngọc Duybc407562017-01-19 18:41:22 +0700241 while (len > 0 && isspace(*ptr)) {
242 ptr++;
243 len--;
244 }
245
Jeff King55cccf42017-02-01 01:21:29 +0100246 if (!len) {
247 dst[0] = '\0';
248 return 0;
249 }
Nguyễn Thái Ngọc Duyc2f41bf2017-01-19 18:41:21 +0700250
Nguyễn Thái Ngọc Duybc407562017-01-19 18:41:22 +0700251 if (!strncasecmp(ptr, "reset", len)) {
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400252 xsnprintf(dst, end - dst, GIT_COLOR_RESET);
Jeff Kingf6c5a292014-10-07 15:33:09 -0400253 return 0;
Jeff King7c92fe02006-09-08 04:03:18 -0400254 }
255
Junio C Hamano8b124132010-02-27 18:56:38 -0800256 /* [fg [bg]] [attr]... */
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500257 while (len > 0) {
Jeff King7c92fe02006-09-08 04:03:18 -0400258 const char *word = ptr;
Jeff King3e1952e2016-08-30 23:43:07 -0400259 struct color c = { COLOR_UNSPECIFIED };
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500260 int val, wordlen = 0;
Jeff King7c92fe02006-09-08 04:03:18 -0400261
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500262 while (len > 0 && !isspace(word[wordlen])) {
263 wordlen++;
264 len--;
265 }
Jeff King7c92fe02006-09-08 04:03:18 -0400266
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500267 ptr = word + wordlen;
268 while (len > 0 && isspace(*ptr)) {
Jeff King7c92fe02006-09-08 04:03:18 -0400269 ptr++;
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500270 len--;
271 }
Jeff King7c92fe02006-09-08 04:03:18 -0400272
Jeff King695d95d2014-11-20 10:17:05 -0500273 if (!parse_color(&c, word, wordlen)) {
274 if (fg.type == COLOR_UNSPECIFIED) {
275 fg = c;
Jeff King7c92fe02006-09-08 04:03:18 -0400276 continue;
277 }
Jeff King695d95d2014-11-20 10:17:05 -0500278 if (bg.type == COLOR_UNSPECIFIED) {
279 bg = c;
Jeff King7c92fe02006-09-08 04:03:18 -0400280 continue;
281 }
282 goto bad;
283 }
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500284 val = parse_attr(word, wordlen);
Junio C Hamano8b124132010-02-27 18:56:38 -0800285 if (0 <= val)
286 attr |= (1 << val);
287 else
Jeff King7c92fe02006-09-08 04:03:18 -0400288 goto bad;
Jeff King7c92fe02006-09-08 04:03:18 -0400289 }
290
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400291#undef OUT
292#define OUT(x) do { \
293 if (dst == end) \
Johannes Schindelin033abf92018-05-02 11:38:39 +0200294 BUG("color parsing ran out of space"); \
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400295 *dst++ = (x); \
296} while(0)
297
Jeff King695d95d2014-11-20 10:17:05 -0500298 if (attr || !color_empty(&fg) || !color_empty(&bg)) {
Jeff King7c92fe02006-09-08 04:03:18 -0400299 int sep = 0;
Junio C Hamano8b124132010-02-27 18:56:38 -0800300 int i;
Jeff King7c92fe02006-09-08 04:03:18 -0400301
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400302 OUT('\033');
303 OUT('[');
Junio C Hamano8b124132010-02-27 18:56:38 -0800304
305 for (i = 0; attr; i++) {
306 unsigned bit = (1 << i);
307 if (!(attr & bit))
308 continue;
309 attr &= ~bit;
310 if (sep++)
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400311 OUT(';');
312 dst += xsnprintf(dst, end - dst, "%d", i);
Jeff King7c92fe02006-09-08 04:03:18 -0400313 }
Jeff King695d95d2014-11-20 10:17:05 -0500314 if (!color_empty(&fg)) {
Jeff King7c92fe02006-09-08 04:03:18 -0400315 if (sep++)
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400316 OUT(';');
Eyal Soha4a28eb02020-01-21 08:56:21 -0800317 dst = color_output(dst, end - dst, &fg, 0);
Jeff King7c92fe02006-09-08 04:03:18 -0400318 }
Jeff King695d95d2014-11-20 10:17:05 -0500319 if (!color_empty(&bg)) {
Jeff King7c92fe02006-09-08 04:03:18 -0400320 if (sep++)
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400321 OUT(';');
Eyal Soha4a28eb02020-01-21 08:56:21 -0800322 dst = color_output(dst, end - dst, &bg, 1);
Jeff King7c92fe02006-09-08 04:03:18 -0400323 }
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400324 OUT('m');
Jeff King7c92fe02006-09-08 04:03:18 -0400325 }
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400326 OUT(0);
Jeff Kingf6c5a292014-10-07 15:33:09 -0400327 return 0;
Jeff King7c92fe02006-09-08 04:03:18 -0400328bad:
Jeff Kingf6c5a292014-10-07 15:33:09 -0400329 return error(_("invalid color value: %.*s"), value_len, value);
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400330#undef OUT
Jeff King7c92fe02006-09-08 04:03:18 -0400331}
332
Jeff Kinge269eb72011-08-17 22:03:48 -0700333int git_config_colorbool(const char *var, const char *value)
Jeff King7c92fe02006-09-08 04:03:18 -0400334{
Junio C Hamano57f2b842007-11-26 14:30:28 -0800335 if (value) {
336 if (!strcasecmp(value, "never"))
337 return 0;
338 if (!strcasecmp(value, "always"))
Jeff King2c1acdf2017-10-13 13:23:24 -0400339 return 1;
Junio C Hamano57f2b842007-11-26 14:30:28 -0800340 if (!strcasecmp(value, "auto"))
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700341 return GIT_COLOR_AUTO;
Jeff King7c92fe02006-09-08 04:03:18 -0400342 }
Junio C Hamano57f2b842007-11-26 14:30:28 -0800343
Mark Lodato73e9da02010-02-16 23:55:58 -0500344 if (!var)
345 return -1;
346
Junio C Hamano57f2b842007-11-26 14:30:28 -0800347 /* Missing or explicit false to turn off colorization */
348 if (!git_config_bool(var, value))
Jeff King7c92fe02006-09-08 04:03:18 -0400349 return 0;
Junio C Hamano57f2b842007-11-26 14:30:28 -0800350
351 /* any normal truth value defaults to 'auto' */
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700352 return GIT_COLOR_AUTO;
353}
354
Johannes Schindelin295d9492018-04-21 12:09:57 +0200355static int check_auto_color(int fd)
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700356{
Johannes Schindelin295d9492018-04-21 12:09:57 +0200357 static int color_stderr_is_tty = -1;
358 int *is_tty_p = fd == 1 ? &color_stdout_is_tty : &color_stderr_is_tty;
359 if (*is_tty_p < 0)
360 *is_tty_p = isatty(fd);
361 if (*is_tty_p || (fd == 1 && pager_in_use() && pager_use_color)) {
Lars Schneidera64f2132017-11-29 15:37:51 +0100362 if (!is_terminal_dumb())
Junio C Hamano57f2b842007-11-26 14:30:28 -0800363 return 1;
364 }
365 return 0;
Jeff King7c92fe02006-09-08 04:03:18 -0400366}
367
Johannes Schindelin295d9492018-04-21 12:09:57 +0200368int want_color_fd(int fd, int var)
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700369{
Martin Ågren6cdf8a72017-08-21 19:43:48 +0200370 /*
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.
375 */
376
Johannes Schindelin295d9492018-04-21 12:09:57 +0200377 static int want_auto[3] = { -1, -1, -1 };
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700378
Eric Sunshine65bb21e2018-08-02 23:07:49 -0700379 if (fd < 1 || fd >= ARRAY_SIZE(want_auto))
380 BUG("file descriptor out of range: %d", fd);
381
Jeff Kingc9bfb952011-08-17 22:05:35 -0700382 if (var < 0)
383 var = git_use_color_default;
384
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700385 if (var == GIT_COLOR_AUTO) {
Johannes Schindelin295d9492018-04-21 12:09:57 +0200386 if (want_auto[fd] < 0)
387 want_auto[fd] = check_auto_color(fd);
388 return want_auto[fd];
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700389 }
Jeff Kingc9bfb952011-08-17 22:05:35 -0700390 return var;
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700391}
392
Jeff King3e1dd172011-08-17 22:05:08 -0700393int git_color_config(const char *var, const char *value, void *cb)
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +0100394{
395 if (!strcmp(var, "color.ui")) {
Jeff Kinge269eb72011-08-17 22:03:48 -0700396 git_use_color_default = git_config_colorbool(var, value);
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +0100397 return 0;
398 }
399
Jeff King3e1dd172011-08-17 22:05:08 -0700400 return 0;
401}
402
Jeff King33c643b2017-10-13 13:24:31 -0400403int git_color_default_config(const char *var, const char *value, void *cb)
404{
405 if (git_color_config(var, value, cb) < 0)
406 return -1;
407
408 return git_default_config(var, value, cb);
409}
410
Jonathan Niederbecbdae2011-02-25 23:09:41 -0600411void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
412{
413 if (*color)
414 fprintf(fp, "%s", color);
415 fprintf(fp, "%s", sb->buf);
416 if (*color)
417 fprintf(fp, "%s", GIT_COLOR_RESET);
418}
419
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400420static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
Jeff King7c92fe02006-09-08 04:03:18 -0400421 va_list args, const char *trail)
422{
423 int r = 0;
424
425 if (*color)
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400426 r += fprintf(fp, "%s", color);
427 r += vfprintf(fp, fmt, args);
Jeff King7c92fe02006-09-08 04:03:18 -0400428 if (*color)
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +0100429 r += fprintf(fp, "%s", GIT_COLOR_RESET);
Jeff King7c92fe02006-09-08 04:03:18 -0400430 if (trail)
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400431 r += fprintf(fp, "%s", trail);
Jeff King7c92fe02006-09-08 04:03:18 -0400432 return r;
433}
434
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400435int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
Jeff King7c92fe02006-09-08 04:03:18 -0400436{
437 va_list args;
438 int r;
439 va_start(args, fmt);
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400440 r = color_vfprintf(fp, color, fmt, args, NULL);
Jeff King7c92fe02006-09-08 04:03:18 -0400441 va_end(args);
442 return r;
443}
444
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400445int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
Jeff King7c92fe02006-09-08 04:03:18 -0400446{
447 va_list args;
448 int r;
449 va_start(args, fmt);
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400450 r = color_vfprintf(fp, color, fmt, args, "\n");
Jeff King7c92fe02006-09-08 04:03:18 -0400451 va_end(args);
452 return r;
453}
Jeff King148135f2010-12-09 12:27:08 -0500454
455int color_is_nil(const char *c)
456{
457 return !strcmp(c, "NIL");
458}