refspec: allow colon-less wildcard "refs/category/*"
[git/git-bigfiles.git] / color.c
blob12a6453f90eb4ce2b39f85762bf3acd1cff2b9f2
1 #include "cache.h"
2 #include "color.h"
4 #define COLOR_RESET "\033[m"
6 int git_use_color_default = 0;
8 static int parse_color(const char *name, int len)
10 static const char * const color_names[] = {
11 "normal", "black", "red", "green", "yellow",
12 "blue", "magenta", "cyan", "white"
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;
21 i = strtol(name, &end, 10);
22 if (end - name == len && i >= -1 && i <= 255)
23 return i;
24 return -2;
27 static int parse_attr(const char *name, int len)
29 static const int attr_values[] = { 1, 2, 4, 5, 7 };
30 static const char * const attr_names[] = {
31 "bold", "dim", "ul", "blink", "reverse"
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];
39 return -1;
42 void color_parse(const char *value, const char *var, char *dst)
44 const char *ptr = value;
45 int attr = -1;
46 int fg = -2;
47 int bg = -2;
49 if (!strcasecmp(value, "reset")) {
50 strcpy(dst, "\033[m");
51 return;
54 /* [fg [bg]] [attr] */
55 while (*ptr) {
56 const char *word = ptr;
57 int val, len = 0;
59 while (word[len] && !isspace(word[len]))
60 len++;
62 ptr = word + len;
63 while (*ptr && isspace(*ptr))
64 ptr++;
66 val = parse_color(word, len);
67 if (val >= -1) {
68 if (fg == -2) {
69 fg = val;
70 continue;
72 if (bg == -2) {
73 bg = val;
74 continue;
76 goto bad;
78 val = parse_attr(word, len);
79 if (val < 0 || attr != -1)
80 goto bad;
81 attr = val;
84 if (attr >= 0 || fg >= 0 || bg >= 0) {
85 int sep = 0;
87 *dst++ = '\033';
88 *dst++ = '[';
89 if (attr >= 0) {
90 *dst++ = '0' + attr;
91 sep++;
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);
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);
113 *dst++ = 'm';
115 *dst = 0;
116 return;
117 bad:
118 die("bad config value '%s' for variable '%s'", value, var);
121 int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
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;
132 /* Missing or explicit false to turn off colorization */
133 if (!git_config_bool(var, value))
134 return 0;
136 /* any normal truth value defaults to 'auto' */
137 auto_color:
138 if (stdout_is_tty < 0)
139 stdout_is_tty = isatty(1);
140 if (stdout_is_tty || (pager_in_use() && pager_use_color)) {
141 char *term = getenv("TERM");
142 if (term && strcmp(term, "dumb"))
143 return 1;
145 return 0;
148 int git_color_default_config(const char *var, const char *value)
150 if (!strcmp(var, "color.ui")) {
151 git_use_color_default = git_config_colorbool(var, value, -1);
152 return 0;
155 return git_default_config(var, value);
158 static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
159 va_list args, const char *trail)
161 int r = 0;
163 if (*color)
164 r += fprintf(fp, "%s", color);
165 r += vfprintf(fp, fmt, args);
166 if (*color)
167 r += fprintf(fp, "%s", COLOR_RESET);
168 if (trail)
169 r += fprintf(fp, "%s", trail);
170 return r;
175 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
177 va_list args;
178 int r;
179 va_start(args, fmt);
180 r = color_vfprintf(fp, color, fmt, args, NULL);
181 va_end(args);
182 return r;
185 int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
187 va_list args;
188 int r;
189 va_start(args, fmt);
190 r = color_vfprintf(fp, color, fmt, args, "\n");
191 va_end(args);
192 return r;