Skip to content

Commit ddf6131

Browse files
committed
color: add color_parse_gently()
When parsing colors, a failed parse leads to an error message due to the result returning error(). To allow for quiet parsing, create color_parse_gently(). To accomplish this, convert the implementation of color_parse_mem() into a static color_parse_mem_1() helper that adds a 'gently' parameter. The color_parse_gently() method can then use this. Since it is a near equivalent to color_parse(), move that method down in the file so they can be nearby while also appearing after color_parse_mem_1(). Signed-off-by: Derrick Stolee <stolee@gmail.com>
1 parent 9221ca2 commit ddf6131

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

color.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,6 @@ static int parse_attr(const char *name, size_t len)
223223
return -1;
224224
}
225225

226-
int color_parse(const char *value, char *dst)
227-
{
228-
return color_parse_mem(value, strlen(value), dst);
229-
}
230-
231226
/*
232227
* Write the ANSI color codes for "c" to "out"; the string should
233228
* already have the ANSI escape code in it. "out" should have enough
@@ -264,7 +259,8 @@ static int color_empty(const struct color *c)
264259
return c->type <= COLOR_NORMAL;
265260
}
266261

267-
int color_parse_mem(const char *value, int value_len, char *dst)
262+
static int color_parse_mem_1(const char *value, int value_len,
263+
char *dst, int gently)
268264
{
269265
const char *ptr = value;
270266
int len = value_len;
@@ -365,10 +361,25 @@ int color_parse_mem(const char *value, int value_len, char *dst)
365361
OUT(0);
366362
return 0;
367363
bad:
368-
return error(_("invalid color value: %.*s"), value_len, value);
364+
return gently ? -1 : error(_("invalid color value: %.*s"), value_len, value);
369365
#undef OUT
370366
}
371367

368+
int color_parse_mem(const char *value, int value_len, char *dst)
369+
{
370+
return color_parse_mem_1(value, value_len, dst, 0);
371+
}
372+
373+
int color_parse(const char *value, char *dst)
374+
{
375+
return color_parse_mem(value, strlen(value), dst);
376+
}
377+
378+
int color_parse_gently(const char *value, char *dst)
379+
{
380+
return color_parse_mem_1(value, strlen(value), dst, 1);
381+
}
382+
372383
enum git_colorbool git_config_colorbool(const char *var, const char *value)
373384
{
374385
if (value) {

color.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ bool want_color_fd(int fd, enum git_colorbool var);
118118
* terminal.
119119
*/
120120
int color_parse(const char *value, char *dst);
121+
int color_parse_gently(const char *value, char *dst);
121122
int color_parse_mem(const char *value, int len, char *dst);
122123

123124
/*

0 commit comments

Comments
 (0)