4 static int git_use_color_default = GIT_COLOR_AUTO;
5 int color_stdout_is_tty = -1;
8 * The list of available column colors.
10 const char *column_colors_ansi[] = {
19 GIT_COLOR_BOLD_YELLOW,
21 GIT_COLOR_BOLD_MAGENTA,
26 /* Ignore the RESET at the end when giving the size */
27 const int column_colors_ansi_max = ARRAY_SIZE(column_colors_ansi) - 1;
29 /* An individual foreground or background color. */
32 COLOR_UNSPECIFIED = 0,
34 COLOR_ANSI, /* basic 0-7 ANSI colors */
37 /* The numeric value for ANSI and 256-color modes */
42 * "word" is a buffer of length "len"; does it match the NUL-terminated
45 static int match_word(const char *word, int len, const char *match)
47 return !strncasecmp(word, match, len) && !match[len];
50 static int parse_color(struct color *out, const char *name, int len)
52 /* Positions in array must match ANSI color codes */
53 static const char * const color_names[] = {
54 "black", "red", "green", "yellow",
55 "blue", "magenta", "cyan", "white"
61 /* First try the special word "normal"... */
62 if (match_word(name, len, "normal")) {
63 out->type = COLOR_NORMAL;
67 /* Then pick from our human-readable color names... */
68 for (i = 0; i < ARRAY_SIZE(color_names); i++) {
69 if (match_word(name, len, color_names[i])) {
70 out->type = COLOR_ANSI;
76 /* And finally try a literal 256-color-mode number */
77 val = strtol(name, &end, 10);
78 if (end - name == len) {
80 * Allow "-1" as an alias for "normal", but other negative
84 ; /* fall through to error */
86 out->type = COLOR_NORMAL;
88 /* Rewrite low numbers as more-portable standard colors. */
90 out->type = COLOR_ANSI;
92 } else if (val < 256) {
93 out->type = COLOR_256;
102 static int parse_attr(const char *name, int len)
104 static const int attr_values[] = { 1, 2, 4, 5, 7 };
105 static const char * const attr_names[] = {
106 "bold", "dim", "ul", "blink", "reverse"
109 for (i = 0; i < ARRAY_SIZE(attr_names); i++) {
110 const char *str = attr_names[i];
111 if (!strncasecmp(name, str, len) && !str[len])
112 return attr_values[i];
117 int color_parse(const char *value, char *dst)
119 return color_parse_mem(value, strlen(value), dst);
122 #define COLOR_FOREGROUND '3'
123 #define COLOR_BACKGROUND '4'
126 * Write the ANSI color codes for "c" to "out"; the string should
127 * already have the ANSI escape code in it. "out" should have enough
128 * space in it to fit any color.
130 static char *color_output(char *out, const struct color *c, char type)
133 case COLOR_UNSPECIFIED:
138 *out++ = '0' + c->value;
141 out += sprintf(out, "%c8;5;%d", type, c->value);
147 static int color_empty(const struct color *c)
149 return c->type <= COLOR_NORMAL;
152 int color_parse_mem(const char *value, int value_len, char *dst)
154 const char *ptr = value;
156 unsigned int attr = 0;
157 struct color fg = { COLOR_UNSPECIFIED };
158 struct color bg = { COLOR_UNSPECIFIED };
160 if (!strncasecmp(value, "reset", len)) {
161 strcpy(dst, GIT_COLOR_RESET);
165 /* [fg [bg]] [attr]... */
167 const char *word = ptr;
169 int val, wordlen = 0;
171 while (len > 0 && !isspace(word[wordlen])) {
176 ptr = word + wordlen;
177 while (len > 0 && isspace(*ptr)) {
182 if (!parse_color(&c, word, wordlen)) {
183 if (fg.type == COLOR_UNSPECIFIED) {
187 if (bg.type == COLOR_UNSPECIFIED) {
193 val = parse_attr(word, wordlen);
200 if (attr || !color_empty(&fg) || !color_empty(&bg)) {
207 for (i = 0; attr; i++) {
208 unsigned bit = (1 << i);
216 if (!color_empty(&fg)) {
219 dst = color_output(dst, &fg, COLOR_FOREGROUND);
221 if (!color_empty(&bg)) {
224 dst = color_output(dst, &bg, COLOR_BACKGROUND);
231 return error(_("invalid color value: %.*s"), value_len, value);
234 int git_config_colorbool(const char *var, const char *value)
237 if (!strcasecmp(value, "never"))
239 if (!strcasecmp(value, "always"))
241 if (!strcasecmp(value, "auto"))
242 return GIT_COLOR_AUTO;
248 /* Missing or explicit false to turn off colorization */
249 if (!git_config_bool(var, value))
252 /* any normal truth value defaults to 'auto' */
253 return GIT_COLOR_AUTO;
256 static int check_auto_color(void)
258 if (color_stdout_is_tty < 0)
259 color_stdout_is_tty = isatty(1);
260 if (color_stdout_is_tty || (pager_in_use() && pager_use_color)) {
261 char *term = getenv("TERM");
262 if (term && strcmp(term, "dumb"))
268 int want_color(int var)
270 static int want_auto = -1;
273 var = git_use_color_default;
275 if (var == GIT_COLOR_AUTO) {
277 want_auto = check_auto_color();
283 int git_color_config(const char *var, const char *value, void *cb)
285 if (!strcmp(var, "color.ui")) {
286 git_use_color_default = git_config_colorbool(var, value);
293 int git_color_default_config(const char *var, const char *value, void *cb)
295 if (git_color_config(var, value, cb) < 0)
298 return git_default_config(var, value, cb);
301 void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
304 fprintf(fp, "%s", color);
305 fprintf(fp, "%s", sb->buf);
307 fprintf(fp, "%s", GIT_COLOR_RESET);
310 static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
311 va_list args, const char *trail)
316 r += fprintf(fp, "%s", color);
317 r += vfprintf(fp, fmt, args);
319 r += fprintf(fp, "%s", GIT_COLOR_RESET);
321 r += fprintf(fp, "%s", trail);
327 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
332 r = color_vfprintf(fp, color, fmt, args, NULL);
337 int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
342 r = color_vfprintf(fp, color, fmt, args, "\n");
347 int color_is_nil(const char *c)
349 return !strcmp(c, "NIL");