Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 1 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Junio C Hamano | 8502357 | 2006-12-19 14:34:12 -0800 | [diff] [blame] | 3 | #include "color.h" |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 4 | |
Matthieu Moy | 4c7f181 | 2013-06-10 16:26:09 +0200 | [diff] [blame] | 5 | static int git_use_color_default = GIT_COLOR_AUTO; |
Jeff King | e269eb7 | 2011-08-17 22:03:48 -0700 | [diff] [blame] | 6 | int color_stdout_is_tty = -1; |
Matthias Kestenholz | 6b2f2d9 | 2008-02-18 08:26:03 +0100 | [diff] [blame] | 7 | |
Dan McGee | 7cd52b5 | 2011-04-05 00:40:23 -0500 | [diff] [blame] | 8 | /* |
| 9 | * The list of available column colors. |
| 10 | */ |
| 11 | const 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 Soha | 4a28eb0 | 2020-01-21 08:56:21 -0800 | [diff] [blame] | 27 | enum { |
| 28 | COLOR_BACKGROUND_OFFSET = 10, |
| 29 | COLOR_FOREGROUND_ANSI = 30, |
| 30 | COLOR_FOREGROUND_RGB = 38, |
| 31 | COLOR_FOREGROUND_256 = 38, |
Eyal Soha | 1751b09 | 2020-01-21 08:56:22 -0800 | [diff] [blame] | 32 | COLOR_FOREGROUND_BRIGHT_ANSI = 90, |
Eyal Soha | 4a28eb0 | 2020-01-21 08:56:21 -0800 | [diff] [blame] | 33 | }; |
| 34 | |
Dan McGee | 7cd52b5 | 2011-04-05 00:40:23 -0500 | [diff] [blame] | 35 | /* Ignore the RESET at the end when giving the size */ |
| 36 | const int column_colors_ansi_max = ARRAY_SIZE(column_colors_ansi) - 1; |
| 37 | |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 38 | /* An individual foreground or background color. */ |
| 39 | struct color { |
| 40 | enum { |
| 41 | COLOR_UNSPECIFIED = 0, |
| 42 | COLOR_NORMAL, |
Robert Estelle | 05f1f41 | 2021-10-25 22:32:36 +0000 | [diff] [blame] | 43 | COLOR_ANSI, /* basic 0-7 ANSI colors + "default" (value = 9) */ |
Jeff King | 17a4be2 | 2014-11-20 10:25:39 -0500 | [diff] [blame] | 44 | COLOR_256, |
| 45 | COLOR_RGB |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 46 | } type; |
| 47 | /* The numeric value for ANSI and 256-color modes */ |
| 48 | unsigned char value; |
Jeff King | 17a4be2 | 2014-11-20 10:25:39 -0500 | [diff] [blame] | 49 | /* 24-bit RGB color values */ |
| 50 | unsigned char red, green, blue; |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | /* |
| 54 | * "word" is a buffer of length "len"; does it match the NUL-terminated |
| 55 | * "match" exactly? |
| 56 | */ |
| 57 | static int match_word(const char *word, int len, const char *match) |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 58 | { |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 59 | return !strncasecmp(word, match, len) && !match[len]; |
| 60 | } |
| 61 | |
Jeff King | 17a4be2 | 2014-11-20 10:25:39 -0500 | [diff] [blame] | 62 | static 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 Soha | 1751b09 | 2020-01-21 08:56:22 -0800 | [diff] [blame] | 72 | /* |
| 73 | * If an ANSI color is recognized in "name", fill "out" and return 0. |
| 74 | * Otherwise, leave out unchanged and return -1. |
| 75 | */ |
| 76 | static int parse_ansi_color(struct color *out, const char *name, int len) |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 77 | { |
| 78 | /* Positions in array must match ANSI color codes */ |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 79 | static const char * const color_names[] = { |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 80 | "black", "red", "green", "yellow", |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 81 | "blue", "magenta", "cyan", "white" |
| 82 | }; |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 83 | int i; |
Eyal Soha | 1751b09 | 2020-01-21 08:56:22 -0800 | [diff] [blame] | 84 | int color_offset = COLOR_FOREGROUND_ANSI; |
| 85 | |
Robert Estelle | 05f1f41 | 2021-10-25 22:32:36 +0000 | [diff] [blame] | 86 | 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 Soha | 1751b09 | 2020-01-21 08:56:22 -0800 | [diff] [blame] | 107 | 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 | |
| 122 | static int parse_color(struct color *out, const char *name, int len) |
| 123 | { |
| 124 | char *end; |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 125 | 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 King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 131 | } |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 132 | |
Jeff King | 17a4be2 | 2014-11-20 10:25:39 -0500 | [diff] [blame] | 133 | /* 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 King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 143 | /* Then pick from our human-readable color names... */ |
Eyal Soha | 1751b09 | 2020-01-21 08:56:22 -0800 | [diff] [blame] | 144 | if (parse_ansi_color(out, name, len) == 0) { |
| 145 | return 0; |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 146 | } |
| 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 Soha | c444f03 | 2020-01-21 08:56:23 -0800 | [diff] [blame] | 160 | /* Rewrite 0-7 as more-portable standard colors. */ |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 161 | } else if (val < 8) { |
| 162 | out->type = COLOR_ANSI; |
Eyal Soha | 4a28eb0 | 2020-01-21 08:56:21 -0800 | [diff] [blame] | 163 | out->value = val + COLOR_FOREGROUND_ANSI; |
Jeff King | 3759d27 | 2015-01-20 17:14:48 -0500 | [diff] [blame] | 164 | return 0; |
Eyal Soha | c444f03 | 2020-01-21 08:56:23 -0800 | [diff] [blame] | 165 | /* 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 King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 170 | } else if (val < 256) { |
| 171 | out->type = COLOR_256; |
| 172 | out->value = val; |
| 173 | return 0; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return -1; |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 178 | } |
| 179 | |
Jeff King | df8e472 | 2016-06-23 13:38:13 -0400 | [diff] [blame] | 180 | static int parse_attr(const char *name, size_t len) |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 181 | { |
Jeff King | df8e472 | 2016-06-23 13:38:13 -0400 | [diff] [blame] | 182 | 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 King | 54590a0 | 2016-06-23 13:39:07 -0400 | [diff] [blame] | 190 | ATTR("italic", 3, 23), |
Jeff King | df8e472 | 2016-06-23 13:38:13 -0400 | [diff] [blame] | 191 | ATTR("ul", 4, 24), |
| 192 | ATTR("blink", 5, 25), |
Jeff King | 9dc3515 | 2016-06-23 13:40:16 -0400 | [diff] [blame] | 193 | ATTR("reverse", 7, 27), |
| 194 | ATTR("strike", 9, 29) |
Jeff King | df8e472 | 2016-06-23 13:38:13 -0400 | [diff] [blame] | 195 | #undef ATTR |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 196 | }; |
Jeff King | df8e472 | 2016-06-23 13:38:13 -0400 | [diff] [blame] | 197 | int negate = 0; |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 198 | int i; |
Jeff King | df8e472 | 2016-06-23 13:38:13 -0400 | [diff] [blame] | 199 | |
Jeff King | 5621068 | 2016-06-23 13:38:44 -0400 | [diff] [blame] | 200 | if (skip_prefix_mem(name, len, "no", &name, &len)) { |
| 201 | skip_prefix_mem(name, len, "-", &name, &len); |
Jeff King | df8e472 | 2016-06-23 13:38:13 -0400 | [diff] [blame] | 202 | negate = 1; |
Jeff King | 5621068 | 2016-06-23 13:38:44 -0400 | [diff] [blame] | 203 | } |
Jeff King | df8e472 | 2016-06-23 13:38:13 -0400 | [diff] [blame] | 204 | |
| 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 King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 208 | } |
| 209 | return -1; |
| 210 | } |
| 211 | |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 212 | int color_parse(const char *value, char *dst) |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 213 | { |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 214 | return color_parse_mem(value, strlen(value), dst); |
René Scharfe | 2c2dc7c | 2009-01-19 23:30:30 -0500 | [diff] [blame] | 215 | } |
| 216 | |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 217 | /* |
| 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 Soha | 4a28eb0 | 2020-01-21 08:56:21 -0800 | [diff] [blame] | 222 | static char *color_output(char *out, int len, const struct color *c, int background) |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 223 | { |
Eyal Soha | 4a28eb0 | 2020-01-21 08:56:21 -0800 | [diff] [blame] | 224 | int offset = 0; |
| 225 | |
| 226 | if (background) |
| 227 | offset = COLOR_BACKGROUND_OFFSET; |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 228 | switch (c->type) { |
| 229 | case COLOR_UNSPECIFIED: |
| 230 | case COLOR_NORMAL: |
| 231 | break; |
| 232 | case COLOR_ANSI: |
Eyal Soha | 4a28eb0 | 2020-01-21 08:56:21 -0800 | [diff] [blame] | 233 | out += xsnprintf(out, len, "%d", c->value + offset); |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 234 | break; |
| 235 | case COLOR_256: |
Eyal Soha | 4a28eb0 | 2020-01-21 08:56:21 -0800 | [diff] [blame] | 236 | out += xsnprintf(out, len, "%d;5;%d", COLOR_FOREGROUND_256 + offset, |
| 237 | c->value); |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 238 | break; |
Jeff King | 17a4be2 | 2014-11-20 10:25:39 -0500 | [diff] [blame] | 239 | case COLOR_RGB: |
Eyal Soha | 4a28eb0 | 2020-01-21 08:56:21 -0800 | [diff] [blame] | 240 | out += xsnprintf(out, len, "%d;2;%d;%d;%d", |
| 241 | COLOR_FOREGROUND_RGB + offset, |
Jeff King | cbc8fee | 2015-09-24 17:08:07 -0400 | [diff] [blame] | 242 | c->red, c->green, c->blue); |
Jeff King | 17a4be2 | 2014-11-20 10:25:39 -0500 | [diff] [blame] | 243 | break; |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 244 | } |
| 245 | return out; |
| 246 | } |
| 247 | |
| 248 | static int color_empty(const struct color *c) |
| 249 | { |
| 250 | return c->type <= COLOR_NORMAL; |
| 251 | } |
| 252 | |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 253 | int color_parse_mem(const char *value, int value_len, char *dst) |
René Scharfe | 2c2dc7c | 2009-01-19 23:30:30 -0500 | [diff] [blame] | 254 | { |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 255 | const char *ptr = value; |
René Scharfe | 2c2dc7c | 2009-01-19 23:30:30 -0500 | [diff] [blame] | 256 | int len = value_len; |
Jeff King | cbc8fee | 2015-09-24 17:08:07 -0400 | [diff] [blame] | 257 | char *end = dst + COLOR_MAXLEN; |
Robert Estelle | de65851 | 2021-10-26 01:03:47 +0000 | [diff] [blame] | 258 | unsigned int has_reset = 0; |
Junio C Hamano | 8b12413 | 2010-02-27 18:56:38 -0800 | [diff] [blame] | 259 | unsigned int attr = 0; |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 260 | struct color fg = { COLOR_UNSPECIFIED }; |
| 261 | struct color bg = { COLOR_UNSPECIFIED }; |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 262 | |
Nguyễn Thái Ngọc Duy | bc40756 | 2017-01-19 18:41:22 +0700 | [diff] [blame] | 263 | while (len > 0 && isspace(*ptr)) { |
| 264 | ptr++; |
| 265 | len--; |
| 266 | } |
| 267 | |
Jeff King | 55cccf4 | 2017-02-01 01:21:29 +0100 | [diff] [blame] | 268 | if (!len) { |
| 269 | dst[0] = '\0'; |
| 270 | return 0; |
| 271 | } |
Nguyễn Thái Ngọc Duy | c2f41bf | 2017-01-19 18:41:21 +0700 | [diff] [blame] | 272 | |
Robert Estelle | de65851 | 2021-10-26 01:03:47 +0000 | [diff] [blame] | 273 | /* [reset] [fg [bg]] [attr]... */ |
René Scharfe | 2c2dc7c | 2009-01-19 23:30:30 -0500 | [diff] [blame] | 274 | while (len > 0) { |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 275 | const char *word = ptr; |
Jeff King | 3e1952e | 2016-08-30 23:43:07 -0400 | [diff] [blame] | 276 | struct color c = { COLOR_UNSPECIFIED }; |
René Scharfe | 2c2dc7c | 2009-01-19 23:30:30 -0500 | [diff] [blame] | 277 | int val, wordlen = 0; |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 278 | |
René Scharfe | 2c2dc7c | 2009-01-19 23:30:30 -0500 | [diff] [blame] | 279 | while (len > 0 && !isspace(word[wordlen])) { |
| 280 | wordlen++; |
| 281 | len--; |
| 282 | } |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 283 | |
René Scharfe | 2c2dc7c | 2009-01-19 23:30:30 -0500 | [diff] [blame] | 284 | ptr = word + wordlen; |
| 285 | while (len > 0 && isspace(*ptr)) { |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 286 | ptr++; |
René Scharfe | 2c2dc7c | 2009-01-19 23:30:30 -0500 | [diff] [blame] | 287 | len--; |
| 288 | } |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 289 | |
Robert Estelle | de65851 | 2021-10-26 01:03:47 +0000 | [diff] [blame] | 290 | if (match_word(word, wordlen, "reset")) { |
| 291 | has_reset = 1; |
| 292 | continue; |
| 293 | } |
| 294 | |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 295 | if (!parse_color(&c, word, wordlen)) { |
| 296 | if (fg.type == COLOR_UNSPECIFIED) { |
| 297 | fg = c; |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 298 | continue; |
| 299 | } |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 300 | if (bg.type == COLOR_UNSPECIFIED) { |
| 301 | bg = c; |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 302 | continue; |
| 303 | } |
| 304 | goto bad; |
| 305 | } |
René Scharfe | 2c2dc7c | 2009-01-19 23:30:30 -0500 | [diff] [blame] | 306 | val = parse_attr(word, wordlen); |
Junio C Hamano | 8b12413 | 2010-02-27 18:56:38 -0800 | [diff] [blame] | 307 | if (0 <= val) |
| 308 | attr |= (1 << val); |
| 309 | else |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 310 | goto bad; |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 311 | } |
| 312 | |
Jeff King | cbc8fee | 2015-09-24 17:08:07 -0400 | [diff] [blame] | 313 | #undef OUT |
| 314 | #define OUT(x) do { \ |
| 315 | if (dst == end) \ |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 316 | BUG("color parsing ran out of space"); \ |
Jeff King | cbc8fee | 2015-09-24 17:08:07 -0400 | [diff] [blame] | 317 | *dst++ = (x); \ |
| 318 | } while(0) |
| 319 | |
Robert Estelle | de65851 | 2021-10-26 01:03:47 +0000 | [diff] [blame] | 320 | if (has_reset || attr || !color_empty(&fg) || !color_empty(&bg)) { |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 321 | int sep = 0; |
Junio C Hamano | 8b12413 | 2010-02-27 18:56:38 -0800 | [diff] [blame] | 322 | int i; |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 323 | |
Jeff King | cbc8fee | 2015-09-24 17:08:07 -0400 | [diff] [blame] | 324 | OUT('\033'); |
| 325 | OUT('['); |
Junio C Hamano | 8b12413 | 2010-02-27 18:56:38 -0800 | [diff] [blame] | 326 | |
Robert Estelle | de65851 | 2021-10-26 01:03:47 +0000 | [diff] [blame] | 327 | if (has_reset) |
| 328 | sep++; |
| 329 | |
Junio C Hamano | 8b12413 | 2010-02-27 18:56:38 -0800 | [diff] [blame] | 330 | for (i = 0; attr; i++) { |
| 331 | unsigned bit = (1 << i); |
| 332 | if (!(attr & bit)) |
| 333 | continue; |
| 334 | attr &= ~bit; |
| 335 | if (sep++) |
Jeff King | cbc8fee | 2015-09-24 17:08:07 -0400 | [diff] [blame] | 336 | OUT(';'); |
| 337 | dst += xsnprintf(dst, end - dst, "%d", i); |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 338 | } |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 339 | if (!color_empty(&fg)) { |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 340 | if (sep++) |
Jeff King | cbc8fee | 2015-09-24 17:08:07 -0400 | [diff] [blame] | 341 | OUT(';'); |
Eyal Soha | 4a28eb0 | 2020-01-21 08:56:21 -0800 | [diff] [blame] | 342 | dst = color_output(dst, end - dst, &fg, 0); |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 343 | } |
Jeff King | 695d95d | 2014-11-20 10:17:05 -0500 | [diff] [blame] | 344 | if (!color_empty(&bg)) { |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 345 | if (sep++) |
Jeff King | cbc8fee | 2015-09-24 17:08:07 -0400 | [diff] [blame] | 346 | OUT(';'); |
Eyal Soha | 4a28eb0 | 2020-01-21 08:56:21 -0800 | [diff] [blame] | 347 | dst = color_output(dst, end - dst, &bg, 1); |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 348 | } |
Jeff King | cbc8fee | 2015-09-24 17:08:07 -0400 | [diff] [blame] | 349 | OUT('m'); |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 350 | } |
Jeff King | cbc8fee | 2015-09-24 17:08:07 -0400 | [diff] [blame] | 351 | OUT(0); |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 352 | return 0; |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 353 | bad: |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 354 | return error(_("invalid color value: %.*s"), value_len, value); |
Jeff King | cbc8fee | 2015-09-24 17:08:07 -0400 | [diff] [blame] | 355 | #undef OUT |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 356 | } |
| 357 | |
Jeff King | e269eb7 | 2011-08-17 22:03:48 -0700 | [diff] [blame] | 358 | int git_config_colorbool(const char *var, const char *value) |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 359 | { |
Junio C Hamano | 57f2b84 | 2007-11-26 14:30:28 -0800 | [diff] [blame] | 360 | if (value) { |
| 361 | if (!strcasecmp(value, "never")) |
| 362 | return 0; |
| 363 | if (!strcasecmp(value, "always")) |
Jeff King | 2c1acdf | 2017-10-13 13:23:24 -0400 | [diff] [blame] | 364 | return 1; |
Junio C Hamano | 57f2b84 | 2007-11-26 14:30:28 -0800 | [diff] [blame] | 365 | if (!strcasecmp(value, "auto")) |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 366 | return GIT_COLOR_AUTO; |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 367 | } |
Junio C Hamano | 57f2b84 | 2007-11-26 14:30:28 -0800 | [diff] [blame] | 368 | |
Mark Lodato | 73e9da0 | 2010-02-16 23:55:58 -0500 | [diff] [blame] | 369 | if (!var) |
| 370 | return -1; |
| 371 | |
Junio C Hamano | 57f2b84 | 2007-11-26 14:30:28 -0800 | [diff] [blame] | 372 | /* Missing or explicit false to turn off colorization */ |
| 373 | if (!git_config_bool(var, value)) |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 374 | return 0; |
Junio C Hamano | 57f2b84 | 2007-11-26 14:30:28 -0800 | [diff] [blame] | 375 | |
| 376 | /* any normal truth value defaults to 'auto' */ |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 377 | return GIT_COLOR_AUTO; |
| 378 | } |
| 379 | |
Johannes Schindelin | 295d949 | 2018-04-21 12:09:57 +0200 | [diff] [blame] | 380 | static int check_auto_color(int fd) |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 381 | { |
Johannes Schindelin | 295d949 | 2018-04-21 12:09:57 +0200 | [diff] [blame] | 382 | 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 Schneider | a64f213 | 2017-11-29 15:37:51 +0100 | [diff] [blame] | 387 | if (!is_terminal_dumb()) |
Junio C Hamano | 57f2b84 | 2007-11-26 14:30:28 -0800 | [diff] [blame] | 388 | return 1; |
| 389 | } |
| 390 | return 0; |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 391 | } |
| 392 | |
Johannes Schindelin | 295d949 | 2018-04-21 12:09:57 +0200 | [diff] [blame] | 393 | int want_color_fd(int fd, int var) |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 394 | { |
Martin Ågren | 6cdf8a7 | 2017-08-21 19:43:48 +0200 | [diff] [blame] | 395 | /* |
| 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 Schindelin | 295d949 | 2018-04-21 12:09:57 +0200 | [diff] [blame] | 402 | static int want_auto[3] = { -1, -1, -1 }; |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 403 | |
Eric Sunshine | 65bb21e | 2018-08-02 23:07:49 -0700 | [diff] [blame] | 404 | if (fd < 1 || fd >= ARRAY_SIZE(want_auto)) |
| 405 | BUG("file descriptor out of range: %d", fd); |
| 406 | |
Jeff King | c9bfb95 | 2011-08-17 22:05:35 -0700 | [diff] [blame] | 407 | if (var < 0) |
| 408 | var = git_use_color_default; |
| 409 | |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 410 | if (var == GIT_COLOR_AUTO) { |
Johannes Schindelin | 295d949 | 2018-04-21 12:09:57 +0200 | [diff] [blame] | 411 | if (want_auto[fd] < 0) |
| 412 | want_auto[fd] = check_auto_color(fd); |
| 413 | return want_auto[fd]; |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 414 | } |
Jeff King | c9bfb95 | 2011-08-17 22:05:35 -0700 | [diff] [blame] | 415 | return var; |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 418 | int git_color_config(const char *var, const char *value, void *cb UNUSED) |
Matthias Kestenholz | 6b2f2d9 | 2008-02-18 08:26:03 +0100 | [diff] [blame] | 419 | { |
| 420 | if (!strcmp(var, "color.ui")) { |
Jeff King | e269eb7 | 2011-08-17 22:03:48 -0700 | [diff] [blame] | 421 | git_use_color_default = git_config_colorbool(var, value); |
Matthias Kestenholz | 6b2f2d9 | 2008-02-18 08:26:03 +0100 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | |
Jeff King | 3e1dd17 | 2011-08-17 22:05:08 -0700 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | |
Jeff King | 33c643b | 2017-10-13 13:24:31 -0400 | [diff] [blame] | 428 | int 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 Nieder | becbdae | 2011-02-25 23:09:41 -0600 | [diff] [blame] | 436 | void 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øgsberg | f26a001 | 2007-09-17 20:06:42 -0400 | [diff] [blame] | 445 | static int color_vfprintf(FILE *fp, const char *color, const char *fmt, |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 446 | va_list args, const char *trail) |
| 447 | { |
| 448 | int r = 0; |
| 449 | |
| 450 | if (*color) |
Kristian Høgsberg | f26a001 | 2007-09-17 20:06:42 -0400 | [diff] [blame] | 451 | r += fprintf(fp, "%s", color); |
| 452 | r += vfprintf(fp, fmt, args); |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 453 | if (*color) |
Arjen Laarhoven | dc6ebd4 | 2009-02-13 22:53:40 +0100 | [diff] [blame] | 454 | r += fprintf(fp, "%s", GIT_COLOR_RESET); |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 455 | if (trail) |
Kristian Høgsberg | f26a001 | 2007-09-17 20:06:42 -0400 | [diff] [blame] | 456 | r += fprintf(fp, "%s", trail); |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 457 | return r; |
| 458 | } |
| 459 | |
Kristian Høgsberg | f26a001 | 2007-09-17 20:06:42 -0400 | [diff] [blame] | 460 | int color_fprintf(FILE *fp, const char *color, const char *fmt, ...) |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 461 | { |
| 462 | va_list args; |
| 463 | int r; |
| 464 | va_start(args, fmt); |
Kristian Høgsberg | f26a001 | 2007-09-17 20:06:42 -0400 | [diff] [blame] | 465 | r = color_vfprintf(fp, color, fmt, args, NULL); |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 466 | va_end(args); |
| 467 | return r; |
| 468 | } |
| 469 | |
Kristian Høgsberg | f26a001 | 2007-09-17 20:06:42 -0400 | [diff] [blame] | 470 | int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...) |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 471 | { |
| 472 | va_list args; |
| 473 | int r; |
| 474 | va_start(args, fmt); |
Kristian Høgsberg | f26a001 | 2007-09-17 20:06:42 -0400 | [diff] [blame] | 475 | r = color_vfprintf(fp, color, fmt, args, "\n"); |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 476 | va_end(args); |
| 477 | return r; |
| 478 | } |
Jeff King | 148135f | 2010-12-09 12:27:08 -0500 | [diff] [blame] | 479 | |
| 480 | int color_is_nil(const char *c) |
| 481 | { |
| 482 | return !strcmp(c, "NIL"); |
| 483 | } |