]>
Commit | Line | Data |
---|---|---|
7c92fe0e | 1 | #include "cache.h" |
85023577 | 2 | #include "color.h" |
7c92fe0e JK |
3 | |
4 | #define COLOR_RESET "\033[m" | |
5 | ||
6b2f2d98 MK |
6 | int git_use_color_default = 0; |
7 | ||
7c92fe0e JK |
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 | 22 | if (end - name == len && i >= -1 && i <= 255) |
7c92fe0e JK |
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 | { | |
44 | const char *ptr = value; | |
45 | int attr = -1; | |
46 | int fg = -2; | |
47 | int bg = -2; | |
48 | ||
49 | if (!strcasecmp(value, "reset")) { | |
50 | strcpy(dst, "\033[m"); | |
51 | return; | |
52 | } | |
53 | ||
54 | /* [fg [bg]] [attr] */ | |
55 | while (*ptr) { | |
56 | const char *word = ptr; | |
57 | int val, len = 0; | |
58 | ||
59 | while (word[len] && !isspace(word[len])) | |
60 | len++; | |
61 | ||
62 | ptr = word + len; | |
63 | while (*ptr && isspace(*ptr)) | |
64 | ptr++; | |
65 | ||
66 | val = parse_color(word, len); | |
67 | if (val >= -1) { | |
68 | if (fg == -2) { | |
69 | fg = val; | |
70 | continue; | |
71 | } | |
72 | if (bg == -2) { | |
73 | bg = val; | |
74 | continue; | |
75 | } | |
76 | goto bad; | |
77 | } | |
78 | val = parse_attr(word, len); | |
79 | if (val < 0 || attr != -1) | |
80 | goto bad; | |
81 | attr = val; | |
82 | } | |
83 | ||
84 | if (attr >= 0 || fg >= 0 || bg >= 0) { | |
85 | int sep = 0; | |
86 | ||
87 | *dst++ = '\033'; | |
88 | *dst++ = '['; | |
89 | if (attr >= 0) { | |
90 | *dst++ = '0' + attr; | |
91 | sep++; | |
92 | } | |
93 | if (fg >= 0) { | |
94 | if (sep++) | |
95 | *dst++ = ';'; | |
96 | if (fg < 8) { | |
97 | *dst++ = '3'; | |
98 | *dst++ = '0' + fg; | |
99 | } else { | |
100 | dst += sprintf(dst, "38;5;%d", fg); | |
101 | } | |
102 | } | |
103 | if (bg >= 0) { | |
104 | if (sep++) | |
105 | *dst++ = ';'; | |
106 | if (bg < 8) { | |
107 | *dst++ = '4'; | |
108 | *dst++ = '0' + bg; | |
109 | } else { | |
110 | dst += sprintf(dst, "48;5;%d", bg); | |
111 | } | |
112 | } | |
113 | *dst++ = 'm'; | |
114 | } | |
115 | *dst = 0; | |
116 | return; | |
117 | bad: | |
118 | die("bad config value '%s' for variable '%s'", value, var); | |
119 | } | |
120 | ||
0f6f5a40 | 121 | int git_config_colorbool(const char *var, const char *value, int stdout_is_tty) |
7c92fe0e | 122 | { |
57f2b842 JH |
123 | if (value) { |
124 | if (!strcasecmp(value, "never")) | |
125 | return 0; | |
126 | if (!strcasecmp(value, "always")) | |
127 | return 1; | |
128 | if (!strcasecmp(value, "auto")) | |
129 | goto auto_color; | |
7c92fe0e | 130 | } |
57f2b842 JH |
131 | |
132 | /* Missing or explicit false to turn off colorization */ | |
133 | if (!git_config_bool(var, value)) | |
7c92fe0e | 134 | return 0; |
57f2b842 JH |
135 | |
136 | /* any normal truth value defaults to 'auto' */ | |
137 | auto_color: | |
0f6f5a40 JH |
138 | if (stdout_is_tty < 0) |
139 | stdout_is_tty = isatty(1); | |
6e9af863 | 140 | if (stdout_is_tty || (pager_in_use() && pager_use_color)) { |
57f2b842 JH |
141 | char *term = getenv("TERM"); |
142 | if (term && strcmp(term, "dumb")) | |
143 | return 1; | |
144 | } | |
145 | return 0; | |
7c92fe0e JK |
146 | } |
147 | ||
ef90d6d4 | 148 | int git_color_default_config(const char *var, const char *value, void *cb) |
6b2f2d98 MK |
149 | { |
150 | if (!strcmp(var, "color.ui")) { | |
151 | git_use_color_default = git_config_colorbool(var, value, -1); | |
152 | return 0; | |
153 | } | |
154 | ||
ef90d6d4 | 155 | return git_default_config(var, value, cb); |
6b2f2d98 MK |
156 | } |
157 | ||
f26a0012 | 158 | static int color_vfprintf(FILE *fp, const char *color, const char *fmt, |
7c92fe0e JK |
159 | va_list args, const char *trail) |
160 | { | |
161 | int r = 0; | |
162 | ||
163 | if (*color) | |
f26a0012 KH |
164 | r += fprintf(fp, "%s", color); |
165 | r += vfprintf(fp, fmt, args); | |
7c92fe0e | 166 | if (*color) |
f26a0012 | 167 | r += fprintf(fp, "%s", COLOR_RESET); |
7c92fe0e | 168 | if (trail) |
f26a0012 | 169 | r += fprintf(fp, "%s", trail); |
7c92fe0e JK |
170 | return r; |
171 | } | |
172 | ||
173 | ||
174 | ||
f26a0012 | 175 | int color_fprintf(FILE *fp, const char *color, const char *fmt, ...) |
7c92fe0e JK |
176 | { |
177 | va_list args; | |
178 | int r; | |
179 | va_start(args, fmt); | |
f26a0012 | 180 | r = color_vfprintf(fp, color, fmt, args, NULL); |
7c92fe0e JK |
181 | va_end(args); |
182 | return r; | |
183 | } | |
184 | ||
f26a0012 | 185 | int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...) |
7c92fe0e JK |
186 | { |
187 | va_list args; | |
188 | int r; | |
189 | va_start(args, fmt); | |
f26a0012 | 190 | r = color_vfprintf(fp, color, fmt, args, "\n"); |
7c92fe0e JK |
191 | va_end(args); |
192 | return r; | |
193 | } |