1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "tool_setup.h"
24 #define ENABLE_CURLX_PRINTF
25 /* use our own printf() functions */
28 #include "tool_cfgable.h"
29 #include "tool_cb_prg.h"
31 #include "memdebug.h" /* keep this as LAST include */
34 ** callback for CURLOPT_PROGRESSFUNCTION
37 #define MAX_BARLENGTH 256
39 int tool_progress_cb(void *clientp,
40 double dltotal, double dlnow,
41 double ultotal, double ulnow)
43 /* The original progress-bar source code was written for curl by Lars Aas,
44 and this new edition inherits some of his concepts. */
46 char line[MAX_BARLENGTH+1];
52 struct timeval now = curlx_tvnow();
53 struct ProgressData *bar = (struct ProgressData *)clientp;
57 if(curlx_tvdiff(now, bar->prevtime) < 200) /* allow 5 Hz */
60 /* expected transfer size */
61 total = (curl_off_t)dltotal + (curl_off_t)ultotal + bar->initial_size;
63 /* we've come this far */
64 point = (curl_off_t)dlnow + (curl_off_t)ulnow + bar->initial_size;
67 /* we have got more than the expected total! */
70 /* simply count invokes */
74 curl_off_t prevblock = bar->prev / 1024;
75 curl_off_t thisblock = point / 1024;
76 while(thisblock > prevblock) {
77 fprintf(bar->out, "#");
81 else if(point != bar->prev) {
82 frac = (double)point / (double)total;
83 percent = frac * 100.0f;
84 barwidth = bar->width - 7;
85 num = (int) (((double)barwidth) * frac);
86 if(num > MAX_BARLENGTH)
88 memset(line, '#', num);
90 snprintf(format, sizeof(format), "\r%%-%ds %%5.1f%%%%", barwidth);
91 fprintf(bar->out, format, line, percent);
100 void progressbarinit(struct ProgressData *bar,
101 struct Configurable *config)
109 memset(bar, 0, sizeof(struct ProgressData));
111 /* pass this through to progress function so
112 * it can display progress towards total file
113 * not just the part that's left. (21-may-03, dbyron) */
114 if(config->use_resume)
115 bar->initial_size = config->resume_from;
117 /* TODO: get terminal width through ansi escapes or something similar.
118 try to update width when xterm is resized... - 19990617 larsa */
121 * OS/2 users most likely won't have this env var set, and besides that
122 * we're using our own way to determine screen width */
123 colp = curlx_getenv("COLUMNS");
126 long num = strtol(colp, &endptr, 10);
127 if((endptr != colp) && (endptr == colp + strlen(colp)) && (num > 0))
128 bar->width = (int)num;
137 * We use this emx library call to get the screen width, and subtract
138 * one from what we got in order to avoid a problem with the cursor
139 * advancing to the next line if we print a string that is as long as
140 * the screen is wide. */
143 bar->width = scr_size[0] - 1;
146 bar->out = config->errors;