blob: 40608b0479b88edce2d89d43a2620e26326cc095 [file] [log] [blame]
Magnus Dammf5914992009-11-08 16:34:43 +01001/* vi: set sw=4 ts=4: */
2/*
3 * Progress bar code.
4 */
5/* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
6 * much of which was blatantly stolen from openssh.
7 */
8/*-
9 * Copyright (c) 1992, 1993
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020010 * The Regents of the University of California. All rights reserved.
Magnus Dammf5914992009-11-08 16:34:43 +010011 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
20 *
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020021 * 3. BSD Advertising Clause omitted per the July 22, 1999 licensing change
22 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
Magnus Dammf5914992009-11-08 16:34:43 +010023 *
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40#include "libbb.h"
41#include "unicode.h"
42
43enum {
44 /* Seconds when xfer considered "stalled" */
45 STALLTIME = 5
46};
47
48static unsigned int get_tty2_width(void)
49{
50 unsigned width;
51 get_terminal_width_height(2, &width, NULL);
52 return width;
53}
54
55void FAST_FUNC bb_progress_init(bb_progress_t *p)
56{
57 p->start_sec = monotonic_sec();
58 p->lastupdate_sec = p->start_sec;
59 p->lastsize = 0;
Denys Vlasenkocbcc1232010-03-05 23:38:54 +010060 p->inited = 1;
Magnus Dammf5914992009-11-08 16:34:43 +010061}
62
63void FAST_FUNC bb_progress_update(bb_progress_t *p,
64 const char *curfile,
65 off_t beg_range,
66 off_t transferred,
67 off_t totalsize)
68{
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +020069 uoff_t beg_and_transferred;
Magnus Dammf5914992009-11-08 16:34:43 +010070 unsigned since_last_update, elapsed;
71 unsigned ratio;
72 int barlength, i;
73
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +020074 /* totalsize == 0 if it is unknown */
75
76 elapsed = monotonic_sec();
77 since_last_update = elapsed - p->lastupdate_sec;
78 /* Do not update on every call
Denys Vlasenkoda0df472010-08-08 04:21:50 +020079 * (we can be called on every network read!) */
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +020080 if (since_last_update == 0 && !totalsize)
Denys Vlasenko84dba9c2011-01-10 12:51:44 +010081 return;
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +020082
83 beg_and_transferred = beg_range + transferred;
Magnus Dammf5914992009-11-08 16:34:43 +010084 ratio = 100;
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +020085 if (beg_and_transferred < totalsize) {
86 /* Do not update on every call
Denys Vlasenkoda0df472010-08-08 04:21:50 +020087 * (we can be called on every network read!) */
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +020088 if (since_last_update == 0)
89 return;
Magnus Dammf5914992009-11-08 16:34:43 +010090 /* long long helps to have it working even if !LFS */
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +020091 ratio = 100ULL * beg_and_transferred / (uoff_t)totalsize;
Magnus Dammf5914992009-11-08 16:34:43 +010092 }
93
Denys Vlasenko19158a82010-03-26 14:06:56 +010094#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko28055022010-01-04 20:49:58 +010095 init_unicode();
Magnus Dammf5914992009-11-08 16:34:43 +010096 {
Denys Vlasenkodc7e5c42011-01-11 13:08:28 +010097 char *buf = unicode_conv_to_printable_fixedwidth(/*NULL,*/ curfile, 20);
Denys Vlasenko0f44c082011-01-10 16:10:29 +010098 fprintf(stderr, "\r%s%4u%% ", buf, ratio);
Magnus Dammf5914992009-11-08 16:34:43 +010099 free(buf);
100 }
101#else
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +0200102 fprintf(stderr, "\r%-20.20s%4u%% ", curfile, ratio);
Magnus Dammf5914992009-11-08 16:34:43 +0100103#endif
104
105 barlength = get_tty2_width() - 49;
106 if (barlength > 0) {
107 /* god bless gcc for variable arrays :) */
Denys Vlasenkoda0df472010-08-08 04:21:50 +0200108 char buf[barlength + 1];
109 unsigned stars = (unsigned)barlength * ratio / (unsigned)100;
110 memset(buf, ' ', barlength);
111 buf[barlength] = '\0';
112 memset(buf, '*', stars);
113 fprintf(stderr, "|%s|", buf);
Magnus Dammf5914992009-11-08 16:34:43 +0100114 }
Denys Vlasenkoda0df472010-08-08 04:21:50 +0200115
Magnus Dammf5914992009-11-08 16:34:43 +0100116 i = 0;
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +0200117 while (beg_and_transferred >= 100000) {
Magnus Dammf5914992009-11-08 16:34:43 +0100118 i++;
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +0200119 beg_and_transferred >>= 10;
Magnus Dammf5914992009-11-08 16:34:43 +0100120 }
121 /* see http://en.wikipedia.org/wiki/Tera */
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +0200122 fprintf(stderr, "%6u%c ", (unsigned)beg_and_transferred, " kMGTPEZY"[i]);
123#define beg_and_transferred dont_use_beg_and_transferred_below
Magnus Dammf5914992009-11-08 16:34:43 +0100124
Magnus Dammf5914992009-11-08 16:34:43 +0100125 if (transferred > p->lastsize) {
126 p->lastupdate_sec = elapsed;
127 p->lastsize = transferred;
128 if (since_last_update >= STALLTIME) {
129 /* We "cut off" these seconds from elapsed time
130 * by adjusting start time */
131 p->start_sec += since_last_update;
132 }
133 since_last_update = 0; /* we are un-stalled now */
134 }
135 elapsed -= p->start_sec; /* now it's "elapsed since start" */
136
137 if (since_last_update >= STALLTIME) {
138 fprintf(stderr, " - stalled -");
139 } else {
140 off_t to_download = totalsize - beg_range;
141 if (!totalsize || transferred <= 0 || (int)elapsed <= 0 || transferred > to_download) {
142 fprintf(stderr, "--:--:-- ETA");
143 } else {
144 /* to_download / (transferred/elapsed) - elapsed: */
Magnus Dammf5914992009-11-08 16:34:43 +0100145 /* (long long helps to have working ETA even if !LFS) */
Denys Vlasenkoda0df472010-08-08 04:21:50 +0200146 unsigned eta = (unsigned long long)to_download*elapsed/(uoff_t)transferred - elapsed;
147 unsigned secs = eta % 3600;
Denys Vlasenko0f44c082011-01-10 16:10:29 +0100148 unsigned hours = eta / 3600;
149 fprintf(stderr, "%02u:%02u:%02u ETA", hours, secs / 60, secs % 60);
Magnus Dammf5914992009-11-08 16:34:43 +0100150 }
151 }
152}