git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/5] wt-status: add helpers for printing wt-status lines

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Feb 26, 2011, 05:09 UTC
Message-ID
<20110226050941.GC27887@elie>
In-Reply-To
<20110226050723.GA27864@elie>

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>
---
 color.c     |    9 +++++++
 color.h     |    3 ++
 wt-status.c |   74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 wt-status.h |    7 +++++
 4 files changed, 93 insertions(+), 0 deletions(-)
diff --git a/color.c b/color.c
index 6a5a54e..417cf8f 100644
--- a/color.c
+++ b/color.c
@@ -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)
 {
diff --git a/color.h b/color.h
index 170ff40..c0528cf 100644
--- a/color.h
+++ b/color.h
@@ -1,6 +1,8 @@
 #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);
 
diff --git a/wt-status.c b/wt-status.c
index 123582b..1abd7c3 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -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];
diff --git a/wt-status.h b/wt-status.h
index 20b17cf..204a519 100644
--- a/wt-status.h
+++ b/wt-status.h
@@ -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, ...)
+	__attribute__((format(printf, 3, 4)));
+void status_printf(struct wt_status *s, const char *color, const char *fmt, ...)
+	__attribute__((format(printf, 3, 4)));
+void status_printf_more(struct wt_status *s, const char *color, const char *fmt, ...)
+	__attribute__((format(printf, 3, 4)));
+
 #endif /* STATUS_H */
-- 
1.7.4.1
Previous: Jonathan NiederNext: Jonathan Nieder
Message 4 of 8 in “status, commit: separate "# " from the translatable part of output”
  1. 0/5 status, commit: separate "# " from the translatable part of outputJonathan Nieder, Feb 26, 2011
  2. 1/5 compat: provide a fallback va_copy definitionJonathan Nieder, Feb 26, 2011
  3. 2/5 strbuf: add strbuf_vaddfJonathan Nieder, Feb 26, 2011
  4. 3/5 wt-status: add helpers for printing wt-status linesJonathan Nieder, Feb 26, 2011
  5. 4/5 commit: refer to commit template as s->fpJonathan Nieder, Feb 26, 2011
  6. 5/5 commit, status: use status_printf{,_ln,_more} helpersJonathan Nieder, Feb 26, 2011
  7. Junio C HamanoFeb 27, 2011
  8. Jonathan NiederFeb 27, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.