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 static int parse_color(const char *name
, int len
)
31 static const char * const color_names
[] = {
32 "normal", "black", "red", "green", "yellow",
33 "blue", "magenta", "cyan", "white"
37 for (i
= 0; i
< ARRAY_SIZE(color_names
); i
++) {
38 const char *str
= color_names
[i
];
39 if (!strncasecmp(name
, str
, len
) && !str
[len
])
42 i
= strtol(name
, &end
, 10);
43 if (end
- name
== len
&& i
>= -1 && i
<= 255)
48 static int parse_attr(const char *name
, int len
)
50 static const int attr_values
[] = { 1, 2, 4, 5, 7 };
51 static const char * const attr_names
[] = {
52 "bold", "dim", "ul", "blink", "reverse"
55 for (i
= 0; i
< ARRAY_SIZE(attr_names
); i
++) {
56 const char *str
= attr_names
[i
];
57 if (!strncasecmp(name
, str
, len
) && !str
[len
])
58 return attr_values
[i
];
63 void color_parse(const char *value
, const char *var
, char *dst
)
65 color_parse_mem(value
, strlen(value
), var
, dst
);
68 void color_parse_mem(const char *value
, int value_len
, const char *var
,
71 const char *ptr
= value
;
73 unsigned int attr
= 0;
77 if (!strncasecmp(value
, "reset", len
)) {
78 strcpy(dst
, GIT_COLOR_RESET
);
82 /* [fg [bg]] [attr]... */
84 const char *word
= ptr
;
87 while (len
> 0 && !isspace(word
[wordlen
])) {
93 while (len
> 0 && isspace(*ptr
)) {
98 val
= parse_color(word
, wordlen
);
110 val
= parse_attr(word
, wordlen
);
117 if (attr
|| fg
>= 0 || bg
>= 0) {
124 for (i
= 0; attr
; i
++) {
125 unsigned bit
= (1 << i
);
140 dst
+= sprintf(dst
, "38;5;%d", fg
);
150 dst
+= sprintf(dst
, "48;5;%d", bg
);
158 die("bad color value '%.*s' for variable '%s'", value_len
, value
, var
);
161 int git_config_colorbool(const char *var
, const char *value
)
164 if (!strcasecmp(value
, "never"))
166 if (!strcasecmp(value
, "always"))
168 if (!strcasecmp(value
, "auto"))
169 return GIT_COLOR_AUTO
;
175 /* Missing or explicit false to turn off colorization */
176 if (!git_config_bool(var
, value
))
179 /* any normal truth value defaults to 'auto' */
180 return GIT_COLOR_AUTO
;
183 static int check_auto_color(void)
185 if (color_stdout_is_tty
< 0)
186 color_stdout_is_tty
= isatty(1);
187 if (color_stdout_is_tty
|| (pager_in_use() && pager_use_color
)) {
188 char *term
= getenv("TERM");
189 if (term
&& strcmp(term
, "dumb"))
195 int want_color(int var
)
197 static int want_auto
= -1;
200 var
= git_use_color_default
;
202 if (var
== GIT_COLOR_AUTO
) {
204 want_auto
= check_auto_color();
210 int git_color_config(const char *var
, const char *value
, void *cb
)
212 if (!strcmp(var
, "color.ui")) {
213 git_use_color_default
= git_config_colorbool(var
, value
);
220 int git_color_default_config(const char *var
, const char *value
, void *cb
)
222 if (git_color_config(var
, value
, cb
) < 0)
225 return git_default_config(var
, value
, cb
);
228 void color_print_strbuf(FILE *fp
, const char *color
, const struct strbuf
*sb
)
231 fprintf(fp
, "%s", color
);
232 fprintf(fp
, "%s", sb
->buf
);
234 fprintf(fp
, "%s", GIT_COLOR_RESET
);
237 static int color_vfprintf(FILE *fp
, const char *color
, const char *fmt
,
238 va_list args
, const char *trail
)
243 r
+= fprintf(fp
, "%s", color
);
244 r
+= vfprintf(fp
, fmt
, args
);
246 r
+= fprintf(fp
, "%s", GIT_COLOR_RESET
);
248 r
+= fprintf(fp
, "%s", trail
);
254 int color_fprintf(FILE *fp
, const char *color
, const char *fmt
, ...)
259 r
= color_vfprintf(fp
, color
, fmt
, args
, NULL
);
264 int color_fprintf_ln(FILE *fp
, const char *color
, const char *fmt
, ...)
269 r
= color_vfprintf(fp
, color
, fmt
, args
, "\n");
274 int color_is_nil(const char *c
)
276 return !strcmp(c
, "NIL");