]> git.ipfire.org Git - thirdparty/git.git/blobdiff - wt-status.c
wt-status: add helpers for printing wt-status lines
[thirdparty/git.git] / wt-status.c
index 9624865e21739139a9fef5ebc31196cbcd22011a..1abd7c33824e84d93479bf654261a1140988706e 100644 (file)
@@ -21,11 +21,89 @@ static char default_wt_status_colors[][COLOR_MAXLEN] = {
        GIT_COLOR_RED,    /* WT_STATUS_UNMERGED */
        GIT_COLOR_GREEN,  /* WT_STATUS_LOCAL_BRANCH */
        GIT_COLOR_RED,    /* WT_STATUS_REMOTE_BRANCH */
+       GIT_COLOR_NIL,    /* WT_STATUS_ONBRANCH */
 };
 
 static const char *color(int slot, struct wt_status *s)
 {
-       return s->use_color > 0 ? s->color_palette[slot] : "";
+       const char *c = s->use_color > 0 ? s->color_palette[slot] : "";
+       if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
+               c = s->color_palette[WT_STATUS_HEADER];
+       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)
@@ -88,7 +166,7 @@ static void wt_status_print_dirty_header(struct wt_status *s,
 {
        const char *c = color(WT_STATUS_HEADER, s);
 
-       color_fprintf_ln(s->fp, c, "# Changed but not updated:");
+       color_fprintf_ln(s->fp, c, "# Changes not staged for commit:");
        if (!advice_status_hints)
                return;
        if (!has_deleted)
@@ -625,7 +703,8 @@ static void wt_status_print_tracking(struct wt_status *s)
 
 void wt_status_print(struct wt_status *s)
 {
-       const char *branch_color = color(WT_STATUS_HEADER, s);
+       const char *branch_color = color(WT_STATUS_ONBRANCH, s);
+       const char *branch_status_color = color(WT_STATUS_HEADER, s);
 
        if (s->branch) {
                const char *on_what = "On branch ";
@@ -634,11 +713,12 @@ void wt_status_print(struct wt_status *s)
                        branch_name += 11;
                else if (!strcmp(branch_name, "HEAD")) {
                        branch_name = "";
-                       branch_color = color(WT_STATUS_NOBRANCH, s);
+                       branch_status_color = color(WT_STATUS_NOBRANCH, s);
                        on_what = "Not currently on any branch.";
                }
                color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
-               color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
+               color_fprintf(s->fp, branch_status_color, "%s", on_what);
+               color_fprintf_ln(s->fp, branch_color, "%s", branch_name);
                if (!s->is_initial)
                        wt_status_print_tracking(s);
        }