]> git.ipfire.org Git - thirdparty/git.git/blame - color.c
Documentation/git-archive: spell --worktree-attributes correctly
[thirdparty/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;
8b124135 50 unsigned int attr = 0;
7c92fe0e
JK
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
8b124135 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);
8b124135
JH
88 if (0 <= val)
89 attr |= (1 << val);
90 else
7c92fe0e 91 goto bad;
7c92fe0e
JK
92 }
93
8b124135 94 if (attr || fg >= 0 || bg >= 0) {
7c92fe0e 95 int sep = 0;
8b124135 96 int i;
7c92fe0e
JK
97
98 *dst++ = '\033';
99 *dst++ = '[';
8b124135
JH
100
101 for (i = 0; attr; i++) {
102 unsigned bit = (1 << i);
103 if (!(attr & bit))
104 continue;
105 attr &= ~bit;
106 if (sep++)
107 *dst++ = ';';
108 *dst++ = '0' + i;
7c92fe0e
JK
109 }
110 if (fg >= 0) {
111 if (sep++)
112 *dst++ = ';';
113 if (fg < 8) {
114 *dst++ = '3';
115 *dst++ = '0' + fg;
116 } else {
117 dst += sprintf(dst, "38;5;%d", fg);
118 }
119 }
120 if (bg >= 0) {
121 if (sep++)
122 *dst++ = ';';
123 if (bg < 8) {
124 *dst++ = '4';
125 *dst++ = '0' + bg;
126 } else {
127 dst += sprintf(dst, "48;5;%d", bg);
128 }
129 }
130 *dst++ = 'm';
131 }
132 *dst = 0;
133 return;
134bad:
2c2dc7c8 135 die("bad color value '%.*s' for variable '%s'", value_len, value, var);
7c92fe0e
JK
136}
137
0f6f5a40 138int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
7c92fe0e 139{
57f2b842
JH
140 if (value) {
141 if (!strcasecmp(value, "never"))
142 return 0;
143 if (!strcasecmp(value, "always"))
144 return 1;
145 if (!strcasecmp(value, "auto"))
146 goto auto_color;
7c92fe0e 147 }
57f2b842
JH
148
149 /* Missing or explicit false to turn off colorization */
150 if (!git_config_bool(var, value))
7c92fe0e 151 return 0;
57f2b842
JH
152
153 /* any normal truth value defaults to 'auto' */
154 auto_color:
0f6f5a40
JH
155 if (stdout_is_tty < 0)
156 stdout_is_tty = isatty(1);
6e9af863 157 if (stdout_is_tty || (pager_in_use() && pager_use_color)) {
57f2b842
JH
158 char *term = getenv("TERM");
159 if (term && strcmp(term, "dumb"))
160 return 1;
161 }
162 return 0;
7c92fe0e
JK
163}
164
ef90d6d4 165int git_color_default_config(const char *var, const char *value, void *cb)
6b2f2d98
MK
166{
167 if (!strcmp(var, "color.ui")) {
168 git_use_color_default = git_config_colorbool(var, value, -1);
169 return 0;
170 }
171
ef90d6d4 172 return git_default_config(var, value, cb);
6b2f2d98
MK
173}
174
f26a0012 175static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
7c92fe0e
JK
176 va_list args, const char *trail)
177{
178 int r = 0;
179
180 if (*color)
f26a0012
KH
181 r += fprintf(fp, "%s", color);
182 r += vfprintf(fp, fmt, args);
7c92fe0e 183 if (*color)
dc6ebd4c 184 r += fprintf(fp, "%s", GIT_COLOR_RESET);
7c92fe0e 185 if (trail)
f26a0012 186 r += fprintf(fp, "%s", trail);
7c92fe0e
JK
187 return r;
188}
189
190
191
f26a0012 192int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
7c92fe0e
JK
193{
194 va_list args;
195 int r;
196 va_start(args, fmt);
f26a0012 197 r = color_vfprintf(fp, color, fmt, args, NULL);
7c92fe0e
JK
198 va_end(args);
199 return r;
200}
201
f26a0012 202int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
7c92fe0e
JK
203{
204 va_list args;
205 int r;
206 va_start(args, fmt);
f26a0012 207 r = color_vfprintf(fp, color, fmt, args, "\n");
7c92fe0e
JK
208 va_end(args);
209 return r;
210}
07b57e90
JS
211
212/*
213 * This function splits the buffer by newlines and colors the lines individually.
214 *
215 * Returns 0 on success.
216 */
217int color_fwrite_lines(FILE *fp, const char *color,
218 size_t count, const char *buf)
219{
220 if (!*color)
221 return fwrite(buf, count, 1, fp) != 1;
222 while (count) {
223 char *p = memchr(buf, '\n', count);
224 if (p != buf && (fputs(color, fp) < 0 ||
225 fwrite(buf, p ? p - buf : count, 1, fp) != 1 ||
dc6ebd4c 226 fputs(GIT_COLOR_RESET, fp) < 0))
07b57e90
JS
227 return -1;
228 if (!p)
229 return 0;
230 if (fputc('\n', fp) < 0)
231 return -1;
232 count -= p + 1 - buf;
233 buf = p + 1;
234 }
235 return 0;
236}
237
238