advice.c contains a useful code snippet which takes a multi-line string
and prints the lines, prefixing and suffixing each line with two
constant strings. This was originally added in 23cb5bf3b3 (i18n of
multi-line advice messages, 2011-12-22) to produce such output:

hint: some multi-line advice
hint: prefixed with "hint: "

The prefix is actually colored after 960786e761 (push: colorize errors,
2018-04-21) and each line has a suffix for resetting the color.

The next commit will teach the same "prefix all the lines"-trick to the
code that produces, e.g., "warning: "-messages. In preparation for that,
extract the code for printing the individual lines and expose it through
git-compat-util.h.

Signed-off-by: Martin Ågren <martin.ag...@gmail.com>
---
I'm open for suggestions on the naming of `prefix_suffix_lines()`...

 git-compat-util.h |  8 ++++++++
 advice.c          | 18 ++++++++----------
 usage.c           | 18 ++++++++++++++++++
 3 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/git-compat-util.h b/git-compat-util.h
index f9e4c5f9bc..23445f7ab9 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -415,6 +415,14 @@ static inline char *git_find_last_dir_sep(const char *path)
 struct strbuf;
 
 /* General helper functions */
+
+/*
+ * Write the message to the file, prefixing and suffixing
+ * each line with `prefix` resp. `suffix`.
+ */
+void prefix_suffix_lines(FILE *f, const char *prefix,
+                        const char *message, const char *suffix);
+
 extern void vreportf(const char *prefix, const char *err, va_list params);
 extern NORETURN void usage(const char *err);
 extern NORETURN void usagef(const char *err, ...) __attribute__((format 
(printf, 1, 2)));
diff --git a/advice.c b/advice.c
index 370a56d054..ffb29e7ef4 100644
--- a/advice.c
+++ b/advice.c
@@ -79,24 +79,22 @@ static struct {
 
 void advise(const char *advice, ...)
 {
+       struct strbuf prefix = STRBUF_INIT;
        struct strbuf buf = STRBUF_INIT;
        va_list params;
-       const char *cp, *np;
+
+       strbuf_addf(&prefix, _("%shint: "),
+                   advise_get_color(ADVICE_COLOR_HINT));
 
        va_start(params, advice);
        strbuf_vaddf(&buf, advice, params);
        va_end(params);
 
-       for (cp = buf.buf; *cp; cp = np) {
-               np = strchrnul(cp, '\n');
-               fprintf(stderr, _("%shint: %.*s%s\n"),
-                       advise_get_color(ADVICE_COLOR_HINT),
-                       (int)(np - cp), cp,
-                       advise_get_color(ADVICE_COLOR_RESET));
-               if (*np)
-                       np++;
-       }
+       prefix_suffix_lines(stderr, prefix.buf, buf.buf,
+                           advise_get_color(ADVICE_COLOR_RESET));
+
        strbuf_release(&buf);
+       strbuf_release(&prefix);
 }
 
 int git_default_advice_config(const char *var, const char *value)
diff --git a/usage.c b/usage.c
index cdd534c9df..80f9c1d14b 100644
--- a/usage.c
+++ b/usage.c
@@ -6,6 +6,24 @@
 #include "git-compat-util.h"
 #include "cache.h"
 
+void prefix_suffix_lines(FILE *f,
+                        const char *prefix,
+                        const char *message,
+                        const char *suffix)
+{
+       const char *cp, *np;
+
+       for (cp = message; *cp; cp = np) {
+               np = strchrnul(cp, '\n');
+               fprintf(f, "%s%.*s%s\n",
+                       prefix,
+                       (int)(np - cp), cp,
+                       suffix);
+               if (*np)
+                       np++;
+       }
+}
+
 void vreportf(const char *prefix, const char *err, va_list params)
 {
        char msg[4096];
-- 
2.17.0.1181.g093e983b05

Reply via email to