67affa4765cff33186766143f935f7f7ec3ec91c
[git/git.git] / color.c
1 #include "cache.h"
2 #include "color.h"
3
4 int git_use_color_default = 0;
5 int color_stdout_is_tty = -1;
6
7 /*
8 * The list of available column colors.
9 */
10 const char *column_colors_ansi[] = {
11 GIT_COLOR_RED,
12 GIT_COLOR_GREEN,
13 GIT_COLOR_YELLOW,
14 GIT_COLOR_BLUE,
15 GIT_COLOR_MAGENTA,
16 GIT_COLOR_CYAN,
17 GIT_COLOR_BOLD_RED,
18 GIT_COLOR_BOLD_GREEN,
19 GIT_COLOR_BOLD_YELLOW,
20 GIT_COLOR_BOLD_BLUE,
21 GIT_COLOR_BOLD_MAGENTA,
22 GIT_COLOR_BOLD_CYAN,
23 GIT_COLOR_RESET,
24 };
25
26 /* Ignore the RESET at the end when giving the size */
27 const int column_colors_ansi_max = ARRAY_SIZE(column_colors_ansi) - 1;
28
29 static int parse_color(const char *name, int len)
30 {
31 static const char * const color_names[] = {
32 "normal", "black", "red", "green", "yellow",
33 "blue", "magenta", "cyan", "white"
34 };
35 char *end;
36 int i;
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])
40 return i - 1;
41 }
42 i = strtol(name, &end, 10);
43 if (end - name == len && i >= -1 && i <= 255)
44 return i;
45 return -2;
46 }
47
48 static int parse_attr(const char *name, int len)
49 {
50 static const int attr_values[] = { 1, 2, 4, 5, 7 };
51 static const char * const attr_names[] = {
52 "bold", "dim", "ul", "blink", "reverse"
53 };
54 int i;
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];
59 }
60 return -1;
61 }
62
63 void color_parse(const char *value, const char *var, char *dst)
64 {
65 color_parse_mem(value, strlen(value), var, dst);
66 }
67
68 void color_parse_mem(const char *value, int value_len, const char *var,
69 char *dst)
70 {
71 const char *ptr = value;
72 int len = value_len;
73 unsigned int attr = 0;
74 int fg = -2;
75 int bg = -2;
76
77 if (!strncasecmp(value, "reset", len)) {
78 strcpy(dst, GIT_COLOR_RESET);
79 return;
80 }
81
82 /* [fg [bg]] [attr]... */
83 while (len > 0) {
84 const char *word = ptr;
85 int val, wordlen = 0;
86
87 while (len > 0 && !isspace(word[wordlen])) {
88 wordlen++;
89 len--;
90 }
91
92 ptr = word + wordlen;
93 while (len > 0 && isspace(*ptr)) {
94 ptr++;
95 len--;
96 }
97
98 val = parse_color(word, wordlen);
99 if (val >= -1) {
100 if (fg == -2) {
101 fg = val;
102 continue;
103 }
104 if (bg == -2) {
105 bg = val;
106 continue;
107 }
108 goto bad;
109 }
110 val = parse_attr(word, wordlen);
111 if (0 <= val)
112 attr |= (1 << val);
113 else
114 goto bad;
115 }
116
117 if (attr || fg >= 0 || bg >= 0) {
118 int sep = 0;
119 int i;
120
121 *dst++ = '\033';
122 *dst++ = '[';
123
124 for (i = 0; attr; i++) {
125 unsigned bit = (1 << i);
126 if (!(attr & bit))
127 continue;
128 attr &= ~bit;
129 if (sep++)
130 *dst++ = ';';
131 *dst++ = '0' + i;
132 }
133 if (fg >= 0) {
134 if (sep++)
135 *dst++ = ';';
136 if (fg < 8) {
137 *dst++ = '3';
138 *dst++ = '0' + fg;
139 } else {
140 dst += sprintf(dst, "38;5;%d", fg);
141 }
142 }
143 if (bg >= 0) {
144 if (sep++)
145 *dst++ = ';';
146 if (bg < 8) {
147 *dst++ = '4';
148 *dst++ = '0' + bg;
149 } else {
150 dst += sprintf(dst, "48;5;%d", bg);
151 }
152 }
153 *dst++ = 'm';
154 }
155 *dst = 0;
156 return;
157 bad:
158 die("bad color value '%.*s' for variable '%s'", value_len, value, var);
159 }
160
161 int git_config_colorbool(const char *var, const char *value)
162 {
163 if (value) {
164 if (!strcasecmp(value, "never"))
165 return 0;
166 if (!strcasecmp(value, "always"))
167 return 1;
168 if (!strcasecmp(value, "auto"))
169 goto auto_color;
170 }
171
172 if (!var)
173 return -1;
174
175 /* Missing or explicit false to turn off colorization */
176 if (!git_config_bool(var, value))
177 return 0;
178
179 /* any normal truth value defaults to 'auto' */
180 auto_color:
181 if (color_stdout_is_tty < 0)
182 color_stdout_is_tty = isatty(1);
183 if (color_stdout_is_tty || (pager_in_use() && pager_use_color)) {
184 char *term = getenv("TERM");
185 if (term && strcmp(term, "dumb"))
186 return 1;
187 }
188 return 0;
189 }
190
191 int git_color_default_config(const char *var, const char *value, void *cb)
192 {
193 if (!strcmp(var, "color.ui")) {
194 git_use_color_default = git_config_colorbool(var, value);
195 return 0;
196 }
197
198 return git_default_config(var, value, cb);
199 }
200
201 void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
202 {
203 if (*color)
204 fprintf(fp, "%s", color);
205 fprintf(fp, "%s", sb->buf);
206 if (*color)
207 fprintf(fp, "%s", GIT_COLOR_RESET);
208 }
209
210 static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
211 va_list args, const char *trail)
212 {
213 int r = 0;
214
215 if (*color)
216 r += fprintf(fp, "%s", color);
217 r += vfprintf(fp, fmt, args);
218 if (*color)
219 r += fprintf(fp, "%s", GIT_COLOR_RESET);
220 if (trail)
221 r += fprintf(fp, "%s", trail);
222 return r;
223 }
224
225
226
227 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
228 {
229 va_list args;
230 int r;
231 va_start(args, fmt);
232 r = color_vfprintf(fp, color, fmt, args, NULL);
233 va_end(args);
234 return r;
235 }
236
237 int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
238 {
239 va_list args;
240 int r;
241 va_start(args, fmt);
242 r = color_vfprintf(fp, color, fmt, args, "\n");
243 va_end(args);
244 return r;
245 }
246
247 int color_is_nil(const char *c)
248 {
249 return !strcmp(c, "NIL");
250 }