1 /* $NetBSD: progressbar.c,v 1.21 2009/04/12 10:18:52 lukem Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6 * Copyright (c) 1997-2009 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
51 static const char * const suffixes[] = {
53 "KiB", /* 2^10 Kibibyte */
54 "MiB", /* 2^20 Mebibyte */
55 "GiB", /* 2^30 Gibibyte */
56 "TiB", /* 2^40 Tebibyte */
57 "PiB", /* 2^50 Pebibyte */
58 "EiB", /* 2^60 Exbibyte */
61 #define NSUFFIXES nitems(suffixes)
62 #define SECSPERHOUR (60 * 60)
63 #define DEFAULT_TTYWIDTH 80
65 /* initialise progress meter structure */
67 progress_init(progress_t *prog, const char *prefix, uint64_t total)
69 struct winsize winsize;
72 (void) memset(prog, 0x0, sizeof(*prog));
74 prog->prefix = strdup(prefix);
75 prog->start = time(NULL);
76 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
77 winsize.ws_col != 0) {
78 prog->ttywidth = winsize.ws_col;
80 prog->ttywidth = DEFAULT_TTYWIDTH;
86 /* update the values in the progress meter */
88 progress_update(progress_t *prog, uint64_t done)
91 prog->percent = (prog->done * 100) / prog->size;
92 prog->now = time(NULL);
93 prog->elapsed = prog->now - prog->start;
94 if (done == 0 || prog->elapsed == 0 || prog->done / prog->elapsed == 0) {
97 prog->eta = prog->size / (prog->done / prog->elapsed) - prog->elapsed;
102 /* update the values in the progress meter */
104 progress_reset_size(progress_t *prog, uint64_t size)
110 /* make it look pretty at the end - display done bytes (usually total) */
112 progress_complete(progress_t *prog, uint64_t done)
114 progress_update(prog, done);
120 /* draw the progress meter */
122 progress_draw(progress_t *prog)
124 #define BAROVERHEAD 45 /* non `*' portion of progress bar */
126 * stars should contain at least
127 * sizeof(buf) - BAROVERHEAD entries
129 #define MIN_BAR_LEN 10
130 static const char stars[] =
131 "*****************************************************************************"
132 "*****************************************************************************"
133 "*****************************************************************************";
134 unsigned bytesabbrev;
137 uint64_t bytespersec;
147 prefix_len = strlen(prog->prefix);
148 barlength = MIN(sizeof(buf) - 1, (unsigned)prog->ttywidth) -
149 BAROVERHEAD - prefix_len;
150 if (barlength < MIN_BAR_LEN) {
153 * In this case the TTY width is too small or the prefix is
154 * too large for a progress bar. We'll try decreasing the
157 barlength = MIN_BAR_LEN;
158 tmp_prefix_len = MIN(sizeof(buf) - 1,(unsigned)prog->ttywidth) -
159 BAROVERHEAD - MIN_BAR_LEN;
160 if (tmp_prefix_len > 0)
161 prefix_len = tmp_prefix_len;
165 starc = (barlength * prog->percent) / 100;
166 abbrevsize = prog->done;
167 for (bytesabbrev = 0; abbrevsize >= 100000 && bytesabbrev < NSUFFIXES; bytesabbrev++) {
170 if (bytesabbrev == NSUFFIXES) {
174 if (prog->done > 0) {
175 bytespersec = prog->done;
176 if (prog->elapsed > 0) {
177 bytespersec /= prog->elapsed;
180 for (bpsabbrev = 1; bytespersec >= 1024000 && bpsabbrev < NSUFFIXES; bpsabbrev++) {
183 if (prog->done == 0 || prog->elapsed <= 0 || prog->done > prog->size) {
186 secsleft = prog->eta;
188 if ((secs = secsleft / SECSPERHOUR) > 0) {
189 (void) snprintf(hours, sizeof(hours), "%2lld:", (long long)secs);
191 (void) snprintf(hours, sizeof(hours), " ");
193 secs = secsleft % SECSPERHOUR;
194 len = snprintf(buf, sizeof(buf),
195 "\r%.*s %3lld%% |%.*s%*s| %5lld %-3s %3lld.%02d %.2sB/s %s%02d:%02d ETA",
196 prefix_len, (prog->prefix) ? prog->prefix : "",
197 (long long)prog->percent,
198 (int)starc, stars, (int)(barlength - starc), "",
199 (long long)abbrevsize,
200 suffixes[bytesabbrev],
201 (long long)(bytespersec / 1024),
202 (int)((bytespersec % 1024) * 100 / 1024),
205 (int)secs / 60, (int)secs % 60);
206 return (int)write(STDOUT_FILENO, buf, len);