| 110 | } |
| 111 | |
| 112 | static void display(struct progress *progress, uint64_t n, const char *done) |
| 113 | { |
| 114 | const char *tp; |
| 115 | struct strbuf *counters_sb = &progress->counters_sb; |
| 116 | int show_update = 0; |
| 117 | int update = !!progress_update; |
| 118 | int last_count_len = counters_sb->len; |
| 119 | |
| 120 | progress_update = 0; |
| 121 | |
| 122 | if (progress->delay && (!update || --progress->delay)) |
| 123 | return; |
| 124 | |
| 125 | progress->last_value = n; |
| 126 | tp = (progress->throughput) ? progress->throughput->display.buf : ""; |
| 127 | if (progress->total) { |
| 128 | unsigned percent = n * 100 / progress->total; |
| 129 | if (percent != progress->last_percent || update) { |
| 130 | progress->last_percent = percent; |
| 131 | |
| 132 | strbuf_reset(counters_sb); |
| 133 | strbuf_addf(counters_sb, |
| 134 | "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent, |
| 135 | (uintmax_t)n, (uintmax_t)progress->total, |
| 136 | tp); |
| 137 | show_update = 1; |
| 138 | } |
| 139 | } else if (update) { |
| 140 | strbuf_reset(counters_sb); |
| 141 | strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp); |
| 142 | show_update = 1; |
| 143 | } |
| 144 | |
| 145 | if (show_update) { |
| 146 | if (is_foreground_fd(fileno(stderr)) || done) { |
| 147 | const char *eol = done ? done : "\r"; |
| 148 | size_t clear_len = counters_sb->len < last_count_len ? |
| 149 | last_count_len - counters_sb->len + 1 : |
| 150 | 0; |
| 151 | /* The "+ 2" accounts for the ": ". */ |
| 152 | size_t progress_line_len = progress->title_len + |
| 153 | counters_sb->len + 2; |
| 154 | int cols = term_columns(); |
| 155 | |
| 156 | if (progress->split) { |
| 157 | fprintf(stderr, " %s%*s", counters_sb->buf, |
| 158 | (int) clear_len, eol); |
| 159 | } else if (!done && cols < progress_line_len) { |
| 160 | clear_len = progress->title_len + 1 < cols ? |
| 161 | cols - progress->title_len - 1 : 0; |
| 162 | fprintf(stderr, "%s:%*s\n %s%s", |
| 163 | progress->title, (int) clear_len, "", |
| 164 | counters_sb->buf, eol); |
| 165 | progress->split = 1; |
| 166 | } else { |
| 167 | fprintf(stderr, "%s: %s%*s", progress->title, |
| 168 | counters_sb->buf, (int) clear_len, eol); |
| 169 | } |
no test coverage detected