Skip to content

Commit becbdae

Browse files
jrngitster
authored andcommitted
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>
1 parent 26db0f2 commit becbdae

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

color.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ int git_color_default_config(const char *var, const char *value, void *cb)
175175
return git_default_config(var, value, cb);
176176
}
177177

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+
178187
static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
179188
va_list args, const char *trail)
180189
{

color.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef COLOR_H
22
#define COLOR_H
33

4+
struct strbuf;
5+
46
/* 2 + (2 * num_attrs) + 8 + 1 + 8 + 'm' + NUL */
57
/* "\033[1;2;4;5;7;38;5;2xx;48;5;2xxm\0" */
68
/*
@@ -64,6 +66,7 @@ __attribute__((format (printf, 3, 4)))
6466
int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
6567
__attribute__((format (printf, 3, 4)))
6668
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);
6770

6871
int color_is_nil(const char *color);
6972

wt-status.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,80 @@ static const char *color(int slot, struct wt_status *s)
3232
return c;
3333
}
3434

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+
35109
void wt_status_prepare(struct wt_status *s)
36110
{
37111
unsigned char sha1[20];

wt-status.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,11 @@ void wt_status_collect(struct wt_status *s);
6868
void wt_shortstatus_print(struct wt_status *s, int null_termination, int show_branch);
6969
void wt_porcelain_print(struct wt_status *s, int null_termination);
7070

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+
7178
#endif /* STATUS_H */

0 commit comments

Comments
 (0)