Make templates honour SHELL_PATH and PERL_PATH
[git/jnareb-git.git] / color.c
blob8f07fc9547efdf37cf44ab0b880f6c26970b3e95
1 #include "cache.h"
2 #include "color.h"
4 int git_use_color_default = 0;
6 static int parse_color(const char *name, int len)
8 static const char * const color_names[] = {
9 "normal", "black", "red", "green", "yellow",
10 "blue", "magenta", "cyan", "white"
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;
19 i = strtol(name, &end, 10);
20 if (end - name == len && i >= -1 && i <= 255)
21 return i;
22 return -2;
25 static int parse_attr(const char *name, int len)
27 static const int attr_values[] = { 1, 2, 4, 5, 7 };
28 static const char * const attr_names[] = {
29 "bold", "dim", "ul", "blink", "reverse"
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];
37 return -1;
40 void color_parse(const char *value, const char *var, char *dst)
42 color_parse_mem(value, strlen(value), var, dst);
45 void color_parse_mem(const char *value, int value_len, const char *var,
46 char *dst)
48 const char *ptr = value;
49 int len = value_len;
50 int attr = -1;
51 int fg = -2;
52 int bg = -2;
54 if (!strncasecmp(value, "reset", len)) {
55 strcpy(dst, GIT_COLOR_RESET);
56 return;
59 /* [fg [bg]] [attr] */
60 while (len > 0) {
61 const char *word = ptr;
62 int val, wordlen = 0;
64 while (len > 0 && !isspace(word[wordlen])) {
65 wordlen++;
66 len--;
69 ptr = word + wordlen;
70 while (len > 0 && isspace(*ptr)) {
71 ptr++;
72 len--;
75 val = parse_color(word, wordlen);
76 if (val >= -1) {
77 if (fg == -2) {
78 fg = val;
79 continue;
81 if (bg == -2) {
82 bg = val;
83 continue;
85 goto bad;
87 val = parse_attr(word, wordlen);
88 if (val < 0 || attr != -1)
89 goto bad;
90 attr = val;
93 if (attr >= 0 || fg >= 0 || bg >= 0) {
94 int sep = 0;
96 *dst++ = '\033';
97 *dst++ = '[';
98 if (attr >= 0) {
99 *dst++ = '0' + attr;
100 sep++;
102 if (fg >= 0) {
103 if (sep++)
104 *dst++ = ';';
105 if (fg < 8) {
106 *dst++ = '3';
107 *dst++ = '0' + fg;
108 } else {
109 dst += sprintf(dst, "38;5;%d", fg);
112 if (bg >= 0) {
113 if (sep++)
114 *dst++ = ';';
115 if (bg < 8) {
116 *dst++ = '4';
117 *dst++ = '0' + bg;
118 } else {
119 dst += sprintf(dst, "48;5;%d", bg);
122 *dst++ = 'm';
124 *dst = 0;
125 return;
126 bad:
127 die("bad color value '%.*s' for variable '%s'", value_len, value, var);
130 int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
132 if (value) {
133 if (!strcasecmp(value, "never"))
134 return 0;
135 if (!strcasecmp(value, "always"))
136 return 1;
137 if (!strcasecmp(value, "auto"))
138 goto auto_color;
141 if (!var)
142 return -1;
144 /* Missing or explicit false to turn off colorization */
145 if (!git_config_bool(var, value))
146 return 0;
148 /* any normal truth value defaults to 'auto' */
149 auto_color:
150 if (stdout_is_tty < 0)
151 stdout_is_tty = isatty(1);
152 if (stdout_is_tty || (pager_in_use() && pager_use_color)) {
153 char *term = getenv("TERM");
154 if (term && strcmp(term, "dumb"))
155 return 1;
157 return 0;
160 int git_color_default_config(const char *var, const char *value, void *cb)
162 if (!strcmp(var, "color.ui")) {
163 git_use_color_default = git_config_colorbool(var, value, -1);
164 return 0;
167 return git_default_config(var, value, cb);
170 static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
171 va_list args, const char *trail)
173 int r = 0;
175 if (*color)
176 r += fprintf(fp, "%s", color);
177 r += vfprintf(fp, fmt, args);
178 if (*color)
179 r += fprintf(fp, "%s", GIT_COLOR_RESET);
180 if (trail)
181 r += fprintf(fp, "%s", trail);
182 return r;
187 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
189 va_list args;
190 int r;
191 va_start(args, fmt);
192 r = color_vfprintf(fp, color, fmt, args, NULL);
193 va_end(args);
194 return r;
197 int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
199 va_list args;
200 int r;
201 va_start(args, fmt);
202 r = color_vfprintf(fp, color, fmt, args, "\n");
203 va_end(args);
204 return r;
208 * This function splits the buffer by newlines and colors the lines individually.
210 * Returns 0 on success.
212 int color_fwrite_lines(FILE *fp, const char *color,
213 size_t count, const char *buf)
215 if (!*color)
216 return fwrite(buf, count, 1, fp) != 1;
217 while (count) {
218 char *p = memchr(buf, '\n', count);
219 if (p != buf && (fputs(color, fp) < 0 ||
220 fwrite(buf, p ? p - buf : count, 1, fp) != 1 ||
221 fputs(GIT_COLOR_RESET, fp) < 0))
222 return -1;
223 if (!p)
224 return 0;
225 if (fputc('\n', fp) < 0)
226 return -1;
227 count -= p + 1 - buf;
228 buf = p + 1;
230 return 0;