@matheusbsbs / matheus-bagliano / commits / 07b57e90f7
Fork 0

Add color_fwrite_lines(), a function coloring each line individually

We have to set the color before every line and reset it before every newline. Add a function color_fwrite_lines() which does that for us. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jan 17, 2009 at 17:29 UTC 07b57e90f7c852c4fe212ab1d91058f27469a74b
2 files changed +29
color.c
+28
@@ -191,3 +191,31 @@ int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
191 va_end(args);
192 return r;
193 }
194 +
195 +/*
196 + * This function splits the buffer by newlines and colors the lines individually.
197 + *
198 + * Returns 0 on success.
199 + */
200 +int color_fwrite_lines(FILE *fp, const char *color,
201 + size_t count, const char *buf)
202 +{
203 + if (!*color)
204 + return fwrite(buf, count, 1, fp) != 1;
205 + while (count) {
206 + char *p = memchr(buf, '\n', count);
207 + if (p != buf && (fputs(color, fp) < 0 ||
208 + fwrite(buf, p ? p - buf : count, 1, fp) != 1 ||
209 + fputs(COLOR_RESET, fp) < 0))
210 + return -1;
211 + if (!p)
212 + return 0;
213 + if (fputc('\n', fp) < 0)
214 + return -1;
215 + count -= p + 1 - buf;
216 + buf = p + 1;
217 + }
218 + return 0;
219 +}
220 +
221 +
color.h
+1
@@ -19,5 +19,6 @@ int git_config_colorbool(const char *var, const char *value, int stdout_is_tty);
19 void color_parse(const char *var, const char *value, char *dst);
20 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
21 int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);
22 +int color_fwrite_lines(FILE *fp, const char *color, size_t count, const char *buf);
23
24 #endif /* COLOR_H */