summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 3ea95d2)
raw | patch | inline | side by side (parent: 3ea95d2)
author | Johannes Schindelin <johannes.schindelin@gmx.de> | |
Sat, 17 Jan 2009 16:29:42 +0000 (17:29 +0100) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Sat, 17 Jan 2009 18:42:03 +0000 (10:42 -0800) |
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>
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>
color.c | patch | blob | history | |
color.h | patch | blob | history |
index fc0b72ad59b13e4bd86372e5e81b4f400c99d26e..d4ae83f9b7b2075aed27e0709e4cc99a76755e13 100644 (file)
--- a/color.c
+++ b/color.c
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;
+}
+
+
index 6cf5c88aaf8d0e38e2853e6fd212e3cdd6c180cb..cd5c985eb7ee51c332020722fdf6282637822f37 100644 (file)
--- a/color.h
+++ b/color.h
void color_parse(const char *var, const char *value, char *dst);
int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);
+int color_fwrite_lines(FILE *fp, const char *color, size_t count, const char *buf);
#endif /* COLOR_H */