Skip to content

Commit 75e5e9c

Browse files
stefanbellergitster
authored andcommitted
color.h: document and modernize header
Add documentation explaining the functions in color.h. While at it, migrate the function `color_set` into grep.c, where the only callers are. Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8279ed0 commit 75e5e9c

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

color.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,6 @@ int color_parse(const char *value, char *dst)
161161
return color_parse_mem(value, strlen(value), dst);
162162
}
163163

164-
void color_set(char *dst, const char *color_bytes)
165-
{
166-
xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes);
167-
}
168-
169164
/*
170165
* Write the ANSI color codes for "c" to "out"; the string should
171166
* already have the ANSI escape code in it. "out" should have enough
@@ -399,8 +394,6 @@ static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
399394
return r;
400395
}
401396

402-
403-
404397
int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
405398
{
406399
va_list args;

color.h

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,46 @@ int git_color_config(const char *var, const char *value, void *cb);
7676
int git_color_default_config(const char *var, const char *value, void *cb);
7777

7878
/*
79-
* Set the color buffer (which must be COLOR_MAXLEN bytes)
80-
* to the raw color bytes; this is useful for initializing
81-
* default color variables.
79+
* Parse a config option, which can be a boolean or one of
80+
* "never", "auto", "always". Return a constant of
81+
* GIT_COLOR_NEVER for "never" or negative boolean,
82+
* GIT_COLOR_ALWAYS for "always" or a positive boolean,
83+
* and GIT_COLOR_AUTO for "auto".
8284
*/
83-
void color_set(char *dst, const char *color_bytes);
84-
8585
int git_config_colorbool(const char *var, const char *value);
86+
87+
/*
88+
* Return a boolean whether to use color, where the argument 'var' is
89+
* one of GIT_COLOR_UNKNOWN, GIT_COLOR_NEVER, GIT_COLOR_ALWAYS, GIT_COLOR_AUTO.
90+
*/
8691
int want_color(int var);
92+
93+
/*
94+
* Translate a Git color from 'value' into a string that the terminal can
95+
* interpret and store it into 'dst'. The Git color values are of the form
96+
* "foreground [background] [attr]" where fore- and background can be a color
97+
* name ("red"), a RGB code (#0xFF0000) or a 256-color-mode from the terminal.
98+
*/
8799
int color_parse(const char *value, char *dst);
88100
int color_parse_mem(const char *value, int len, char *dst);
101+
102+
/*
103+
* Output the formatted string in the specified color (and then reset to normal
104+
* color so subsequent output is uncolored). Omits the color encapsulation if
105+
* `color` is NULL. The `color_fprintf_ln` prints a new line after resetting
106+
* the color. The `color_print_strbuf` prints the contents of the given
107+
* strbuf (BUG: but only up to its first NUL character).
108+
*/
89109
__attribute__((format (printf, 3, 4)))
90110
int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
91111
__attribute__((format (printf, 3, 4)))
92112
int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);
93113
void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb);
94114

115+
/*
116+
* Check if the given color is GIT_COLOR_NIL that means "no color selected".
117+
* The caller needs to replace the color with the actual desired color.
118+
*/
95119
int color_is_nil(const char *color);
96120

97121
#endif /* COLOR_H */

grep.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ static void std_output(struct grep_opt *opt, const void *buf, size_t size)
1818
fwrite(buf, size, 1, stdout);
1919
}
2020

21+
static void color_set(char *dst, const char *color_bytes)
22+
{
23+
xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes);
24+
}
25+
2126
/*
2227
* Initialize the grep_defaults template with hardcoded defaults.
2328
* We could let the compiler do this, but without C99 initializers

0 commit comments

Comments
 (0)