Merge branch 'jc/index-pack'
[git/jnareb-git.git] / color.c
blob3db214c24720496f13481b718e46810c21f53b8d
1 #include "cache.h"
2 #include "color.h"
4 int git_use_color_default = 0;
6 /*
7 * The list of available column colors.
8 */
9 const char *column_colors_ansi[] = {
10 GIT_COLOR_RED,
11 GIT_COLOR_GREEN,
12 GIT_COLOR_YELLOW,
13 GIT_COLOR_BLUE,
14 GIT_COLOR_MAGENTA,
15 GIT_COLOR_CYAN,
16 GIT_COLOR_BOLD_RED,
17 GIT_COLOR_BOLD_GREEN,
18 GIT_COLOR_BOLD_YELLOW,
19 GIT_COLOR_BOLD_BLUE,
20 GIT_COLOR_BOLD_MAGENTA,
21 GIT_COLOR_BOLD_CYAN,
22 GIT_COLOR_RESET,
25 /* Ignore the RESET at the end when giving the size */
26 const int column_colors_ansi_max = ARRAY_SIZE(column_colors_ansi) - 1;
28 static int parse_color(const char *name, int len)
30 static const char * const color_names[] = {
31 "normal", "black", "red", "green", "yellow",
32 "blue", "magenta", "cyan", "white"
34 char *end;
35 int i;
36 for (i = 0; i < ARRAY_SIZE(color_names); i++) {
37 const char *str = color_names[i];
38 if (!strncasecmp(name, str, len) && !str[len])
39 return i - 1;
41 i = strtol(name, &end, 10);
42 if (end - name == len && i >= -1 && i <= 255)
43 return i;
44 return -2;
47 static int parse_attr(const char *name, int len)
49 static const int attr_values[] = { 1, 2, 4, 5, 7 };
50 static const char * const attr_names[] = {
51 "bold", "dim", "ul", "blink", "reverse"
53 int i;
54 for (i = 0; i < ARRAY_SIZE(attr_names); i++) {
55 const char *str = attr_names[i];
56 if (!strncasecmp(name, str, len) && !str[len])
57 return attr_values[i];
59 return -1;
62 void color_parse(const char *value, const char *var, char *dst)
64 color_parse_mem(value, strlen(value), var, dst);
67 void color_parse_mem(const char *value, int value_len, const char *var,
68 char *dst)
70 const char *ptr = value;
71 int len = value_len;
72 unsigned int attr = 0;
73 int fg = -2;
74 int bg = -2;
76 if (!strncasecmp(value, "reset", len)) {
77 strcpy(dst, GIT_COLOR_RESET);
78 return;
81 /* [fg [bg]] [attr]... */
82 while (len > 0) {
83 const char *word = ptr;
84 int val, wordlen = 0;
86 while (len > 0 && !isspace(word[wordlen])) {
87 wordlen++;
88 len--;
91 ptr = word + wordlen;
92 while (len > 0 && isspace(*ptr)) {
93 ptr++;
94 len--;
97 val = parse_color(word, wordlen);
98 if (val >= -1) {
99 if (fg == -2) {
100 fg = val;
101 continue;
103 if (bg == -2) {
104 bg = val;
105 continue;
107 goto bad;
109 val = parse_attr(word, wordlen);
110 if (0 <= val)
111 attr |= (1 << val);
112 else
113 goto bad;
116 if (attr || fg >= 0 || bg >= 0) {
117 int sep = 0;
118 int i;
120 *dst++ = '\033';
121 *dst++ = '[';
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;
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);
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);
152 *dst++ = 'm';
154 *dst = 0;
155 return;
156 bad:
157 die("bad color value '%.*s' for variable '%s'", value_len, value, var);
160 int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
162 if (value) {
163 if (!strcasecmp(value, "never"))
164 return 0;
165 if (!strcasecmp(value, "always"))
166 return 1;
167 if (!strcasecmp(value, "auto"))
168 goto auto_color;
171 if (!var)
172 return -1;
174 /* Missing or explicit false to turn off colorization */
175 if (!git_config_bool(var, value))
176 return 0;
178 /* any normal truth value defaults to 'auto' */
179 auto_color:
180 if (stdout_is_tty < 0)
181 stdout_is_tty = isatty(1);
182 if (stdout_is_tty || (pager_in_use() && pager_use_color)) {
183 char *term = getenv("TERM");
184 if (term && strcmp(term, "dumb"))
185 return 1;
187 return 0;
190 int git_color_default_config(const char *var, const char *value, void *cb)
192 if (!strcmp(var, "color.ui")) {
193 git_use_color_default = git_config_colorbool(var, value, -1);
194 return 0;
197 return git_default_config(var, value, cb);
200 void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
202 if (*color)
203 fprintf(fp, "%s", color);
204 fprintf(fp, "%s", sb->buf);
205 if (*color)
206 fprintf(fp, "%s", GIT_COLOR_RESET);
209 static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
210 va_list args, const char *trail)
212 int r = 0;
214 if (*color)
215 r += fprintf(fp, "%s", color);
216 r += vfprintf(fp, fmt, args);
217 if (*color)
218 r += fprintf(fp, "%s", GIT_COLOR_RESET);
219 if (trail)
220 r += fprintf(fp, "%s", trail);
221 return r;
226 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
228 va_list args;
229 int r;
230 va_start(args, fmt);
231 r = color_vfprintf(fp, color, fmt, args, NULL);
232 va_end(args);
233 return r;
236 int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
238 va_list args;
239 int r;
240 va_start(args, fmt);
241 r = color_vfprintf(fp, color, fmt, args, "\n");
242 va_end(args);
243 return r;
246 int color_is_nil(const char *c)
248 return !strcmp(c, "NIL");