4 int git_use_color_default = 0;
7 * The list of available column colors.
9 const char *column_colors_ansi[] = {
18 GIT_COLOR_BOLD_YELLOW,
20 GIT_COLOR_BOLD_MAGENTA,
25 /* Ignore the RESET at the end when giving the size */
26 const int column_colors_ansi_max = ARRAY_SIZE(column_colors_ansi) - 1;
28 static int parse_color(const char *name, int len)
30 static const char * const color_names[] = {
31 "normal", "black", "red", "green", "yellow",
32 "blue", "magenta", "cyan", "white"
36 for (i = 0; i < ARRAY_SIZE(color_names); i++) {
37 const char *str = color_names[i];
38 if (!strncasecmp(name, str, len) && !str[len])
41 i = strtol(name, &end, 10);
42 if (end - name == len && i >= -1 && i <= 255)
47 static int parse_attr(const char *name, int len)
49 static const int attr_values[] = { 1, 2, 4, 5, 7 };
50 static const char * const attr_names[] = {
51 "bold", "dim", "ul", "blink", "reverse"
54 for (i = 0; i < ARRAY_SIZE(attr_names); i++) {
55 const char *str = attr_names[i];
56 if (!strncasecmp(name, str, len) && !str[len])
57 return attr_values[i];
62 void color_parse(const char *value, const char *var, char *dst)
64 color_parse_mem(value, strlen(value), var, dst);
67 void color_parse_mem(const char *value, int value_len, const char *var,
70 const char *ptr = value;
72 unsigned int attr = 0;
76 if (!strncasecmp(value, "reset", len)) {
77 strcpy(dst, GIT_COLOR_RESET);
81 /* [fg [bg]] [attr]... */
83 const char *word = ptr;
86 while (len > 0 && !isspace(word[wordlen])) {
92 while (len > 0 && isspace(*ptr)) {
97 val = parse_color(word, wordlen);
109 val = parse_attr(word, wordlen);
116 if (attr || fg >= 0 || bg >= 0) {
123 for (i = 0; attr; i++) {
124 unsigned bit = (1 << i);
139 dst += sprintf(dst, "38;5;%d", fg);
149 dst += sprintf(dst, "48;5;%d", bg);
157 die("bad color value '%.*s' for variable '%s'", value_len, value, var);
160 int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
163 if (!strcasecmp(value, "never"))
165 if (!strcasecmp(value, "always"))
167 if (!strcasecmp(value, "auto"))
174 /* Missing or explicit false to turn off colorization */
175 if (!git_config_bool(var, value))
178 /* any normal truth value defaults to 'auto' */
180 if (stdout_is_tty < 0)
181 stdout_is_tty = isatty(1);
182 if (stdout_is_tty || (pager_in_use() && pager_use_color)) {
183 char *term = getenv("TERM");
184 if (term && strcmp(term, "dumb"))
190 int git_color_default_config(const char *var, const char *value, void *cb)
192 if (!strcmp(var, "color.ui")) {
193 git_use_color_default = git_config_colorbool(var, value, -1);
197 return git_default_config(var, value, cb);
200 void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
203 fprintf(fp, "%s", color);
204 fprintf(fp, "%s", sb->buf);
206 fprintf(fp, "%s", GIT_COLOR_RESET);
209 static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
210 va_list args, const char *trail)
215 r += fprintf(fp, "%s", color);
216 r += vfprintf(fp, fmt, args);
218 r += fprintf(fp, "%s", GIT_COLOR_RESET);
220 r += fprintf(fp, "%s", trail);
226 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
231 r = color_vfprintf(fp, color, fmt, args, NULL);
236 int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
241 r = color_vfprintf(fp, color, fmt, args, "\n");
246 int color_is_nil(const char *c)
248 return !strcmp(c, "NIL");