]> git.ipfire.org Git - thirdparty/git.git/blame - color.c
l10n: vi.po: Update new message strings
[thirdparty/git.git] / color.c
CommitLineData
7c92fe0e 1#include "cache.h"
85023577 2#include "color.h"
7c92fe0e 3
4c7f1819 4static int git_use_color_default = GIT_COLOR_AUTO;
e269eb79 5int color_stdout_is_tty = -1;
6b2f2d98 6
7cd52b5b
DM
7/*
8 * The list of available column colors.
9 */
10const 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 */
27const int column_colors_ansi_max = ARRAY_SIZE(column_colors_ansi) - 1;
28
7c92fe0e
JK
29static 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);
a0cf49c1 43 if (end - name == len && i >= -1 && i <= 255)
7c92fe0e
JK
44 return i;
45 return -2;
46}
47
48static 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
f6c5a296 63int color_parse(const char *value, char *dst)
2c2dc7c8 64{
f6c5a296 65 return color_parse_mem(value, strlen(value), dst);
2c2dc7c8
RS
66}
67
f6c5a296 68int color_parse_mem(const char *value, int value_len, char *dst)
7c92fe0e
JK
69{
70 const char *ptr = value;
2c2dc7c8 71 int len = value_len;
8b124135 72 unsigned int attr = 0;
7c92fe0e
JK
73 int fg = -2;
74 int bg = -2;
75
2c2dc7c8 76 if (!strncasecmp(value, "reset", len)) {
dc6ebd4c 77 strcpy(dst, GIT_COLOR_RESET);
f6c5a296 78 return 0;
7c92fe0e
JK
79 }
80
8b124135 81 /* [fg [bg]] [attr]... */
2c2dc7c8 82 while (len > 0) {
7c92fe0e 83 const char *word = ptr;
2c2dc7c8 84 int val, wordlen = 0;
7c92fe0e 85
2c2dc7c8
RS
86 while (len > 0 && !isspace(word[wordlen])) {
87 wordlen++;
88 len--;
89 }
7c92fe0e 90
2c2dc7c8
RS
91 ptr = word + wordlen;
92 while (len > 0 && isspace(*ptr)) {
7c92fe0e 93 ptr++;
2c2dc7c8
RS
94 len--;
95 }
7c92fe0e 96
2c2dc7c8 97 val = parse_color(word, wordlen);
7c92fe0e
JK
98 if (val >= -1) {
99 if (fg == -2) {
100 fg = val;
101 continue;
102 }
103 if (bg == -2) {
104 bg = val;
105 continue;
106 }
107 goto bad;
108 }
2c2dc7c8 109 val = parse_attr(word, wordlen);
8b124135
JH
110 if (0 <= val)
111 attr |= (1 << val);
112 else
7c92fe0e 113 goto bad;
7c92fe0e
JK
114 }
115
8b124135 116 if (attr || fg >= 0 || bg >= 0) {
7c92fe0e 117 int sep = 0;
8b124135 118 int i;
7c92fe0e
JK
119
120 *dst++ = '\033';
121 *dst++ = '[';
8b124135
JH
122
123 for (i = 0; attr; i++) {
124 unsigned bit = (1 << i);
125 if (!(attr & bit))
126 continue;
127 attr &= ~bit;
128 if (sep++)
129 *dst++ = ';';
130 *dst++ = '0' + i;
7c92fe0e
JK
131 }
132 if (fg >= 0) {
133 if (sep++)
134 *dst++ = ';';
135 if (fg < 8) {
136 *dst++ = '3';
137 *dst++ = '0' + fg;
138 } else {
139 dst += sprintf(dst, "38;5;%d", fg);
140 }
141 }
142 if (bg >= 0) {
143 if (sep++)
144 *dst++ = ';';
145 if (bg < 8) {
146 *dst++ = '4';
147 *dst++ = '0' + bg;
148 } else {
149 dst += sprintf(dst, "48;5;%d", bg);
150 }
151 }
152 *dst++ = 'm';
153 }
154 *dst = 0;
f6c5a296 155 return 0;
7c92fe0e 156bad:
f6c5a296 157 return error(_("invalid color value: %.*s"), value_len, value);
7c92fe0e
JK
158}
159
e269eb79 160int git_config_colorbool(const char *var, const char *value)
7c92fe0e 161{
57f2b842
JH
162 if (value) {
163 if (!strcasecmp(value, "never"))
164 return 0;
165 if (!strcasecmp(value, "always"))
166 return 1;
167 if (!strcasecmp(value, "auto"))
daa0c3d9 168 return GIT_COLOR_AUTO;
7c92fe0e 169 }
57f2b842 170
73e9da01
ML
171 if (!var)
172 return -1;
173
57f2b842
JH
174 /* Missing or explicit false to turn off colorization */
175 if (!git_config_bool(var, value))
7c92fe0e 176 return 0;
57f2b842
JH
177
178 /* any normal truth value defaults to 'auto' */
daa0c3d9
JK
179 return GIT_COLOR_AUTO;
180}
181
182static int check_auto_color(void)
183{
e269eb79
JK
184 if (color_stdout_is_tty < 0)
185 color_stdout_is_tty = isatty(1);
186 if (color_stdout_is_tty || (pager_in_use() && pager_use_color)) {
57f2b842
JH
187 char *term = getenv("TERM");
188 if (term && strcmp(term, "dumb"))
189 return 1;
190 }
191 return 0;
7c92fe0e
JK
192}
193
daa0c3d9
JK
194int want_color(int var)
195{
196 static int want_auto = -1;
197
c9bfb953
JK
198 if (var < 0)
199 var = git_use_color_default;
200
daa0c3d9
JK
201 if (var == GIT_COLOR_AUTO) {
202 if (want_auto < 0)
203 want_auto = check_auto_color();
204 return want_auto;
205 }
c9bfb953 206 return var;
daa0c3d9
JK
207}
208
3e1dd17a 209int git_color_config(const char *var, const char *value, void *cb)
6b2f2d98
MK
210{
211 if (!strcmp(var, "color.ui")) {
e269eb79 212 git_use_color_default = git_config_colorbool(var, value);
6b2f2d98
MK
213 return 0;
214 }
215
3e1dd17a
JK
216 return 0;
217}
218
219int git_color_default_config(const char *var, const char *value, void *cb)
220{
221 if (git_color_config(var, value, cb) < 0)
222 return -1;
223
ef90d6d4 224 return git_default_config(var, value, cb);
6b2f2d98
MK
225}
226
becbdae8
JN
227void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
228{
229 if (*color)
230 fprintf(fp, "%s", color);
231 fprintf(fp, "%s", sb->buf);
232 if (*color)
233 fprintf(fp, "%s", GIT_COLOR_RESET);
234}
235
f26a0012 236static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
7c92fe0e
JK
237 va_list args, const char *trail)
238{
239 int r = 0;
240
241 if (*color)
f26a0012
KH
242 r += fprintf(fp, "%s", color);
243 r += vfprintf(fp, fmt, args);
7c92fe0e 244 if (*color)
dc6ebd4c 245 r += fprintf(fp, "%s", GIT_COLOR_RESET);
7c92fe0e 246 if (trail)
f26a0012 247 r += fprintf(fp, "%s", trail);
7c92fe0e
JK
248 return r;
249}
250
251
252
f26a0012 253int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
7c92fe0e
JK
254{
255 va_list args;
256 int r;
257 va_start(args, fmt);
f26a0012 258 r = color_vfprintf(fp, color, fmt, args, NULL);
7c92fe0e
JK
259 va_end(args);
260 return r;
261}
262
f26a0012 263int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
7c92fe0e
JK
264{
265 va_list args;
266 int r;
267 va_start(args, fmt);
f26a0012 268 r = color_vfprintf(fp, color, fmt, args, "\n");
7c92fe0e
JK
269 va_end(args);
270 return r;
271}
148135fc
JK
272
273int color_is_nil(const char *c)
274{
275 return !strcmp(c, "NIL");
276}