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