X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=color.c;h=db4dccfb77d80c9bd8981719fe2d0dc17c6772b6;hb=ee27ca4a781844ddbf556ec64daae24d748a7c5a;hp=915d7a97f67dfd8459e2adc444acab777e503ddd;hpb=f18e6bef23809d2823c1a687f375b22c6af0e735;p=git.git diff --git a/color.c b/color.c index 915d7a97f..db4dccfb7 100644 --- a/color.c +++ b/color.c @@ -202,3 +202,31 @@ int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...) va_end(args); return r; } + +/* + * This function splits the buffer by newlines and colors the lines individually. + * + * Returns 0 on success. + */ +int color_fwrite_lines(FILE *fp, const char *color, + size_t count, const char *buf) +{ + if (!*color) + return fwrite(buf, count, 1, fp) != 1; + while (count) { + char *p = memchr(buf, '\n', count); + if (p != buf && (fputs(color, fp) < 0 || + fwrite(buf, p ? p - buf : count, 1, fp) != 1 || + fputs(COLOR_RESET, fp) < 0)) + return -1; + if (!p) + return 0; + if (fputc('\n', fp) < 0) + return -1; + count -= p + 1 - buf; + buf = p + 1; + } + return 0; +} + +