wt-status: add helpers for printing wt-status lines
Introduce status_printf{,_ln,_more} wrapper functions around color_vfprintf() which take care of adding "#" to the beginning of status lines automatically. The semantics: - status_printf() is just like color_fprintf() but it adds a "# " at the beginning of each line of output; - status_printf_ln() is a convenience function that additionally adds "\n" at the end; - status_printf_more() is a variant of status_printf() used to continue lines that have already started. It suppresses the "#" at the beginning of the first line. Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Nieder committed
Feb 25, 2011 at 23:09 UTC
e0335fcdad28da578904bb33fcf1dfbcdf172599
4 files changed
+93
color.c
+9
@@ -175,6 +175,15 @@ int git_color_default_config(const char *var, const char *value, void *cb)
175
return git_default_config(var, value, cb);
176
}
177
178
+void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
179
+{
180
+ if (*color)
181
+ fprintf(fp, "%s", color);
182
+ fprintf(fp, "%s", sb->buf);
183
+ if (*color)
184
+ fprintf(fp, "%s", GIT_COLOR_RESET);
185
+}
186
+
187
static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
188
va_list args, const char *trail)
189
{
color.h
+3
@@ -1,6 +1,8 @@
1
#ifndef COLOR_H
2
#define COLOR_H
3
4
+struct strbuf;
5
+
6
/* 2 + (2 * num_attrs) + 8 + 1 + 8 + 'm' + NUL */
7
/* "\033[1;2;4;5;7;38;5;2xx;48;5;2xxm\0" */
8
/*
@@ -64,6 +66,7 @@ __attribute__((format (printf, 3, 4)))
66
int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
67
__attribute__((format (printf, 3, 4)))
68
int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);
69
+void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb);
70
71
int color_is_nil(const char *color);
72
wt-status.c
+74
@@ -32,6 +32,80 @@ static const char *color(int slot, struct wt_status *s)
32
return c;
33
}
34
35
+static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
36
+ const char *fmt, va_list ap, const char *trail)
37
+{
38
+ struct strbuf sb = STRBUF_INIT;
39
+ struct strbuf linebuf = STRBUF_INIT;
40
+ const char *line, *eol;
41
+
42
+ strbuf_vaddf(&sb, fmt, ap);
43
+ if (!sb.len) {
44
+ strbuf_addch(&sb, '#');
45
+ if (!trail)
46
+ strbuf_addch(&sb, ' ');
47
+ color_print_strbuf(s->fp, color, &sb);
48
+ if (trail)
49
+ fprintf(s->fp, "%s", trail);
50
+ strbuf_release(&sb);
51
+ return;
52
+ }
53
+ for (line = sb.buf; *line; line = eol + 1) {
54
+ eol = strchr(line, '\n');
55
+
56
+ strbuf_reset(&linebuf);
57
+ if (at_bol) {
58
+ strbuf_addch(&linebuf, '#');
59
+ if (*line != '\n' && *line != '\t')
60
+ strbuf_addch(&linebuf, ' ');
61
+ }
62
+ if (eol)
63
+ strbuf_add(&linebuf, line, eol - line);
64
+ else
65
+ strbuf_addstr(&linebuf, line);
66
+ color_print_strbuf(s->fp, color, &linebuf);
67
+ if (eol)
68
+ fprintf(s->fp, "\n");
69
+ else
70
+ break;
71
+ at_bol = 1;
72
+ }
73
+ if (trail)
74
+ fprintf(s->fp, "%s", trail);
75
+ strbuf_release(&linebuf);
76
+ strbuf_release(&sb);
77
+}
78
+
79
+void status_printf_ln(struct wt_status *s, const char *color,
80
+ const char *fmt, ...)
81
+{
82
+ va_list ap;
83
+
84
+ va_start(ap, fmt);
85
+ status_vprintf(s, 1, color, fmt, ap, "\n");
86
+ va_end(ap);
87
+}
88
+
89
+void status_printf(struct wt_status *s, const char *color,
90
+ const char *fmt, ...)
91
+{
92
+ va_list ap;
93
+
94
+ va_start(ap, fmt);
95
+ status_vprintf(s, 1, color, fmt, ap, NULL);
96
+ va_end(ap);
97
+}
98
+
99
+void status_printf_more(struct wt_status *s, const char *color,
100
+ const char *fmt, ...)
101
+{
102
+ va_list ap;
103
+
104
+ va_start(ap, fmt);
105
+ status_vprintf(s, 0, color, fmt, ap, NULL);
106
+ va_end(ap);
107
+}
108
+
109
void wt_status_prepare(struct wt_status *s)
110
{
111
unsigned char sha1[20];
wt-status.h
+7
@@ -68,4 +68,11 @@ void wt_status_collect(struct wt_status *s);
68
void wt_shortstatus_print(struct wt_status *s, int null_termination, int show_branch);
69
void wt_porcelain_print(struct wt_status *s, int null_termination);
70
71
+void status_printf_ln(struct wt_status *s, const char *color, const char *fmt, ...)
72
+ ;
73
+void status_printf(struct wt_status *s, const char *color, const char *fmt, ...)
74
+ ;
75
+void status_printf_more(struct wt_status *s, const char *color, const char *fmt, ...)
76
+ __attribute__((format(printf, 3, 4)));
77
+
78
#endif /* STATUS_H */