blob: f05d8a81d72115edbf47d34950a31615a15ea98e [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,
Robert Estelle05f1f412021-10-25 22:32:36 +000043 COLOR_ANSI, /* basic 0-7 ANSI colors + "default" (value = 9) */
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
Robert Estelle05f1f412021-10-25 22:32:36 +000086 if (match_word(name, len, "default")) {
87 /*
88 * Restores to the terminal's default color, which may not be
89 * the same as explicitly setting "white" or "black".
90 *
91 * ECMA-48 - Control Functions \
92 * for Coded Character Sets, 5th edition (June 1991):
93 * > 39 default display colour (implementation-defined)
94 * > 49 default background colour (implementation-defined)
95 *
96 * Although not supported /everywhere/--according to terminfo,
97 * some terminals define "op" (original pair) as a blunt
98 * "set to white on black", or even "send full SGR reset"--
99 * it's standard and well-supported enough that if a user
100 * asks for it in their config this will do the right thing.
101 */
102 out->type = COLOR_ANSI;
103 out->value = 9 + color_offset;
104 return 0;
105 }
106
Eyal Soha1751b092020-01-21 08:56:22 -0800107 if (strncasecmp(name, "bright", 6) == 0) {
108 color_offset = COLOR_FOREGROUND_BRIGHT_ANSI;
109 name += 6;
110 len -= 6;
111 }
112 for (i = 0; i < ARRAY_SIZE(color_names); i++) {
113 if (match_word(name, len, color_names[i])) {
114 out->type = COLOR_ANSI;
115 out->value = i + color_offset;
116 return 0;
117 }
118 }
119 return -1;
120}
121
122static int parse_color(struct color *out, const char *name, int len)
123{
124 char *end;
Jeff King695d95d2014-11-20 10:17:05 -0500125 long val;
126
127 /* First try the special word "normal"... */
128 if (match_word(name, len, "normal")) {
129 out->type = COLOR_NORMAL;
130 return 0;
Jeff King7c92fe02006-09-08 04:03:18 -0400131 }
Jeff King695d95d2014-11-20 10:17:05 -0500132
Jeff King17a4be22014-11-20 10:25:39 -0500133 /* Try a 24-bit RGB value */
134 if (len == 7 && name[0] == '#') {
135 if (!get_hex_color(name + 1, &out->red) &&
136 !get_hex_color(name + 3, &out->green) &&
137 !get_hex_color(name + 5, &out->blue)) {
138 out->type = COLOR_RGB;
139 return 0;
140 }
141 }
142
Jeff King695d95d2014-11-20 10:17:05 -0500143 /* Then pick from our human-readable color names... */
Eyal Soha1751b092020-01-21 08:56:22 -0800144 if (parse_ansi_color(out, name, len) == 0) {
145 return 0;
Jeff King695d95d2014-11-20 10:17:05 -0500146 }
147
148 /* And finally try a literal 256-color-mode number */
149 val = strtol(name, &end, 10);
150 if (end - name == len) {
151 /*
152 * Allow "-1" as an alias for "normal", but other negative
153 * numbers are bogus.
154 */
155 if (val < -1)
156 ; /* fall through to error */
157 else if (val < 0) {
158 out->type = COLOR_NORMAL;
159 return 0;
Eyal Sohac444f032020-01-21 08:56:23 -0800160 /* Rewrite 0-7 as more-portable standard colors. */
Jeff King695d95d2014-11-20 10:17:05 -0500161 } else if (val < 8) {
162 out->type = COLOR_ANSI;
Eyal Soha4a28eb02020-01-21 08:56:21 -0800163 out->value = val + COLOR_FOREGROUND_ANSI;
Jeff King3759d272015-01-20 17:14:48 -0500164 return 0;
Eyal Sohac444f032020-01-21 08:56:23 -0800165 /* Rewrite 8-15 as more-portable aixterm colors. */
166 } else if (val < 16) {
167 out->type = COLOR_ANSI;
168 out->value = val - 8 + COLOR_FOREGROUND_BRIGHT_ANSI;
169 return 0;
Jeff King695d95d2014-11-20 10:17:05 -0500170 } else if (val < 256) {
171 out->type = COLOR_256;
172 out->value = val;
173 return 0;
174 }
175 }
176
177 return -1;
Jeff King7c92fe02006-09-08 04:03:18 -0400178}
179
Jeff Kingdf8e4722016-06-23 13:38:13 -0400180static int parse_attr(const char *name, size_t len)
Jeff King7c92fe02006-09-08 04:03:18 -0400181{
Jeff Kingdf8e4722016-06-23 13:38:13 -0400182 static const struct {
183 const char *name;
184 size_t len;
185 int val, neg;
186 } attrs[] = {
187#define ATTR(x, val, neg) { (x), sizeof(x)-1, (val), (neg) }
188 ATTR("bold", 1, 22),
189 ATTR("dim", 2, 22),
Jeff King54590a02016-06-23 13:39:07 -0400190 ATTR("italic", 3, 23),
Jeff Kingdf8e4722016-06-23 13:38:13 -0400191 ATTR("ul", 4, 24),
192 ATTR("blink", 5, 25),
Jeff King9dc35152016-06-23 13:40:16 -0400193 ATTR("reverse", 7, 27),
194 ATTR("strike", 9, 29)
Jeff Kingdf8e4722016-06-23 13:38:13 -0400195#undef ATTR
Jeff King7c92fe02006-09-08 04:03:18 -0400196 };
Jeff Kingdf8e4722016-06-23 13:38:13 -0400197 int negate = 0;
Jeff King7c92fe02006-09-08 04:03:18 -0400198 int i;
Jeff Kingdf8e4722016-06-23 13:38:13 -0400199
Jeff King56210682016-06-23 13:38:44 -0400200 if (skip_prefix_mem(name, len, "no", &name, &len)) {
201 skip_prefix_mem(name, len, "-", &name, &len);
Jeff Kingdf8e4722016-06-23 13:38:13 -0400202 negate = 1;
Jeff King56210682016-06-23 13:38:44 -0400203 }
Jeff Kingdf8e4722016-06-23 13:38:13 -0400204
205 for (i = 0; i < ARRAY_SIZE(attrs); i++) {
206 if (attrs[i].len == len && !memcmp(attrs[i].name, name, len))
207 return negate ? attrs[i].neg : attrs[i].val;
Jeff King7c92fe02006-09-08 04:03:18 -0400208 }
209 return -1;
210}
211
Jeff Kingf6c5a292014-10-07 15:33:09 -0400212int color_parse(const char *value, char *dst)
Jeff King7c92fe02006-09-08 04:03:18 -0400213{
Jeff Kingf6c5a292014-10-07 15:33:09 -0400214 return color_parse_mem(value, strlen(value), dst);
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500215}
216
Jeff King695d95d2014-11-20 10:17:05 -0500217/*
218 * Write the ANSI color codes for "c" to "out"; the string should
219 * already have the ANSI escape code in it. "out" should have enough
220 * space in it to fit any color.
221 */
Eyal Soha4a28eb02020-01-21 08:56:21 -0800222static char *color_output(char *out, int len, const struct color *c, int background)
Jeff King695d95d2014-11-20 10:17:05 -0500223{
Eyal Soha4a28eb02020-01-21 08:56:21 -0800224 int offset = 0;
225
226 if (background)
227 offset = COLOR_BACKGROUND_OFFSET;
Jeff King695d95d2014-11-20 10:17:05 -0500228 switch (c->type) {
229 case COLOR_UNSPECIFIED:
230 case COLOR_NORMAL:
231 break;
232 case COLOR_ANSI:
Eyal Soha4a28eb02020-01-21 08:56:21 -0800233 out += xsnprintf(out, len, "%d", c->value + offset);
Jeff King695d95d2014-11-20 10:17:05 -0500234 break;
235 case COLOR_256:
Eyal Soha4a28eb02020-01-21 08:56:21 -0800236 out += xsnprintf(out, len, "%d;5;%d", COLOR_FOREGROUND_256 + offset,
237 c->value);
Jeff King695d95d2014-11-20 10:17:05 -0500238 break;
Jeff King17a4be22014-11-20 10:25:39 -0500239 case COLOR_RGB:
Eyal Soha4a28eb02020-01-21 08:56:21 -0800240 out += xsnprintf(out, len, "%d;2;%d;%d;%d",
241 COLOR_FOREGROUND_RGB + offset,
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400242 c->red, c->green, c->blue);
Jeff King17a4be22014-11-20 10:25:39 -0500243 break;
Jeff King695d95d2014-11-20 10:17:05 -0500244 }
245 return out;
246}
247
248static int color_empty(const struct color *c)
249{
250 return c->type <= COLOR_NORMAL;
251}
252
Jeff Kingf6c5a292014-10-07 15:33:09 -0400253int color_parse_mem(const char *value, int value_len, char *dst)
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500254{
Jeff King7c92fe02006-09-08 04:03:18 -0400255 const char *ptr = value;
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500256 int len = value_len;
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400257 char *end = dst + COLOR_MAXLEN;
Robert Estellede658512021-10-26 01:03:47 +0000258 unsigned int has_reset = 0;
Junio C Hamano8b124132010-02-27 18:56:38 -0800259 unsigned int attr = 0;
Jeff King695d95d2014-11-20 10:17:05 -0500260 struct color fg = { COLOR_UNSPECIFIED };
261 struct color bg = { COLOR_UNSPECIFIED };
Jeff King7c92fe02006-09-08 04:03:18 -0400262
Nguyễn Thái Ngọc Duybc407562017-01-19 18:41:22 +0700263 while (len > 0 && isspace(*ptr)) {
264 ptr++;
265 len--;
266 }
267
Jeff King55cccf42017-02-01 01:21:29 +0100268 if (!len) {
269 dst[0] = '\0';
270 return 0;
271 }
Nguyễn Thái Ngọc Duyc2f41bf2017-01-19 18:41:21 +0700272
Robert Estellede658512021-10-26 01:03:47 +0000273 /* [reset] [fg [bg]] [attr]... */
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500274 while (len > 0) {
Jeff King7c92fe02006-09-08 04:03:18 -0400275 const char *word = ptr;
Jeff King3e1952e2016-08-30 23:43:07 -0400276 struct color c = { COLOR_UNSPECIFIED };
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500277 int val, wordlen = 0;
Jeff King7c92fe02006-09-08 04:03:18 -0400278
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500279 while (len > 0 && !isspace(word[wordlen])) {
280 wordlen++;
281 len--;
282 }
Jeff King7c92fe02006-09-08 04:03:18 -0400283
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500284 ptr = word + wordlen;
285 while (len > 0 && isspace(*ptr)) {
Jeff King7c92fe02006-09-08 04:03:18 -0400286 ptr++;
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500287 len--;
288 }
Jeff King7c92fe02006-09-08 04:03:18 -0400289
Robert Estellede658512021-10-26 01:03:47 +0000290 if (match_word(word, wordlen, "reset")) {
291 has_reset = 1;
292 continue;
293 }
294
Jeff King695d95d2014-11-20 10:17:05 -0500295 if (!parse_color(&c, word, wordlen)) {
296 if (fg.type == COLOR_UNSPECIFIED) {
297 fg = c;
Jeff King7c92fe02006-09-08 04:03:18 -0400298 continue;
299 }
Jeff King695d95d2014-11-20 10:17:05 -0500300 if (bg.type == COLOR_UNSPECIFIED) {
301 bg = c;
Jeff King7c92fe02006-09-08 04:03:18 -0400302 continue;
303 }
304 goto bad;
305 }
René Scharfe2c2dc7c2009-01-19 23:30:30 -0500306 val = parse_attr(word, wordlen);
Junio C Hamano8b124132010-02-27 18:56:38 -0800307 if (0 <= val)
308 attr |= (1 << val);
309 else
Jeff King7c92fe02006-09-08 04:03:18 -0400310 goto bad;
Jeff King7c92fe02006-09-08 04:03:18 -0400311 }
312
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400313#undef OUT
314#define OUT(x) do { \
315 if (dst == end) \
Johannes Schindelin033abf92018-05-02 11:38:39 +0200316 BUG("color parsing ran out of space"); \
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400317 *dst++ = (x); \
318} while(0)
319
Robert Estellede658512021-10-26 01:03:47 +0000320 if (has_reset || attr || !color_empty(&fg) || !color_empty(&bg)) {
Jeff King7c92fe02006-09-08 04:03:18 -0400321 int sep = 0;
Junio C Hamano8b124132010-02-27 18:56:38 -0800322 int i;
Jeff King7c92fe02006-09-08 04:03:18 -0400323
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400324 OUT('\033');
325 OUT('[');
Junio C Hamano8b124132010-02-27 18:56:38 -0800326
Robert Estellede658512021-10-26 01:03:47 +0000327 if (has_reset)
328 sep++;
329
Junio C Hamano8b124132010-02-27 18:56:38 -0800330 for (i = 0; attr; i++) {
331 unsigned bit = (1 << i);
332 if (!(attr & bit))
333 continue;
334 attr &= ~bit;
335 if (sep++)
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400336 OUT(';');
337 dst += xsnprintf(dst, end - dst, "%d", i);
Jeff King7c92fe02006-09-08 04:03:18 -0400338 }
Jeff King695d95d2014-11-20 10:17:05 -0500339 if (!color_empty(&fg)) {
Jeff King7c92fe02006-09-08 04:03:18 -0400340 if (sep++)
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400341 OUT(';');
Eyal Soha4a28eb02020-01-21 08:56:21 -0800342 dst = color_output(dst, end - dst, &fg, 0);
Jeff King7c92fe02006-09-08 04:03:18 -0400343 }
Jeff King695d95d2014-11-20 10:17:05 -0500344 if (!color_empty(&bg)) {
Jeff King7c92fe02006-09-08 04:03:18 -0400345 if (sep++)
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400346 OUT(';');
Eyal Soha4a28eb02020-01-21 08:56:21 -0800347 dst = color_output(dst, end - dst, &bg, 1);
Jeff King7c92fe02006-09-08 04:03:18 -0400348 }
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400349 OUT('m');
Jeff King7c92fe02006-09-08 04:03:18 -0400350 }
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400351 OUT(0);
Jeff Kingf6c5a292014-10-07 15:33:09 -0400352 return 0;
Jeff King7c92fe02006-09-08 04:03:18 -0400353bad:
Jeff Kingf6c5a292014-10-07 15:33:09 -0400354 return error(_("invalid color value: %.*s"), value_len, value);
Jeff Kingcbc8fee2015-09-24 17:08:07 -0400355#undef OUT
Jeff King7c92fe02006-09-08 04:03:18 -0400356}
357
Jeff Kinge269eb72011-08-17 22:03:48 -0700358int git_config_colorbool(const char *var, const char *value)
Jeff King7c92fe02006-09-08 04:03:18 -0400359{
Junio C Hamano57f2b842007-11-26 14:30:28 -0800360 if (value) {
361 if (!strcasecmp(value, "never"))
362 return 0;
363 if (!strcasecmp(value, "always"))
Jeff King2c1acdf2017-10-13 13:23:24 -0400364 return 1;
Junio C Hamano57f2b842007-11-26 14:30:28 -0800365 if (!strcasecmp(value, "auto"))
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700366 return GIT_COLOR_AUTO;
Jeff King7c92fe02006-09-08 04:03:18 -0400367 }
Junio C Hamano57f2b842007-11-26 14:30:28 -0800368
Mark Lodato73e9da02010-02-16 23:55:58 -0500369 if (!var)
370 return -1;
371
Junio C Hamano57f2b842007-11-26 14:30:28 -0800372 /* Missing or explicit false to turn off colorization */
373 if (!git_config_bool(var, value))
Jeff King7c92fe02006-09-08 04:03:18 -0400374 return 0;
Junio C Hamano57f2b842007-11-26 14:30:28 -0800375
376 /* any normal truth value defaults to 'auto' */
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700377 return GIT_COLOR_AUTO;
378}
379
Johannes Schindelin295d9492018-04-21 12:09:57 +0200380static int check_auto_color(int fd)
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700381{
Johannes Schindelin295d9492018-04-21 12:09:57 +0200382 static int color_stderr_is_tty = -1;
383 int *is_tty_p = fd == 1 ? &color_stdout_is_tty : &color_stderr_is_tty;
384 if (*is_tty_p < 0)
385 *is_tty_p = isatty(fd);
386 if (*is_tty_p || (fd == 1 && pager_in_use() && pager_use_color)) {
Lars Schneidera64f2132017-11-29 15:37:51 +0100387 if (!is_terminal_dumb())
Junio C Hamano57f2b842007-11-26 14:30:28 -0800388 return 1;
389 }
390 return 0;
Jeff King7c92fe02006-09-08 04:03:18 -0400391}
392
Johannes Schindelin295d9492018-04-21 12:09:57 +0200393int want_color_fd(int fd, int var)
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700394{
Martin Ågren6cdf8a72017-08-21 19:43:48 +0200395 /*
396 * NEEDSWORK: This function is sometimes used from multiple threads, and
397 * we end up using want_auto racily. That "should not matter" since
398 * we always write the same value, but it's still wrong. This function
399 * is listed in .tsan-suppressions for the time being.
400 */
401
Johannes Schindelin295d9492018-04-21 12:09:57 +0200402 static int want_auto[3] = { -1, -1, -1 };
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700403
Eric Sunshine65bb21e2018-08-02 23:07:49 -0700404 if (fd < 1 || fd >= ARRAY_SIZE(want_auto))
405 BUG("file descriptor out of range: %d", fd);
406
Jeff Kingc9bfb952011-08-17 22:05:35 -0700407 if (var < 0)
408 var = git_use_color_default;
409
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700410 if (var == GIT_COLOR_AUTO) {
Johannes Schindelin295d9492018-04-21 12:09:57 +0200411 if (want_auto[fd] < 0)
412 want_auto[fd] = check_auto_color(fd);
413 return want_auto[fd];
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700414 }
Jeff Kingc9bfb952011-08-17 22:05:35 -0700415 return var;
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700416}
417
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200418int git_color_config(const char *var, const char *value, void *cb UNUSED)
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +0100419{
420 if (!strcmp(var, "color.ui")) {
Jeff Kinge269eb72011-08-17 22:03:48 -0700421 git_use_color_default = git_config_colorbool(var, value);
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +0100422 return 0;
423 }
424
Jeff King3e1dd172011-08-17 22:05:08 -0700425 return 0;
426}
427
Jeff King33c643b2017-10-13 13:24:31 -0400428int git_color_default_config(const char *var, const char *value, void *cb)
429{
430 if (git_color_config(var, value, cb) < 0)
431 return -1;
432
433 return git_default_config(var, value, cb);
434}
435
Jonathan Niederbecbdae2011-02-25 23:09:41 -0600436void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
437{
438 if (*color)
439 fprintf(fp, "%s", color);
440 fprintf(fp, "%s", sb->buf);
441 if (*color)
442 fprintf(fp, "%s", GIT_COLOR_RESET);
443}
444
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400445static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
Jeff King7c92fe02006-09-08 04:03:18 -0400446 va_list args, const char *trail)
447{
448 int r = 0;
449
450 if (*color)
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400451 r += fprintf(fp, "%s", color);
452 r += vfprintf(fp, fmt, args);
Jeff King7c92fe02006-09-08 04:03:18 -0400453 if (*color)
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +0100454 r += fprintf(fp, "%s", GIT_COLOR_RESET);
Jeff King7c92fe02006-09-08 04:03:18 -0400455 if (trail)
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400456 r += fprintf(fp, "%s", trail);
Jeff King7c92fe02006-09-08 04:03:18 -0400457 return r;
458}
459
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400460int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
Jeff King7c92fe02006-09-08 04:03:18 -0400461{
462 va_list args;
463 int r;
464 va_start(args, fmt);
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400465 r = color_vfprintf(fp, color, fmt, args, NULL);
Jeff King7c92fe02006-09-08 04:03:18 -0400466 va_end(args);
467 return r;
468}
469
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400470int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
Jeff King7c92fe02006-09-08 04:03:18 -0400471{
472 va_list args;
473 int r;
474 va_start(args, fmt);
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400475 r = color_vfprintf(fp, color, fmt, args, "\n");
Jeff King7c92fe02006-09-08 04:03:18 -0400476 va_end(args);
477 return r;
478}
Jeff King148135f2010-12-09 12:27:08 -0500479
480int color_is_nil(const char *c)
481{
482 return !strcmp(c, "NIL");
483}