Skip to content

Commit d53ba84

Browse files
szedergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9219d12 commit d53ba84

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

progress.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct progress {
3636
unsigned delay;
3737
struct throughput *throughput;
3838
uint64_t start_ns;
39+
struct strbuf counters_sb;
3940
};
4041

4142
static volatile sig_atomic_t progress_update;
@@ -80,31 +81,39 @@ static int is_foreground_fd(int fd)
8081

8182
static void display(struct progress *progress, uint64_t n, const char *done)
8283
{
83-
const char *eol, *tp;
84+
const char *tp;
85+
struct strbuf *counters_sb = &progress->counters_sb;
86+
int show_update = 0;
8487

8588
if (progress->delay && (!progress_update || --progress->delay))
8689
return;
8790

8891
progress->last_value = n;
8992
tp = (progress->throughput) ? progress->throughput->display.buf : "";
90-
eol = done ? done : " \r";
9193
if (progress->total) {
9294
unsigned percent = n * 100 / progress->total;
9395
if (percent != progress->last_percent || progress_update) {
9496
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;
103104
}
104105
} 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) {
105112
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);
108117
fflush(stderr);
109118
}
110119
progress_update = 0;
@@ -207,6 +216,7 @@ static struct progress *start_progress_delay(const char *title, uint64_t total,
207216
progress->delay = delay;
208217
progress->throughput = NULL;
209218
progress->start_ns = getnanotime();
219+
strbuf_init(&progress->counters_sb, 0);
210220
set_progress_signal();
211221
return progress;
212222
}
@@ -250,6 +260,7 @@ void stop_progress_msg(struct progress **p_progress, const char *msg)
250260
free(buf);
251261
}
252262
clear_progress_signal();
263+
strbuf_release(&progress->counters_sb);
253264
if (progress->throughput)
254265
strbuf_release(&progress->throughput->display);
255266
free(progress->throughput);

0 commit comments

Comments
 (0)