2011-09-20 19:54:14 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2015-11-13 10:15:26 -05:00
|
|
|
* Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-09-20 19:54:14 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2012-04-06 17:35:15 -04:00
|
|
|
#include "tool_setup.h"
|
2011-09-20 19:54:14 -04:00
|
|
|
|
|
|
|
#define ENABLE_CURLX_PRINTF
|
|
|
|
/* use our own printf() functions */
|
|
|
|
#include "curlx.h"
|
|
|
|
|
|
|
|
#include "tool_cfgable.h"
|
2011-09-24 11:38:16 -04:00
|
|
|
#include "tool_cb_prg.h"
|
2013-07-31 09:36:56 -04:00
|
|
|
#include "tool_util.h"
|
2011-09-20 19:54:14 -04:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h" /* keep this as LAST include */
|
2011-09-20 19:54:14 -04:00
|
|
|
|
2011-09-24 11:38:16 -04:00
|
|
|
/*
|
2013-10-13 13:39:41 -04:00
|
|
|
** callback for CURLOPT_XFERINFOFUNCTION
|
2011-09-24 11:38:16 -04:00
|
|
|
*/
|
|
|
|
|
2011-11-09 16:50:36 -05:00
|
|
|
#define MAX_BARLENGTH 256
|
|
|
|
|
2011-09-24 11:38:16 -04:00
|
|
|
int tool_progress_cb(void *clientp,
|
2013-10-13 13:39:41 -04:00
|
|
|
curl_off_t dltotal, curl_off_t dlnow,
|
|
|
|
curl_off_t ultotal, curl_off_t ulnow)
|
2011-09-20 19:54:14 -04:00
|
|
|
{
|
|
|
|
/* The original progress-bar source code was written for curl by Lars Aas,
|
|
|
|
and this new edition inherits some of his concepts. */
|
|
|
|
|
2011-11-09 16:50:36 -05:00
|
|
|
char line[MAX_BARLENGTH+1];
|
2011-09-20 19:54:14 -04:00
|
|
|
char format[40];
|
|
|
|
double frac;
|
|
|
|
double percent;
|
|
|
|
int barwidth;
|
|
|
|
int num;
|
2013-07-31 09:36:56 -04:00
|
|
|
struct timeval now = tvnow();
|
2011-09-20 19:54:14 -04:00
|
|
|
struct ProgressData *bar = (struct ProgressData *)clientp;
|
2013-07-30 18:17:56 -04:00
|
|
|
curl_off_t total;
|
|
|
|
curl_off_t point;
|
|
|
|
|
2011-09-20 19:54:14 -04:00
|
|
|
/* expected transfer size */
|
2013-10-13 13:39:41 -04:00
|
|
|
total = dltotal + ultotal + bar->initial_size;
|
2011-09-20 19:54:14 -04:00
|
|
|
|
|
|
|
/* we've come this far */
|
2013-10-13 13:39:41 -04:00
|
|
|
point = dlnow + ulnow + bar->initial_size;
|
2011-09-20 19:54:14 -04:00
|
|
|
|
2014-01-18 16:45:49 -05:00
|
|
|
if(bar->calls && (tvdiff(now, bar->prevtime) < 100L) && point < total)
|
|
|
|
/* after first call, limit progress-bar updating to 10 Hz */
|
2014-01-06 14:32:33 -05:00
|
|
|
/* update when we're at 100% even if last update is less than 200ms ago */
|
|
|
|
return 0;
|
|
|
|
|
2011-09-20 19:54:14 -04:00
|
|
|
if(point > total)
|
|
|
|
/* we have got more than the expected total! */
|
|
|
|
total = point;
|
|
|
|
|
|
|
|
/* simply count invokes */
|
|
|
|
bar->calls++;
|
|
|
|
|
|
|
|
if(total < 1) {
|
|
|
|
curl_off_t prevblock = bar->prev / 1024;
|
|
|
|
curl_off_t thisblock = point / 1024;
|
|
|
|
while(thisblock > prevblock) {
|
|
|
|
fprintf(bar->out, "#");
|
|
|
|
prevblock++;
|
|
|
|
}
|
|
|
|
}
|
2012-04-16 18:45:20 -04:00
|
|
|
else if(point != bar->prev) {
|
2011-09-20 19:54:14 -04:00
|
|
|
frac = (double)point / (double)total;
|
|
|
|
percent = frac * 100.0f;
|
|
|
|
barwidth = bar->width - 7;
|
|
|
|
num = (int) (((double)barwidth) * frac);
|
2011-11-09 16:50:36 -05:00
|
|
|
if(num > MAX_BARLENGTH)
|
|
|
|
num = MAX_BARLENGTH;
|
2013-07-30 18:17:56 -04:00
|
|
|
memset(line, '#', num);
|
|
|
|
line[num] = '\0';
|
2011-11-09 16:50:36 -05:00
|
|
|
snprintf(format, sizeof(format), "\r%%-%ds %%5.1f%%%%", barwidth);
|
|
|
|
fprintf(bar->out, format, line, percent);
|
2011-09-20 19:54:14 -04:00
|
|
|
}
|
|
|
|
fflush(bar->out);
|
|
|
|
bar->prev = point;
|
2013-07-30 18:17:56 -04:00
|
|
|
bar->prevtime = now;
|
2011-09-20 19:54:14 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void progressbarinit(struct ProgressData *bar,
|
2014-02-23 07:59:59 -05:00
|
|
|
struct OperationConfig *config)
|
2011-09-20 19:54:14 -04:00
|
|
|
{
|
|
|
|
#ifdef __EMX__
|
|
|
|
/* 20000318 mgs */
|
|
|
|
int scr_size[2];
|
|
|
|
#endif
|
|
|
|
char *colp;
|
|
|
|
|
|
|
|
memset(bar, 0, sizeof(struct ProgressData));
|
|
|
|
|
|
|
|
/* pass this through to progress function so
|
|
|
|
* it can display progress towards total file
|
|
|
|
* not just the part that's left. (21-may-03, dbyron) */
|
|
|
|
if(config->use_resume)
|
|
|
|
bar->initial_size = config->resume_from;
|
|
|
|
|
|
|
|
#ifndef __EMX__
|
2015-11-13 10:15:26 -05:00
|
|
|
/* OS/2 users most likely won't have this env var set, and besides that
|
2011-09-20 19:54:14 -04:00
|
|
|
* we're using our own way to determine screen width */
|
|
|
|
colp = curlx_getenv("COLUMNS");
|
|
|
|
if(colp) {
|
|
|
|
char *endptr;
|
|
|
|
long num = strtol(colp, &endptr, 10);
|
|
|
|
if((endptr != colp) && (endptr == colp + strlen(colp)) && (num > 0))
|
|
|
|
bar->width = (int)num;
|
|
|
|
else
|
|
|
|
bar->width = 79;
|
|
|
|
curl_free(colp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bar->width = 79;
|
|
|
|
#else
|
2015-11-13 10:15:26 -05:00
|
|
|
/* We use this emx library call to get the screen width, and subtract
|
2011-09-20 19:54:14 -04:00
|
|
|
* one from what we got in order to avoid a problem with the cursor
|
|
|
|
* advancing to the next line if we print a string that is as long as
|
|
|
|
* the screen is wide. */
|
|
|
|
|
|
|
|
_scrsize(scr_size);
|
|
|
|
bar->width = scr_size[0] - 1;
|
|
|
|
#endif
|
|
|
|
|
2014-03-01 08:00:31 -05:00
|
|
|
bar->out = config->global->errors;
|
2011-09-20 19:54:14 -04:00
|
|
|
}
|