-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprogressbar.c
260 lines (231 loc) · 7.96 KB
/
progressbar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*-
* Copyright (c) 2011-2014 Baptiste Daroussin <[email protected]>
* Copyright (c) 2011-2012 Julien Laffaye <[email protected]>
* Copyright (c) 2011 Will Andrews <[email protected]>
* Copyright (c) 2011-2012 Marin Atanasov Nikolov <[email protected]>
* Copyright (c) 2014 Vsevolod Stakhov <[email protected]>
* Copyright (c) 2015 Matthew Seaman <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer
* in this position and unchanged.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <libutil.h>
#include <utstring.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <unistd.h>
#define STALL_TIME 5
static char *progress_message = NULL;
static UT_string *msg_buf = NULL;
static int last_progress_percent = -1;
static bool progress_started = false;
static bool progress_interrupted = false;
static bool progress_debit = false;
static int64_t last_tick = 0;
static int64_t stalled;
static int64_t bytes_per_second;
static time_t last_update;
static time_t begin = 0;
static int add_deps_depth;
static bool signal_handler_installed = false;
static bool quiet = false;
/* units for format_size */
static const char *unit_SI[] = { " ", "k", "M", "G", "T", };
static void
format_rate_SI(char *buf, int size, off_t bytes)
{
int i;
bytes *= 100;
for (i = 0; bytes >= 100*1000 && unit_SI[i][0] != 'T'; i++)
bytes = (bytes + 500) / 1000;
if (i == 0) {
i++;
bytes = (bytes + 500) / 1000;
}
snprintf(buf, size, "%3lld.%1lld%s%s",
(long long) (bytes + 5) / 100,
(long long) (bytes + 5) / 10 % 10,
unit_SI[i],
i ? "B" : " ");
}
void
provides_progressbar_stop(void)
{
if (progress_started) {
if (!isatty(STDOUT_FILENO))
printf(" done");
putchar('\n');
}
last_progress_percent = -1;
progress_started = false;
progress_interrupted = false;
}
void
provides_progressbar_start(const char *pmsg) {
free(progress_message);
progress_message = NULL;
progress_debit = true;
if (quiet)
return;
if (pmsg != NULL)
progress_message = strdup(pmsg);
else {
progress_message = strdup(utstring_body(msg_buf));
}
last_progress_percent = -1;
last_tick = 0;
begin = last_update = time(NULL);
bytes_per_second = 0;
stalled = 0;
progress_started = true;
progress_interrupted = false;
if (!isatty(STDOUT_FILENO))
printf("%s: ", progress_message);
else
printf("%s: 0%%", progress_message);
}
static void
draw_progressbar(int64_t current, int64_t total)
{
int percent;
int64_t transferred;
time_t elapsed = 0, now = 0;
char buf[8];
int64_t bytes_left;
int cur_speed;
int hours, minutes, seconds;
float age_factor;
if (!progress_started) {
provides_progressbar_stop();
return;
}
if (progress_debit) {
now = time(NULL);
elapsed = (now >= last_update) ? now - last_update : 0;
}
percent = (total != 0) ? (current * 100. / total) : 100;
/**
* Wait for interval for debit bars to keep calc per second.
* If not debit, show on every % change, or if ticking after
* an interruption (which removed our progressbar output).
*/
if (current >= total || (progress_debit && elapsed >= 1) ||
(!progress_debit &&
(percent != last_progress_percent || progress_interrupted))) {
last_progress_percent = percent;
printf("\r%s: %3d%%", progress_message, percent);
if (progress_debit) {
transferred = current - last_tick;
last_tick = current;
bytes_left = total - current;
if (bytes_left <= 0) {
elapsed = now - begin;
/* Always show at least 1 second at end. */
if (elapsed == 0)
elapsed = 1;
/* Calculate true total speed when done */
transferred = total;
bytes_per_second = 0;
}
if (elapsed != 0)
cur_speed = (transferred / elapsed);
else
cur_speed = transferred;
#define AGE_FACTOR_SLOW_START 3
if (now - begin <= AGE_FACTOR_SLOW_START)
age_factor = 0.4;
else
age_factor = 0.9;
if (bytes_per_second != 0) {
bytes_per_second =
(bytes_per_second * age_factor) +
(cur_speed * (1.0 - age_factor));
} else
bytes_per_second = cur_speed;
humanize_number(buf, sizeof(buf),
current,"B", HN_AUTOSCALE, HN_IEC_PREFIXES);
printf(" %*s", (int)sizeof(buf), buf);
if (bytes_left > 0)
format_rate_SI(buf, sizeof(buf), transferred);
else /* Show overall speed when done */
format_rate_SI(buf, sizeof(buf),
bytes_per_second);
printf(" %s/s ", buf);
if (!transferred)
stalled += elapsed;
else
stalled = 0;
if (stalled >= STALL_TIME)
printf(" - stalled -");
else if (bytes_per_second == 0 && bytes_left > 0)
printf(" --:-- ETA");
else {
if (bytes_left > 0)
seconds = bytes_left / bytes_per_second;
else
seconds = elapsed;
hours = seconds / 3600;
seconds -= hours * 3600;
minutes = seconds / 60;
seconds -= minutes * 60;
if (hours != 0)
printf("%02d:%02d:%02d", hours,
minutes, seconds);
else
printf(" %02d:%02d", minutes, seconds);
if (bytes_left > 0)
printf(" ETA");
else
printf(" ");
last_update = now;
}
}
fflush(stdout);
}
if (current >= total)
return provides_progressbar_stop();
}
void
provides_progressbar_tick(int64_t current, int64_t total)
{
int percent;
if (!quiet && progress_started) {
if (isatty(STDOUT_FILENO))
draw_progressbar(current, total);
else {
if (progress_interrupted) {
printf("%s...", progress_message);
} else if (!getenv("NO_TICK")){
percent = (total != 0) ? (current * 100. / total) : 100;
if (last_progress_percent / 10 < percent / 10) {
last_progress_percent = percent;
printf(".");
fflush(stdout);
}
}
if (current >= total)
provides_progressbar_stop();
}
}
progress_interrupted = false;
}