提交 becbdae8 编写于 作者: J Jonathan Nieder 提交者: Junio C Hamano

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: NJunio C Hamano <gitster@pobox.com>
Signed-off-by: NJonathan Nieder <jrnieder@gmail.com>
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 26db0f2e
......@@ -175,6 +175,15 @@ int git_color_default_config(const char *var, const char *value, void *cb)
return git_default_config(var, value, cb);
}
void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
{
if (*color)
fprintf(fp, "%s", color);
fprintf(fp, "%s", sb->buf);
if (*color)
fprintf(fp, "%s", GIT_COLOR_RESET);
}
static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
va_list args, const char *trail)
{
......
#ifndef COLOR_H
#define COLOR_H
struct strbuf;
/* 2 + (2 * num_attrs) + 8 + 1 + 8 + 'm' + NUL */
/* "\033[1;2;4;5;7;38;5;2xx;48;5;2xxm\0" */
/*
......@@ -64,6 +66,7 @@ __attribute__((format (printf, 3, 4)))
int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
__attribute__((format (printf, 3, 4)))
int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);
void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb);
int color_is_nil(const char *color);
......
......@@ -32,6 +32,80 @@ static const char *color(int slot, struct wt_status *s)
return c;
}
static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
const char *fmt, va_list ap, const char *trail)
{
struct strbuf sb = STRBUF_INIT;
struct strbuf linebuf = STRBUF_INIT;
const char *line, *eol;
strbuf_vaddf(&sb, fmt, ap);
if (!sb.len) {
strbuf_addch(&sb, '#');
if (!trail)
strbuf_addch(&sb, ' ');
color_print_strbuf(s->fp, color, &sb);
if (trail)
fprintf(s->fp, "%s", trail);
strbuf_release(&sb);
return;
}
for (line = sb.buf; *line; line = eol + 1) {
eol = strchr(line, '\n');
strbuf_reset(&linebuf);
if (at_bol) {
strbuf_addch(&linebuf, '#');
if (*line != '\n' && *line != '\t')
strbuf_addch(&linebuf, ' ');
}
if (eol)
strbuf_add(&linebuf, line, eol - line);
else
strbuf_addstr(&linebuf, line);
color_print_strbuf(s->fp, color, &linebuf);
if (eol)
fprintf(s->fp, "\n");
else
break;
at_bol = 1;
}
if (trail)
fprintf(s->fp, "%s", trail);
strbuf_release(&linebuf);
strbuf_release(&sb);
}
void status_printf_ln(struct wt_status *s, const char *color,
const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
status_vprintf(s, 1, color, fmt, ap, "\n");
va_end(ap);
}
void status_printf(struct wt_status *s, const char *color,
const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
status_vprintf(s, 1, color, fmt, ap, NULL);
va_end(ap);
}
void status_printf_more(struct wt_status *s, const char *color,
const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
status_vprintf(s, 0, color, fmt, ap, NULL);
va_end(ap);
}
void wt_status_prepare(struct wt_status *s)
{
unsigned char sha1[20];
......
......@@ -68,4 +68,11 @@ void wt_status_collect(struct wt_status *s);
void wt_shortstatus_print(struct wt_status *s, int null_termination, int show_branch);
void wt_porcelain_print(struct wt_status *s, int null_termination);
void status_printf_ln(struct wt_status *s, const char *color, const char *fmt, ...)
;
void status_printf(struct wt_status *s, const char *color, const char *fmt, ...)
;
void status_printf_more(struct wt_status *s, const char *color, const char *fmt, ...)
__attribute__((format(printf, 3, 4)));
#endif /* STATUS_H */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册