Skip to content
This repository
Newer
Older
100644 277 lines (242 sloc) 5.353 kb
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
1 #include "cache.h"
85023577 » Junio C Hamano
2006-12-19 simplify inclusion of system header files.
2 #include "color.h"
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
3
c9bfb953 » peff
2011-08-17 want_color: automatically fallback to color.ui
4 static int git_use_color_default = 0;
e269eb79 » peff
2011-08-17 git_config_colorbool: refactor stdout_is_tty handling
5 int color_stdout_is_tty = -1;
6b2f2d98 » matthiask
2008-02-18 Add color.ui variable which globally enables colorization if set
6
7cd52b5b » toofishes
2011-04-05 Share color list between graph and show-branch
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
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
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);
a0cf49c1 » tihirvon
2008-02-06 Fix parsing numeric color values
43 if (end - name == len && i >= -1 && i <= 255)
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
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 {
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
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 {
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
71 const char *ptr = value;
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
72 int len = value_len;
8b124135 » gitster
2010-02-27 color: allow multiple attributes
73 unsigned int attr = 0;
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
74 int fg = -2;
75 int bg = -2;
76
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
77 if (!strncasecmp(value, "reset", len)) {
dc6ebd4c » ArjenL
2009-02-13 Clean up use of ANSI color sequences
78 strcpy(dst, GIT_COLOR_RESET);
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
79 return;
80 }
81
8b124135 » gitster
2010-02-27 color: allow multiple attributes
82 /* [fg [bg]] [attr]... */
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
83 while (len > 0) {
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
84 const char *word = ptr;
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
85 int val, wordlen = 0;
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
86
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
87 while (len > 0 && !isspace(word[wordlen])) {
88 wordlen++;
89 len--;
90 }
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
91
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
92 ptr = word + wordlen;
93 while (len > 0 && isspace(*ptr)) {
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
94 ptr++;
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
95 len--;
96 }
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
97
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
98 val = parse_color(word, wordlen);
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
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 }
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
110 val = parse_attr(word, wordlen);
8b124135 » gitster
2010-02-27 color: allow multiple attributes
111 if (0 <= val)
112 attr |= (1 << val);
113 else
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
114 goto bad;
115 }
116
8b124135 » gitster
2010-02-27 color: allow multiple attributes
117 if (attr || fg >= 0 || bg >= 0) {
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
118 int sep = 0;
8b124135 » gitster
2010-02-27 color: allow multiple attributes
119 int i;
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
120
121 *dst++ = '\033';
122 *dst++ = '[';
8b124135 » gitster
2010-02-27 color: allow multiple attributes
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;
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
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:
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
158 die("bad color value '%.*s' for variable '%s'", value_len, value, var);
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
159 }
160
e269eb79 » peff
2011-08-17 git_config_colorbool: refactor stdout_is_tty handling
161 int git_config_colorbool(const char *var, const char *value)
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
162 {
57f2b842 » gitster
2007-11-26 "color.diff = true" is not "always" anymore.
163 if (value) {
164 if (!strcasecmp(value, "never"))
165 return 0;
166 if (!strcasecmp(value, "always"))
167 return 1;
168 if (!strcasecmp(value, "auto"))
daa0c3d9 » peff
2011-08-17 color: delay auto-color decision until point of use
169 return GIT_COLOR_AUTO;
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
170 }
57f2b842 » gitster
2007-11-26 "color.diff = true" is not "always" anymore.
171
73e9da01 » MarkLodato
2010-02-16 Add an optional argument for --color options
172 if (!var)
173 return -1;
174
57f2b842 » gitster
2007-11-26 "color.diff = true" is not "always" anymore.
175 /* Missing or explicit false to turn off colorization */
176 if (!git_config_bool(var, value))
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
177 return 0;
57f2b842 » gitster
2007-11-26 "color.diff = true" is not "always" anymore.
178
179 /* any normal truth value defaults to 'auto' */
daa0c3d9 » peff
2011-08-17 color: delay auto-color decision until point of use
180 return GIT_COLOR_AUTO;
181 }
182
183 static int check_auto_color(void)
184 {
e269eb79 » peff
2011-08-17 git_config_colorbool: refactor stdout_is_tty handling
185 if (color_stdout_is_tty < 0)
186 color_stdout_is_tty = isatty(1);
187 if (color_stdout_is_tty || (pager_in_use() && pager_use_color)) {
57f2b842 » gitster
2007-11-26 "color.diff = true" is not "always" anymore.
188 char *term = getenv("TERM");
189 if (term && strcmp(term, "dumb"))
190 return 1;
191 }
192 return 0;
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
193 }
194
daa0c3d9 » peff
2011-08-17 color: delay auto-color decision until point of use
195 int want_color(int var)
196 {
197 static int want_auto = -1;
198
c9bfb953 » peff
2011-08-17 want_color: automatically fallback to color.ui
199 if (var < 0)
200 var = git_use_color_default;
201
daa0c3d9 » peff
2011-08-17 color: delay auto-color decision until point of use
202 if (var == GIT_COLOR_AUTO) {
203 if (want_auto < 0)
204 want_auto = check_auto_color();
205 return want_auto;
206 }
c9bfb953 » peff
2011-08-17 want_color: automatically fallback to color.ui
207 return var;
daa0c3d9 » peff
2011-08-17 color: delay auto-color decision until point of use
208 }
209
3e1dd17a » peff
2011-08-17 diff: don't load color config in plumbing
210 int git_color_config(const char *var, const char *value, void *cb)
6b2f2d98 » matthiask
2008-02-18 Add color.ui variable which globally enables colorization if set
211 {
212 if (!strcmp(var, "color.ui")) {
e269eb79 » peff
2011-08-17 git_config_colorbool: refactor stdout_is_tty handling
213 git_use_color_default = git_config_colorbool(var, value);
6b2f2d98 » matthiask
2008-02-18 Add color.ui variable which globally enables colorization if set
214 return 0;
215 }
216
3e1dd17a » peff
2011-08-17 diff: don't load color config in plumbing
217 return 0;
218 }
219
220 int git_color_default_config(const char *var, const char *value, void *cb)
221 {
222 if (git_color_config(var, value, cb) < 0)
223 return -1;
224
ef90d6d4 » dscho
2008-05-14 Provide git_config with a callback-data parameter
225 return git_default_config(var, value, cb);
6b2f2d98 » matthiask
2008-02-18 Add color.ui variable which globally enables colorization if set
226 }
227
becbdae8 » jrn
2011-02-25 wt-status: add helpers for printing wt-status lines
228 void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
229 {
230 if (*color)
231 fprintf(fp, "%s", color);
232 fprintf(fp, "%s", sb->buf);
233 if (*color)
234 fprintf(fp, "%s", GIT_COLOR_RESET);
235 }
236
f26a0012 » Kristian Høgsberg
2007-09-17 Enable wt-status output to a given FILE pointer.
237 static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
238 va_list args, const char *trail)
239 {
240 int r = 0;
241
242 if (*color)
f26a0012 » Kristian Høgsberg
2007-09-17 Enable wt-status output to a given FILE pointer.
243 r += fprintf(fp, "%s", color);
244 r += vfprintf(fp, fmt, args);
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
245 if (*color)
dc6ebd4c » ArjenL
2009-02-13 Clean up use of ANSI color sequences
246 r += fprintf(fp, "%s", GIT_COLOR_RESET);
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
247 if (trail)
f26a0012 » Kristian Høgsberg
2007-09-17 Enable wt-status output to a given FILE pointer.
248 r += fprintf(fp, "%s", trail);
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
249 return r;
250 }
251
252
253
f26a0012 » Kristian Høgsberg
2007-09-17 Enable wt-status output to a given FILE pointer.
254 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
255 {
256 va_list args;
257 int r;
258 va_start(args, fmt);
f26a0012 » Kristian Høgsberg
2007-09-17 Enable wt-status output to a given FILE pointer.
259 r = color_vfprintf(fp, color, fmt, args, NULL);
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
260 va_end(args);
261 return r;
262 }
263
f26a0012 » Kristian Høgsberg
2007-09-17 Enable wt-status output to a given FILE pointer.
264 int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
265 {
266 va_list args;
267 int r;
268 va_start(args, fmt);
f26a0012 » Kristian Høgsberg
2007-09-17 Enable wt-status output to a given FILE pointer.
269 r = color_vfprintf(fp, color, fmt, args, "\n");
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
270 va_end(args);
271 return r;
272 }
148135fc » peff
2010-12-09 default color.status.branch to "same as header"
273
274 int color_is_nil(const char *c)
275 {
276 return !strcmp(c, "NIL");
277 }
Something went wrong with that request. Please try again.