Skip to content

Commit 4a28eb0

Browse files
eyal0gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0654dc commit 4a28eb0

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

color.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ const char *column_colors_ansi[] = {
2424
GIT_COLOR_RESET,
2525
};
2626

27+
enum {
28+
COLOR_BACKGROUND_OFFSET = 10,
29+
COLOR_FOREGROUND_ANSI = 30,
30+
COLOR_FOREGROUND_RGB = 38,
31+
COLOR_FOREGROUND_256 = 38,
32+
};
33+
2734
/* Ignore the RESET at the end when giving the size */
2835
const int column_colors_ansi_max = ARRAY_SIZE(column_colors_ansi) - 1;
2936

@@ -92,7 +99,7 @@ static int parse_color(struct color *out, const char *name, int len)
9299
for (i = 0; i < ARRAY_SIZE(color_names); i++) {
93100
if (match_word(name, len, color_names[i])) {
94101
out->type = COLOR_ANSI;
95-
out->value = i;
102+
out->value = i + COLOR_FOREGROUND_ANSI;
96103
return 0;
97104
}
98105
}
@@ -112,7 +119,7 @@ static int parse_color(struct color *out, const char *name, int len)
112119
/* Rewrite low numbers as more-portable standard colors. */
113120
} else if (val < 8) {
114121
out->type = COLOR_ANSI;
115-
out->value = val;
122+
out->value = val + COLOR_FOREGROUND_ANSI;
116123
return 0;
117124
} else if (val < 256) {
118125
out->type = COLOR_256;
@@ -166,23 +173,26 @@ int color_parse(const char *value, char *dst)
166173
* already have the ANSI escape code in it. "out" should have enough
167174
* space in it to fit any color.
168175
*/
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)
170177
{
178+
int offset = 0;
179+
180+
if (background)
181+
offset = COLOR_BACKGROUND_OFFSET;
171182
switch (c->type) {
172183
case COLOR_UNSPECIFIED:
173184
case COLOR_NORMAL:
174185
break;
175186
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);
180188
break;
181189
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);
183192
break;
184193
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,
186196
c->red, c->green, c->blue);
187197
break;
188198
}
@@ -279,14 +289,12 @@ int color_parse_mem(const char *value, int value_len, char *dst)
279289
if (!color_empty(&fg)) {
280290
if (sep++)
281291
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);
284293
}
285294
if (!color_empty(&bg)) {
286295
if (sep++)
287296
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);
290298
}
291299
OUT('m');
292300
}

0 commit comments

Comments
 (0)