Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 204 lines (180 sloc) 4.021 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
4 #define COLOR_RESET "\033[m"
5
6b2f2d98 » matthiask
2008-02-18 Add color.ui variable which globally enables colorization if set
6 int git_use_color_default = 0;
7
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
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 » tihirvon
2008-02-06 Fix parsing numeric color values
22 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]
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)
43 {
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
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)
49 {
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
50 const char *ptr = value;
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
51 int len = value_len;
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
52 int attr = -1;
53 int fg = -2;
54 int bg = -2;
55
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
56 if (!strncasecmp(value, "reset", len)) {
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
57 strcpy(dst, "\033[m");
58 return;
59 }
60
61 /* [fg [bg]] [attr] */
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
62 while (len > 0) {
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
63 const char *word = ptr;
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
64 int val, wordlen = 0;
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
65
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
66 while (len > 0 && !isspace(word[wordlen])) {
67 wordlen++;
68 len--;
69 }
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
70
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
71 ptr = word + wordlen;
72 while (len > 0 && isspace(*ptr)) {
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
73 ptr++;
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
74 len--;
75 }
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
76
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
77 val = parse_color(word, wordlen);
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
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 » René Scharfe
2009-01-19 Optimize color_parse_mem
89 val = parse_attr(word, wordlen);
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
90 if (val < 0 || attr != -1)
91 goto bad;
92 attr = val;
93 }
94
95 if (attr >= 0 || fg >= 0 || bg >= 0) {
96 int sep = 0;
97
98 *dst++ = '\033';
99 *dst++ = '[';
100 if (attr >= 0) {
101 *dst++ = '0' + attr;
102 sep++;
103 }
104 if (fg >= 0) {
105 if (sep++)
106 *dst++ = ';';
107 if (fg < 8) {
108 *dst++ = '3';
109 *dst++ = '0' + fg;
110 } else {
111 dst += sprintf(dst, "38;5;%d", fg);
112 }
113 }
114 if (bg >= 0) {
115 if (sep++)
116 *dst++ = ';';
117 if (bg < 8) {
118 *dst++ = '4';
119 *dst++ = '0' + bg;
120 } else {
121 dst += sprintf(dst, "48;5;%d", bg);
122 }
123 }
124 *dst++ = 'm';
125 }
126 *dst = 0;
127 return;
128 bad:
2c2dc7c8 » René Scharfe
2009-01-19 Optimize color_parse_mem
129 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]
130 }
131
0f6f5a40 »
2007-12-05 git config --get-colorbool
132 int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
133 {
57f2b842 »
2007-11-26 "color.diff = true" is not "always" anymore.
134 if (value) {
135 if (!strcasecmp(value, "never"))
136 return 0;
137 if (!strcasecmp(value, "always"))
138 return 1;
139 if (!strcasecmp(value, "auto"))
140 goto auto_color;
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
141 }
57f2b842 »
2007-11-26 "color.diff = true" is not "always" anymore.
142
143 /* Missing or explicit false to turn off colorization */
144 if (!git_config_bool(var, value))
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
145 return 0;
57f2b842 »
2007-11-26 "color.diff = true" is not "always" anymore.
146
147 /* any normal truth value defaults to 'auto' */
148 auto_color:
0f6f5a40 »
2007-12-05 git config --get-colorbool
149 if (stdout_is_tty < 0)
150 stdout_is_tty = isatty(1);
6e9af863 » peff
2007-12-11 Support GIT_PAGER_IN_USE environment variable
151 if (stdout_is_tty || (pager_in_use() && pager_use_color)) {
57f2b842 »
2007-11-26 "color.diff = true" is not "always" anymore.
152 char *term = getenv("TERM");
153 if (term && strcmp(term, "dumb"))
154 return 1;
155 }
156 return 0;
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
157 }
158
ef90d6d4 » dscho
2008-05-14 Provide git_config with a callback-data parameter
159 int git_color_default_config(const char *var, const char *value, void *cb)
6b2f2d98 » matthiask
2008-02-18 Add color.ui variable which globally enables colorization if set
160 {
161 if (!strcmp(var, "color.ui")) {
162 git_use_color_default = git_config_colorbool(var, value, -1);
163 return 0;
164 }
165
ef90d6d4 » dscho
2008-05-14 Provide git_config with a callback-data parameter
166 return git_default_config(var, value, cb);
6b2f2d98 » matthiask
2008-02-18 Add color.ui variable which globally enables colorization if set
167 }
168
f26a0012 » Kristian Høgsberg
2007-09-17 Enable wt-status output to a given FILE pointer.
169 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]
170 va_list args, const char *trail)
171 {
172 int r = 0;
173
174 if (*color)
f26a0012 » Kristian Høgsberg
2007-09-17 Enable wt-status output to a given FILE pointer.
175 r += fprintf(fp, "%s", color);
176 r += vfprintf(fp, fmt, args);
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
177 if (*color)
f26a0012 » Kristian Høgsberg
2007-09-17 Enable wt-status output to a given FILE pointer.
178 r += fprintf(fp, "%s", COLOR_RESET);
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
179 if (trail)
f26a0012 » Kristian Høgsberg
2007-09-17 Enable wt-status output to a given FILE pointer.
180 r += fprintf(fp, "%s", trail);
7c92fe0e » peff
2006-09-08 Move color option parsing out of diff.c and into color.[ch]
181 return r;
182 }
183
184
185
f26a0012 » Kristian Høgsberg
2007-09-17 Enable wt-status output to a given FILE pointer.
186 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]
187 {
188 va_list args;
189 int r;
190 va_start(args, fmt);
f26a0012 » Kristian Høgsberg
2007-09-17 Enable wt-status output to a given FILE pointer.
191 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]
192 va_end(args);
193 return r;
194 }
195
f26a0012 » Kristian Høgsberg
2007-09-17 Enable wt-status output to a given FILE pointer.
196 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]
197 {
198 va_list args;
199 int r;
200 va_start(args, fmt);
f26a0012 » Kristian Høgsberg
2007-09-17 Enable wt-status output to a given FILE pointer.
201 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]
202 va_end(args);
203 return r;
204 }
Something went wrong with that request. Please try again.