1cfc0a495SScott Long /* $NetBSD: progressbar.c,v 1.21 2009/04/12 10:18:52 lukem Exp $ */
2cfc0a495SScott Long
3cfc0a495SScott Long /*-
4*b61a5730SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
51de7b4b8SPedro F. Giffuni *
6cfc0a495SScott Long * Copyright (c) 1997-2009 The NetBSD Foundation, Inc.
7cfc0a495SScott Long * All rights reserved.
8cfc0a495SScott Long *
9cfc0a495SScott Long * This code is derived from software contributed to The NetBSD Foundation
10cfc0a495SScott Long * by Luke Mewburn.
11cfc0a495SScott Long *
12cfc0a495SScott Long * Redistribution and use in source and binary forms, with or without
13cfc0a495SScott Long * modification, are permitted provided that the following conditions
14cfc0a495SScott Long * are met:
15cfc0a495SScott Long * 1. Redistributions of source code must retain the above copyright
16cfc0a495SScott Long * notice, this list of conditions and the following disclaimer.
17cfc0a495SScott Long * 2. Redistributions in binary form must reproduce the above copyright
18cfc0a495SScott Long * notice, this list of conditions and the following disclaimer in the
19cfc0a495SScott Long * documentation and/or other materials provided with the distribution.
20cfc0a495SScott Long *
21cfc0a495SScott Long * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22cfc0a495SScott Long * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23cfc0a495SScott Long * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24cfc0a495SScott Long * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25cfc0a495SScott Long * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26cfc0a495SScott Long * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27cfc0a495SScott Long * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28cfc0a495SScott Long * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29cfc0a495SScott Long * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30cfc0a495SScott Long * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31cfc0a495SScott Long * POSSIBILITY OF SUCH DAMAGE.
32cfc0a495SScott Long */
33cfc0a495SScott Long
34cfc0a495SScott Long #include <sys/types.h>
35cfc0a495SScott Long #include <sys/param.h>
36cfc0a495SScott Long #include <sys/ioctl.h>
37cfc0a495SScott Long
38cfc0a495SScott Long #include <errno.h>
39cfc0a495SScott Long #include <stdio.h>
40cfc0a495SScott Long #include <stdlib.h>
41cfc0a495SScott Long #include <string.h>
42cfc0a495SScott Long #include <termios.h>
43cfc0a495SScott Long #include <time.h>
44cfc0a495SScott Long #include <unistd.h>
45cfc0a495SScott Long
46cfc0a495SScott Long #include <sys/cdefs.h>
47cfc0a495SScott Long #include "progress.h"
48cfc0a495SScott Long
49cfc0a495SScott Long static const char * const suffixes[] = {
50cfc0a495SScott Long "", /* 2^0 (byte) */
51cfc0a495SScott Long "KiB", /* 2^10 Kibibyte */
52cfc0a495SScott Long "MiB", /* 2^20 Mebibyte */
53cfc0a495SScott Long "GiB", /* 2^30 Gibibyte */
54cfc0a495SScott Long "TiB", /* 2^40 Tebibyte */
55cfc0a495SScott Long "PiB", /* 2^50 Pebibyte */
56cfc0a495SScott Long "EiB", /* 2^60 Exbibyte */
57cfc0a495SScott Long };
58cfc0a495SScott Long
59286d1971SMarcelo Araujo #define NSUFFIXES nitems(suffixes)
60cfc0a495SScott Long #define SECSPERHOUR (60 * 60)
61cfc0a495SScott Long #define DEFAULT_TTYWIDTH 80
62cfc0a495SScott Long
63cfc0a495SScott Long /* initialise progress meter structure */
64cfc0a495SScott Long int
progress_init(progress_t * prog,const char * prefix,uint64_t total)65cfc0a495SScott Long progress_init(progress_t *prog, const char *prefix, uint64_t total)
66cfc0a495SScott Long {
67cfc0a495SScott Long struct winsize winsize;
68cfc0a495SScott Long int oerrno = errno;
69cfc0a495SScott Long
70cfc0a495SScott Long (void) memset(prog, 0x0, sizeof(*prog));
71cfc0a495SScott Long prog->size = total;
72cfc0a495SScott Long prog->prefix = strdup(prefix);
73cfc0a495SScott Long prog->start = time(NULL);
74cfc0a495SScott Long if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
75cfc0a495SScott Long winsize.ws_col != 0) {
76cfc0a495SScott Long prog->ttywidth = winsize.ws_col;
77cfc0a495SScott Long } else {
78cfc0a495SScott Long prog->ttywidth = DEFAULT_TTYWIDTH;
79cfc0a495SScott Long }
80cfc0a495SScott Long errno = oerrno;
81cfc0a495SScott Long return 1;
82cfc0a495SScott Long }
83cfc0a495SScott Long
84cfc0a495SScott Long /* update the values in the progress meter */
85cfc0a495SScott Long int
progress_update(progress_t * prog,uint64_t done)86cfc0a495SScott Long progress_update(progress_t *prog, uint64_t done)
87cfc0a495SScott Long {
88cfc0a495SScott Long prog->done = done;
89cfc0a495SScott Long prog->percent = (prog->done * 100) / prog->size;
90cfc0a495SScott Long prog->now = time(NULL);
91cfc0a495SScott Long prog->elapsed = prog->now - prog->start;
92cfc0a495SScott Long if (done == 0 || prog->elapsed == 0 || prog->done / prog->elapsed == 0) {
93cfc0a495SScott Long prog->eta = 0;
94cfc0a495SScott Long } else {
95cfc0a495SScott Long prog->eta = prog->size / (prog->done / prog->elapsed) - prog->elapsed;
96cfc0a495SScott Long }
97cfc0a495SScott Long return 1;
98cfc0a495SScott Long }
99cfc0a495SScott Long
100cfc0a495SScott Long /* update the values in the progress meter */
101cfc0a495SScott Long int
progress_reset_size(progress_t * prog,uint64_t size)102cfc0a495SScott Long progress_reset_size(progress_t *prog, uint64_t size)
103cfc0a495SScott Long {
104cfc0a495SScott Long prog->size = size;
105cfc0a495SScott Long return 1;
106cfc0a495SScott Long }
107cfc0a495SScott Long
108cfc0a495SScott Long /* make it look pretty at the end - display done bytes (usually total) */
109cfc0a495SScott Long int
progress_complete(progress_t * prog,uint64_t done)110cfc0a495SScott Long progress_complete(progress_t *prog, uint64_t done)
111cfc0a495SScott Long {
112cfc0a495SScott Long progress_update(prog, done);
113cfc0a495SScott Long progress_draw(prog);
114cfc0a495SScott Long printf("\n");
115cfc0a495SScott Long return 1;
116cfc0a495SScott Long }
117cfc0a495SScott Long
118cfc0a495SScott Long /* draw the progress meter */
119cfc0a495SScott Long int
progress_draw(progress_t * prog)120cfc0a495SScott Long progress_draw(progress_t *prog)
121cfc0a495SScott Long {
122cfc0a495SScott Long #define BAROVERHEAD 45 /* non `*' portion of progress bar */
123cfc0a495SScott Long /*
124cfc0a495SScott Long * stars should contain at least
125cfc0a495SScott Long * sizeof(buf) - BAROVERHEAD entries
126cfc0a495SScott Long */
1270e358df0SKenneth D. Merry #define MIN_BAR_LEN 10
128cfc0a495SScott Long static const char stars[] =
129cfc0a495SScott Long "*****************************************************************************"
130cfc0a495SScott Long "*****************************************************************************"
131cfc0a495SScott Long "*****************************************************************************";
132cfc0a495SScott Long unsigned bytesabbrev;
133cfc0a495SScott Long unsigned bpsabbrev;
134cfc0a495SScott Long int64_t secs;
135cfc0a495SScott Long uint64_t bytespersec;
136cfc0a495SScott Long uint64_t abbrevsize;
137cfc0a495SScott Long int64_t secsleft;
1380e358df0SKenneth D. Merry ssize_t barlength;
139cfc0a495SScott Long size_t starc;
140cfc0a495SScott Long char hours[12];
141cfc0a495SScott Long char buf[256];
142cfc0a495SScott Long int len;
1430e358df0SKenneth D. Merry int prefix_len;
144cfc0a495SScott Long
1450e358df0SKenneth D. Merry prefix_len = strlen(prog->prefix);
1460e358df0SKenneth D. Merry barlength = MIN(sizeof(buf) - 1, (unsigned)prog->ttywidth) -
1470e358df0SKenneth D. Merry BAROVERHEAD - prefix_len;
1480e358df0SKenneth D. Merry if (barlength < MIN_BAR_LEN) {
1490e358df0SKenneth D. Merry int tmp_prefix_len;
1500e358df0SKenneth D. Merry /*
1510e358df0SKenneth D. Merry * In this case the TTY width is too small or the prefix is
1520e358df0SKenneth D. Merry * too large for a progress bar. We'll try decreasing the
1530e358df0SKenneth D. Merry * prefix length.
1540e358df0SKenneth D. Merry */
1550e358df0SKenneth D. Merry barlength = MIN_BAR_LEN;
1560e358df0SKenneth D. Merry tmp_prefix_len = MIN(sizeof(buf) - 1,(unsigned)prog->ttywidth) -
1570e358df0SKenneth D. Merry BAROVERHEAD - MIN_BAR_LEN;
1580e358df0SKenneth D. Merry if (tmp_prefix_len > 0)
1590e358df0SKenneth D. Merry prefix_len = tmp_prefix_len;
1600e358df0SKenneth D. Merry else
1610e358df0SKenneth D. Merry prefix_len = 0;
1620e358df0SKenneth D. Merry }
163cfc0a495SScott Long starc = (barlength * prog->percent) / 100;
164cfc0a495SScott Long abbrevsize = prog->done;
165cfc0a495SScott Long for (bytesabbrev = 0; abbrevsize >= 100000 && bytesabbrev < NSUFFIXES; bytesabbrev++) {
166cfc0a495SScott Long abbrevsize >>= 10;
167cfc0a495SScott Long }
168cfc0a495SScott Long if (bytesabbrev == NSUFFIXES) {
169cfc0a495SScott Long bytesabbrev--;
170cfc0a495SScott Long }
171cfc0a495SScott Long bytespersec = 0;
172cfc0a495SScott Long if (prog->done > 0) {
173cfc0a495SScott Long bytespersec = prog->done;
174cfc0a495SScott Long if (prog->elapsed > 0) {
175cfc0a495SScott Long bytespersec /= prog->elapsed;
176cfc0a495SScott Long }
177cfc0a495SScott Long }
178cfc0a495SScott Long for (bpsabbrev = 1; bytespersec >= 1024000 && bpsabbrev < NSUFFIXES; bpsabbrev++) {
179cfc0a495SScott Long bytespersec >>= 10;
180cfc0a495SScott Long }
181cfc0a495SScott Long if (prog->done == 0 || prog->elapsed <= 0 || prog->done > prog->size) {
182cfc0a495SScott Long secsleft = 0;
183cfc0a495SScott Long } else {
184cfc0a495SScott Long secsleft = prog->eta;
185cfc0a495SScott Long }
186cfc0a495SScott Long if ((secs = secsleft / SECSPERHOUR) > 0) {
187cfc0a495SScott Long (void) snprintf(hours, sizeof(hours), "%2lld:", (long long)secs);
188cfc0a495SScott Long } else {
189cfc0a495SScott Long (void) snprintf(hours, sizeof(hours), " ");
190cfc0a495SScott Long }
191cfc0a495SScott Long secs = secsleft % SECSPERHOUR;
192cfc0a495SScott Long len = snprintf(buf, sizeof(buf),
1930e358df0SKenneth D. Merry "\r%.*s %3lld%% |%.*s%*s| %5lld %-3s %3lld.%02d %.2sB/s %s%02d:%02d ETA",
1940e358df0SKenneth D. Merry prefix_len, (prog->prefix) ? prog->prefix : "",
195cfc0a495SScott Long (long long)prog->percent,
196cfc0a495SScott Long (int)starc, stars, (int)(barlength - starc), "",
197cfc0a495SScott Long (long long)abbrevsize,
198cfc0a495SScott Long suffixes[bytesabbrev],
199cfc0a495SScott Long (long long)(bytespersec / 1024),
200cfc0a495SScott Long (int)((bytespersec % 1024) * 100 / 1024),
201cfc0a495SScott Long suffixes[bpsabbrev],
202cfc0a495SScott Long hours,
203cfc0a495SScott Long (int)secs / 60, (int)secs % 60);
204cfc0a495SScott Long return (int)write(STDOUT_FILENO, buf, len);
205cfc0a495SScott Long }
206