color.c: refactor color_output arguments
color_output() takes a "type" parameter, which is either '3' or '4', and that byte is shown in front of '0'-'7' to form "30"-"37" or "40"-"47" in ANSI output mode for fore-ground and back-ground colors. Clarify the purpose of the parameter by renaming it to the "background" that is a boolean. Also, change the .value field in the color struct from storing 0-7 for basic 8 colors to storing 30-37 for ANSI colors. This aligns the code to show ANSI colors to the code for the 256 color scheme, which already uses the actual value to be sent to the terminal. Signed-off-by: Eyal Soha <shawarmakarma@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Eyal Soha committed
Jan 21, 2020 at 08:56 UTC
4a28eb0ae4fc666291a6a6f84a0605ccb9279278
1 file changed
+21
-13
color.c
+21
-13
@@ -24,6 +24,13 @@ const char *column_colors_ansi[] = {
24
GIT_COLOR_RESET,
25
};
26
27
+enum {
28
+ COLOR_BACKGROUND_OFFSET = 10,
29
+ COLOR_FOREGROUND_ANSI = 30,
30
+ COLOR_FOREGROUND_RGB = 38,
31
+ COLOR_FOREGROUND_256 = 38,
32
+};
33
+
34
/* Ignore the RESET at the end when giving the size */
35
const int column_colors_ansi_max = ARRAY_SIZE(column_colors_ansi) - 1;
36
@@ -92,7 +99,7 @@ static int parse_color(struct color *out, const char *name, int len)
99
for (i = 0; i < ARRAY_SIZE(color_names); i++) {
100
if (match_word(name, len, color_names[i])) {
101
out->type = COLOR_ANSI;
95
- out->value = i;
102
+ out->value = i + COLOR_FOREGROUND_ANSI;
103
return 0;
104
}
105
}
@@ -112,7 +119,7 @@ static int parse_color(struct color *out, const char *name, int len)
119
/* Rewrite low numbers as more-portable standard colors. */
120
} else if (val < 8) {
121
out->type = COLOR_ANSI;
115
- out->value = val;
122
+ out->value = val + COLOR_FOREGROUND_ANSI;
123
return 0;
124
} else if (val < 256) {
125
out->type = COLOR_256;
@@ -166,23 +173,26 @@ int color_parse(const char *value, char *dst)
173
* already have the ANSI escape code in it. "out" should have enough
174
* space in it to fit any color.
175
*/
169
-static char *color_output(char *out, int len, const struct color *c, char type)
176
+static char *color_output(char *out, int len, const struct color *c, int background)
177
{
178
+ int offset = 0;
179
+
180
+ if (background)
181
+ offset = COLOR_BACKGROUND_OFFSET;
182
switch (c->type) {
183
case COLOR_UNSPECIFIED:
184
case COLOR_NORMAL:
185
break;
186
case COLOR_ANSI:
176
- if (len < 2)
177
- BUG("color parsing ran out of space");
178
- *out++ = type;
179
- *out++ = '0' + c->value;
187
+ out += xsnprintf(out, len, "%d", c->value + offset);
188
break;
189
case COLOR_256:
182
- out += xsnprintf(out, len, "%c8;5;%d", type, c->value);
190
+ out += xsnprintf(out, len, "%d;5;%d", COLOR_FOREGROUND_256 + offset,
191
+ c->value);
192
break;
193
case COLOR_RGB:
185
- out += xsnprintf(out, len, "%c8;2;%d;%d;%d", type,
194
+ out += xsnprintf(out, len, "%d;2;%d;%d;%d",
195
+ COLOR_FOREGROUND_RGB + offset,
196
c->red, c->green, c->blue);
197
break;
198
}
@@ -279,14 +289,12 @@ int color_parse_mem(const char *value, int value_len, char *dst)
289
if (!color_empty(&fg)) {
290
if (sep++)
291
OUT(';');
282
- /* foreground colors are all in the 3x range */
283
- dst = color_output(dst, end - dst, &fg, '3');
292
+ dst = color_output(dst, end - dst, &fg, 0);
293
}
294
if (!color_empty(&bg)) {
295
if (sep++)
296
OUT(';');
288
- /* background colors are all in the 4x range */
289
- dst = color_output(dst, end - dst, &bg, '4');
297
+ dst = color_output(dst, end - dst, &bg, 1);
298
}
299
OUT('m');
300
}