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 <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Feb 12, 2018 at 17:41 UTC 75e5e9c3f7f9615ae2ee38a839aafbbe642a735a
3 files changed +34 -12
color.c
-7
@@ -161,11 +161,6 @@ int color_parse(const char *value, char *dst)
161 return color_parse_mem(value, strlen(value), dst);
162 }
163
164 -void color_set(char *dst, const char *color_bytes)
165 -{
166 - xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes);
167 -}
168 -
164 /*
165 * Write the ANSI color codes for "c" to "out"; the string should
166 * 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,
394 return r;
395 }
396
402 -
403 -
397 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
398 {
399 va_list args;
color.h
+29 -5
@@ -76,22 +76,46 @@ int git_color_config(const char *var, const char *value, void *cb);
76 int git_color_default_config(const char *var, const char *value, void *cb);
77
78 /*
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".
84 */
83 -void color_set(char *dst, const char *color_bytes);
84 -
85 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 + */
91 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 + */
99 int color_parse(const char *value, char *dst);
100 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 + */
109 __attribute__((format (printf, 3, 4)))
110 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
111 __attribute__((format (printf, 3, 4)))
112 int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);
113 void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb);
114
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 + */
119 int color_is_nil(const char *color);
120
121 #endif /* COLOR_H */
grep.c
+5
@@ -18,6 +18,11 @@ static void std_output(struct grep_opt *opt, const void *buf, size_t size)
18 fwrite(buf, size, 1, stdout);
19 }
20
21 +static void color_set(char *dst, const char *color_bytes)
22 +{
23 + xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes);
24 +}
25 +
26 /*
27 * Initialize the grep_defaults template with hardcoded defaults.
28 * We could let the compiler do this, but without C99 initializers