Git 1.7.0
[git/git.git] / color.c
CommitLineData
7c92fe0e 1#include "cache.h"
85023577 2#include "color.h"
7c92fe0e 3
6b2f2d98
MK
4int git_use_color_default = 0;
5
7c92fe0e
JK
6static int parse_color(const char *name, int len)
7{
8 static const char * const color_names[] = {
9 "normal", "black", "red", "green", "yellow",
10 "blue", "magenta", "cyan", "white"
11 };
12 char *end;
13 int i;
14 for (i = 0; i < ARRAY_SIZE(color_names); i++) {
15 const char *str = color_names[i];
16 if (!strncasecmp(name, str, len) && !str[len])
17 return i - 1;
18 }
19 i = strtol(name, &end, 10);
a0cf49c1 20 if (end - name == len && i >= -1 && i <= 255)
7c92fe0e
JK
21 return i;
22 return -2;
23}
24
25static int parse_attr(const char *name, int len)
26{
27 static const int attr_values[] = { 1, 2, 4, 5, 7 };
28 static const char * const attr_names[] = {
29 "bold", "dim", "ul", "blink", "reverse"
30 };
31 int i;
32 for (i = 0; i < ARRAY_SIZE(attr_names); i++) {
33 const char *str = attr_names[i];
34 if (!strncasecmp(name, str, len) && !str[len])
35 return attr_values[i];
36 }
37 return -1;
38}
39
40void color_parse(const char *value, const char *var, char *dst)
2c2dc7c8
RS
41{
42 color_parse_mem(value, strlen(value), var, dst);
43}
44
45void color_parse_mem(const char *value, int value_len, const char *var,
46 char *dst)
7c92fe0e
JK
47{
48 const char *ptr = value;
2c2dc7c8 49 int len = value_len;
7c92fe0e
JK
50 int attr = -1;
51 int fg = -2;
52 int bg = -2;
53
2c2dc7c8 54 if (!strncasecmp(value, "reset", len)) {
dc6ebd4c 55 strcpy(dst, GIT_COLOR_RESET);
7c92fe0e
JK
56 return;
57 }
58
59 /* [fg [bg]] [attr] */
2c2dc7c8 60 while (len > 0) {
7c92fe0e 61 const char *word = ptr;
2c2dc7c8 62 int val, wordlen = 0;
7c92fe0e 63
2c2dc7c8
RS
64 while (len > 0 && !isspace(word[wordlen])) {
65 wordlen++;
66 len--;
67 }
7c92fe0e 68
2c2dc7c8
RS
69 ptr = word + wordlen;
70 while (len > 0 && isspace(*ptr)) {
7c92fe0e 71 ptr++;
2c2dc7c8
RS
72 len--;
73 }
7c92fe0e 74
2c2dc7c8 75 val = parse_color(word, wordlen);
7c92fe0e
JK
76 if (val >= -1) {
77 if (fg == -2) {
78 fg = val;
79 continue;
80 }
81 if (bg == -2) {
82 bg = val;
83 continue;
84 }
85 goto bad;
86 }
2c2dc7c8 87 val = parse_attr(word, wordlen);
7c92fe0e
JK
88 if (val < 0 || attr != -1)
89 goto bad;
90 attr = val;
91 }
92
93 if (attr >= 0 || fg >= 0 || bg >= 0) {
94 int sep = 0;
95
96 *dst++ = '\033';
97 *dst++ = '[';
98 if (attr >= 0) {
99 *dst++ = '0' + attr;
100 sep++;
101 }
102 if (fg >= 0) {
103 if (sep++)
104 *dst++ = ';';
105 if (fg < 8) {
106 *dst++ = '3';
107 *dst++ = '0' + fg;
108 } else {
109 dst += sprintf(dst, "38;5;%d", fg);
110 }
111 }
112 if (bg >= 0) {
113 if (sep++)
114 *dst++ = ';';
115 if (bg < 8) {
116 *dst++ = '4';
117 *dst++ = '0' + bg;
118 } else {
119 dst += sprintf(dst, "48;5;%d", bg);
120 }
121 }
122 *dst++ = 'm';
123 }
124 *dst = 0;
125 return;
126bad:
2c2dc7c8 127 die("bad color value '%.*s' for variable '%s'", value_len, value, var);
7c92fe0e
JK
128}
129
0f6f5a40 130int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
7c92fe0e 131{
57f2b842
JH
132 if (value) {
133 if (!strcasecmp(value, "never"))
134 return 0;
135 if (!strcasecmp(value, "always"))
136 return 1;
137 if (!strcasecmp(value, "auto"))
138 goto auto_color;
7c92fe0e 139 }
57f2b842
JH
140
141 /* Missing or explicit false to turn off colorization */
142 if (!git_config_bool(var, value))
7c92fe0e 143 return 0;
57f2b842
JH
144
145 /* any normal truth value defaults to 'auto' */
146 auto_color:
0f6f5a40
JH
147 if (stdout_is_tty < 0)
148 stdout_is_tty = isatty(1);
6e9af863 149 if (stdout_is_tty || (pager_in_use() && pager_use_color)) {
57f2b842
JH
150 char *term = getenv("TERM");
151 if (term && strcmp(term, "dumb"))
152 return 1;
153 }
154 return 0;
7c92fe0e
JK
155}
156
ef90d6d4 157int git_color_default_config(const char *var, const char *value, void *cb)
6b2f2d98
MK
158{
159 if (!strcmp(var, "color.ui")) {
160 git_use_color_default = git_config_colorbool(var, value, -1);
161 return 0;
162 }
163
ef90d6d4 164 return git_default_config(var, value, cb);
6b2f2d98
MK
165}
166
f26a0012 167static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
7c92fe0e
JK
168 va_list args, const char *trail)
169{
170 int r = 0;
171
172 if (*color)
f26a0012
KH
173 r += fprintf(fp, "%s", color);
174 r += vfprintf(fp, fmt, args);
7c92fe0e 175 if (*color)
dc6ebd4c 176 r += fprintf(fp, "%s", GIT_COLOR_RESET);
7c92fe0e 177 if (trail)
f26a0012 178 r += fprintf(fp, "%s", trail);
7c92fe0e
JK
179 return r;
180}
181
182
183
f26a0012 184int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
7c92fe0e
JK
185{
186 va_list args;
187 int r;
188 va_start(args, fmt);
f26a0012 189 r = color_vfprintf(fp, color, fmt, args, NULL);
7c92fe0e
JK
190 va_end(args);
191 return r;
192}
193
f26a0012 194int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
7c92fe0e
JK
195{
196 va_list args;
197 int r;
198 va_start(args, fmt);
f26a0012 199 r = color_vfprintf(fp, color, fmt, args, "\n");
7c92fe0e
JK
200 va_end(args);
201 return r;
202}
07b57e90
JS
203
204/*
205 * This function splits the buffer by newlines and colors the lines individually.
206 *
207 * Returns 0 on success.
208 */
209int color_fwrite_lines(FILE *fp, const char *color,
210 size_t count, const char *buf)
211{
212 if (!*color)
213 return fwrite(buf, count, 1, fp) != 1;
214 while (count) {
215 char *p = memchr(buf, '\n', count);
216 if (p != buf && (fputs(color, fp) < 0 ||
217 fwrite(buf, p ? p - buf : count, 1, fp) != 1 ||
dc6ebd4c 218 fputs(GIT_COLOR_RESET, fp) < 0))
07b57e90
JS
219 return -1;
220 if (!p)
221 return 0;
222 if (fputc('\n', fp) < 0)
223 return -1;
224 count -= p + 1 - buf;
225 buf = p + 1;
226 }
227 return 0;
228}
229
230