]> git.ipfire.org Git - thirdparty/git.git/blame - color.c
GIT-VERSION-FILE: check ./version first.
[thirdparty/git.git] / color.c
CommitLineData
7c92fe0e 1#include "cache.h"
85023577 2#include "color.h"
7c92fe0e
JK
3
4#define COLOR_RESET "\033[m"
5
6static int parse_color(const char *name, int len)
7{
8 static const char * const color_names[] = {
9 "normal", "black", "red", "green", "yellow",
10 "blue", "magenta", "cyan", "white"
11 };
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;
18 }
19 i = strtol(name, &end, 10);
20 if (*name && !*end && i >= -1 && i <= 255)
21 return i;
22 return -2;
23}
24
25static int parse_attr(const char *name, int len)
26{
27 static const int attr_values[] = { 1, 2, 4, 5, 7 };
28 static const char * const attr_names[] = {
29 "bold", "dim", "ul", "blink", "reverse"
30 };
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];
36 }
37 return -1;
38}
39
40void color_parse(const char *value, const char *var, char *dst)
41{
42 const char *ptr = value;
43 int attr = -1;
44 int fg = -2;
45 int bg = -2;
46
47 if (!strcasecmp(value, "reset")) {
48 strcpy(dst, "\033[m");
49 return;
50 }
51
52 /* [fg [bg]] [attr] */
53 while (*ptr) {
54 const char *word = ptr;
55 int val, len = 0;
56
57 while (word[len] && !isspace(word[len]))
58 len++;
59
60 ptr = word + len;
61 while (*ptr && isspace(*ptr))
62 ptr++;
63
64 val = parse_color(word, len);
65 if (val >= -1) {
66 if (fg == -2) {
67 fg = val;
68 continue;
69 }
70 if (bg == -2) {
71 bg = val;
72 continue;
73 }
74 goto bad;
75 }
76 val = parse_attr(word, len);
77 if (val < 0 || attr != -1)
78 goto bad;
79 attr = val;
80 }
81
82 if (attr >= 0 || fg >= 0 || bg >= 0) {
83 int sep = 0;
84
85 *dst++ = '\033';
86 *dst++ = '[';
87 if (attr >= 0) {
88 *dst++ = '0' + attr;
89 sep++;
90 }
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);
99 }
100 }
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);
109 }
110 }
111 *dst++ = 'm';
112 }
113 *dst = 0;
114 return;
115bad:
116 die("bad config value '%s' for variable '%s'", value, var);
117}
118
119int git_config_colorbool(const char *var, const char *value)
120{
121 if (!value)
122 return 1;
123 if (!strcasecmp(value, "auto")) {
124 if (isatty(1) || (pager_in_use && pager_use_color)) {
125 char *term = getenv("TERM");
126 if (term && strcmp(term, "dumb"))
127 return 1;
128 }
129 return 0;
130 }
131 if (!strcasecmp(value, "never"))
132 return 0;
133 if (!strcasecmp(value, "always"))
134 return 1;
135 return git_config_bool(var, value);
136}
137
138static int color_vprintf(const char *color, const char *fmt,
139 va_list args, const char *trail)
140{
141 int r = 0;
142
143 if (*color)
144 r += printf("%s", color);
145 r += vprintf(fmt, args);
146 if (*color)
147 r += printf("%s", COLOR_RESET);
148 if (trail)
149 r += printf("%s", trail);
150 return r;
151}
152
153
154
155int color_printf(const char *color, const char *fmt, ...)
156{
157 va_list args;
158 int r;
159 va_start(args, fmt);
160 r = color_vprintf(color, fmt, args, NULL);
161 va_end(args);
162 return r;
163}
164
165int color_printf_ln(const char *color, const char *fmt, ...)
166{
167 va_list args;
168 int r;
169 va_start(args, fmt);
170 r = color_vprintf(color, fmt, args, "\n");
171 va_end(args);
172 return r;
173}