progress: assemble percentage and counters in a strbuf before printing

The following patches in this series want to handle the progress bar's title and changing parts (i.e. the counter and the optional percentage and throughput combined) differently, and need to know the length of the changing parts of the previously displayed progress bar. To prepare for those changes assemble the changing parts in a separate strbuf kept in 'struct progress' before printing. Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Apr 5, 2019 at 02:45 UTC d53ba841d4feec0096f5f019ae2d304f1edd226e
1 file changed +23 -12
progress.c
+23 -12
@@ -36,6 +36,7 @@ struct progress {
36 unsigned delay;
37 struct throughput *throughput;
38 uint64_t start_ns;
39 + struct strbuf counters_sb;
40 };
41
42 static volatile sig_atomic_t progress_update;
@@ -80,31 +81,39 @@ static int is_foreground_fd(int fd)
81
82 static void display(struct progress *progress, uint64_t n, const char *done)
83 {
83 - const char *eol, *tp;
84 + const char *tp;
85 + struct strbuf *counters_sb = &progress->counters_sb;
86 + int show_update = 0;
87
88 if (progress->delay && (!progress_update || --progress->delay))
89 return;
90
91 progress->last_value = n;
92 tp = (progress->throughput) ? progress->throughput->display.buf : "";
90 - eol = done ? done : " \r";
93 if (progress->total) {
94 unsigned percent = n * 100 / progress->total;
95 if (percent != progress->last_percent || progress_update) {
96 progress->last_percent = percent;
95 - if (is_foreground_fd(fileno(stderr)) || done) {
96 - fprintf(stderr, "%s: %3u%% (%"PRIuMAX"/%"PRIuMAX")%s%s",
97 - progress->title, percent,
98 - (uintmax_t)n, (uintmax_t)progress->total,
99 - tp, eol);
100 - fflush(stderr);
101 - }
102 - progress_update = 0;
97 +
98 + strbuf_reset(counters_sb);
99 + strbuf_addf(counters_sb,
100 + "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent,
101 + (uintmax_t)n, (uintmax_t)progress->total,
102 + tp);
103 + show_update = 1;
104 }
105 } else if (progress_update) {
106 + strbuf_reset(counters_sb);
107 + strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp);
108 + show_update = 1;
109 + }
110 +
111 + if (show_update) {
112 if (is_foreground_fd(fileno(stderr)) || done) {
106 - fprintf(stderr, "%s: %"PRIuMAX"%s%s",
107 - progress->title, (uintmax_t)n, tp, eol);
113 + const char *eol = done ? done : " \r";
114 +
115 + fprintf(stderr, "%s: %s%s", progress->title,
116 + counters_sb->buf, eol);
117 fflush(stderr);
118 }
119 progress_update = 0;
@@ -207,6 +216,7 @@ static struct progress *start_progress_delay(const char *title, uint64_t total,
216 progress->delay = delay;
217 progress->throughput = NULL;
218 progress->start_ns = getnanotime();
219 + strbuf_init(&progress->counters_sb, 0);
220 set_progress_signal();
221 return progress;
222 }
@@ -250,6 +260,7 @@ void stop_progress_msg(struct progress **p_progress, const char *msg)
260 free(buf);
261 }
262 clear_progress_signal();
263 + strbuf_release(&progress->counters_sb);
264 if (progress->throughput)
265 strbuf_release(&progress->throughput->display);
266 free(progress->throughput);