4 #define COLOR_RESET "\033[m"
6 int git_use_color_default = 0;
8 static int parse_color(const char *name, int len)
10 static const char * const color_names[] = {
11 "normal", "black", "red", "green", "yellow",
12 "blue", "magenta", "cyan", "white"
16 for (i = 0; i < ARRAY_SIZE(color_names); i++) {
17 const char *str = color_names[i];
18 if (!strncasecmp(name, str, len) && !str[len])
21 i = strtol(name, &end, 10);
22 if (end - name == len && i >= -1 && i <= 255)
27 static int parse_attr(const char *name, int len)
29 static const int attr_values[] = { 1, 2, 4, 5, 7 };
30 static const char * const attr_names[] = {
31 "bold", "dim", "ul", "blink", "reverse"
34 for (i = 0; i < ARRAY_SIZE(attr_names); i++) {
35 const char *str = attr_names[i];
36 if (!strncasecmp(name, str, len) && !str[len])
37 return attr_values[i];
42 void color_parse(const char *value, const char *var, char *dst)
44 color_parse_mem(value, strlen(value), var, dst);
47 void color_parse_mem(const char *value, int value_len, const char *var,
50 const char *ptr = value;
56 if (!strncasecmp(value, "reset", len)) {
57 strcpy(dst, "\033[m");
61 /* [fg [bg]] [attr] */
63 const char *word = ptr;
66 while (len > 0 && !isspace(word[wordlen])) {
72 while (len > 0 && isspace(*ptr)) {
77 val = parse_color(word, wordlen);
89 val = parse_attr(word, wordlen);
90 if (val < 0 || attr != -1)
95 if (attr >= 0 || fg >= 0 || bg >= 0) {
111 dst += sprintf(dst, "38;5;%d", fg);
121 dst += sprintf(dst, "48;5;%d", bg);
129 die("bad color value '%.*s' for variable '%s'", value_len, value, var);
132 int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
135 if (!strcasecmp(value, "never"))
137 if (!strcasecmp(value, "always"))
139 if (!strcasecmp(value, "auto"))
143 /* Missing or explicit false to turn off colorization */
144 if (!git_config_bool(var, value))
147 /* any normal truth value defaults to 'auto' */
149 if (stdout_is_tty < 0)
150 stdout_is_tty = isatty(1);
151 if (stdout_is_tty || (pager_in_use() && pager_use_color)) {
152 char *term = getenv("TERM");
153 if (term && strcmp(term, "dumb"))
159 int git_color_default_config(const char *var, const char *value, void *cb)
161 if (!strcmp(var, "color.ui")) {
162 git_use_color_default = git_config_colorbool(var, value, -1);
166 return git_default_config(var, value, cb);
169 static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
170 va_list args, const char *trail)
175 r += fprintf(fp, "%s", color);
176 r += vfprintf(fp, fmt, args);
178 r += fprintf(fp, "%s", COLOR_RESET);
180 r += fprintf(fp, "%s", trail);
186 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
191 r = color_vfprintf(fp, color, fmt, args, NULL);
196 int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
201 r = color_vfprintf(fp, color, fmt, args, "\n");
207 * This function splits the buffer by newlines and colors the lines individually.
209 * Returns 0 on success.
211 int color_fwrite_lines(FILE *fp, const char *color,
212 size_t count, const char *buf)
215 return fwrite(buf, count, 1, fp) != 1;
217 char *p = memchr(buf, '\n', count);
218 if (p != buf && (fputs(color, fp) < 0 ||
219 fwrite(buf, p ? p - buf : count, 1, fp) != 1 ||
220 fputs(COLOR_RESET, fp) < 0))
224 if (fputc('\n', fp) < 0)
226 count -= p + 1 - buf;