Allow ':/<oneline-prefix>' syntax to work with save_commit_buffer == 0
[git/platforms.git] / color.c
blob97cfbda31ac02b7e4fe747052c28821a34ee0165
1 #include "cache.h"
2 #include "color.h"
4 #define COLOR_RESET "\033[m"
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 (*name && !*end && 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 const char *ptr = value;
43 int attr = -1;
44 int fg = -2;
45 int bg = -2;
47 if (!strcasecmp(value, "reset")) {
48 strcpy(dst, "\033[m");
49 return;
52 /* [fg [bg]] [attr] */
53 while (*ptr) {
54 const char *word = ptr;
55 int val, len = 0;
57 while (word[len] && !isspace(word[len]))
58 len++;
60 ptr = word + len;
61 while (*ptr && isspace(*ptr))
62 ptr++;
64 val = parse_color(word, len);
65 if (val >= -1) {
66 if (fg == -2) {
67 fg = val;
68 continue;
70 if (bg == -2) {
71 bg = val;
72 continue;
74 goto bad;
76 val = parse_attr(word, len);
77 if (val < 0 || attr != -1)
78 goto bad;
79 attr = val;
82 if (attr >= 0 || fg >= 0 || bg >= 0) {
83 int sep = 0;
85 *dst++ = '\033';
86 *dst++ = '[';
87 if (attr >= 0) {
88 *dst++ = '0' + attr;
89 sep++;
91 if (fg >= 0) {
92 if (sep++)
93 *dst++ = ';';
94 if (fg < 8) {
95 *dst++ = '3';
96 *dst++ = '0' + fg;
97 } else {
98 dst += sprintf(dst, "38;5;%d", fg);
101 if (bg >= 0) {
102 if (sep++)
103 *dst++ = ';';
104 if (bg < 8) {
105 *dst++ = '4';
106 *dst++ = '0' + bg;
107 } else {
108 dst += sprintf(dst, "48;5;%d", bg);
111 *dst++ = 'm';
113 *dst = 0;
114 return;
115 bad:
116 die("bad config value '%s' for variable '%s'", value, var);
119 int git_config_colorbool(const char *var, const char *value)
121 if (value) {
122 if (!strcasecmp(value, "never"))
123 return 0;
124 if (!strcasecmp(value, "always"))
125 return 1;
126 if (!strcasecmp(value, "auto"))
127 goto auto_color;
130 /* Missing or explicit false to turn off colorization */
131 if (!git_config_bool(var, value))
132 return 0;
134 /* any normal truth value defaults to 'auto' */
135 auto_color:
136 if (isatty(1) || (pager_in_use && pager_use_color)) {
137 char *term = getenv("TERM");
138 if (term && strcmp(term, "dumb"))
139 return 1;
141 return 0;
144 static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
145 va_list args, const char *trail)
147 int r = 0;
149 if (*color)
150 r += fprintf(fp, "%s", color);
151 r += vfprintf(fp, fmt, args);
152 if (*color)
153 r += fprintf(fp, "%s", COLOR_RESET);
154 if (trail)
155 r += fprintf(fp, "%s", trail);
156 return r;
161 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
163 va_list args;
164 int r;
165 va_start(args, fmt);
166 r = color_vfprintf(fp, color, fmt, args, NULL);
167 va_end(args);
168 return r;
171 int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
173 va_list args;
174 int r;
175 va_start(args, fmt);
176 r = color_vfprintf(fp, color, fmt, args, "\n");
177 va_end(args);
178 return r;