Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
curl: progress bar refresh, get width using ioctl()
Get screen width from the environment variable COLUMNS first, if set. If
not, use ioctl(). If nether works, assume 79.

Closes #2242

The "refresh" is for the -# output when no total transfer size is
known. It will now only use a single updated line even for this case:

The "-=O=-" ship moves when data is transferred. The four flying
"hashes" move (on a sine wave) on each refresh, independent of data.
  • Loading branch information
bagder committed Jan 22, 2018
1 parent 9d82cde commit 993dd56
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 21 deletions.
108 changes: 89 additions & 19 deletions src/tool_cb_prg.c
Expand Up @@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2014, 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
Expand All @@ -21,6 +21,10 @@
***************************************************************************/
#include "tool_setup.h"

#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif

#define ENABLE_CURLX_PRINTF
/* use our own printf() functions */
#include "curlx.h"
Expand All @@ -31,6 +35,65 @@

#include "memdebug.h" /* keep this as LAST include */

/* 200 values generated by this perl code:
my $pi = 3.1415;
foreach my $i (1 .. 200) {
printf "%d, ", sin($i/200 * 2 * $pi) * 5000 + 5000;
}
*/
static const unsigned int sinus[] = {
5157, 5313, 5470, 5626, 5782, 5936, 6090, 6243, 6394, 6545, 6693, 6840, 6985,
7128, 7269, 7408, 7545, 7679, 7810, 7938, 8064, 8187, 8306, 8422, 8535, 8644,
8750, 8852, 8950, 9045, 9135, 9221, 9303, 9381, 9454, 9524, 9588, 9648, 9704,
9755, 9801, 9842, 9879, 9911, 9938, 9960, 9977, 9990, 9997, 9999, 9997, 9990,
9977, 9960, 9938, 9911, 9879, 9842, 9801, 9755, 9704, 9648, 9588, 9524, 9455,
9381, 9303, 9221, 9135, 9045, 8950, 8852, 8750, 8645, 8535, 8422, 8306, 8187,
8064, 7939, 7810, 7679, 7545, 7409, 7270, 7129, 6986, 6841, 6694, 6545, 6395,
6243, 6091, 5937, 5782, 5627, 5470, 5314, 5157, 5000, 4843, 4686, 4529, 4373,
4218, 4063, 3909, 3757, 3605, 3455, 3306, 3159, 3014, 2871, 2730, 2591, 2455,
2321, 2190, 2061, 1935, 1813, 1693, 1577, 1464, 1355, 1249, 1147, 1049, 955,
864, 778, 696, 618, 545, 476, 411, 351, 295, 244, 198, 157, 120, 88, 61, 39,
22, 9, 2, 0, 2, 9, 22, 39, 61, 88, 120, 156, 198, 244, 295, 350, 410, 475,
544, 618, 695, 777, 864, 954, 1048, 1146, 1248, 1354, 1463, 1576, 1692, 1812,
1934, 2060, 2188, 2320, 2454, 2590, 2729, 2870, 3013, 3158, 3305, 3454, 3604,
3755, 3908, 4062, 4216, 4372, 4528, 4685, 4842, 4999
};

static void fly(struct ProgressData *bar, bool moved)
{
char buf[256];
int pos;
int check = bar->width - 2;

snprintf(buf, sizeof(buf), "%*s\r", bar->width-1, " ");
memcpy(&buf[bar->bar], "-=O=-", 5);

pos = sinus[bar->tick%200] / (10000 / check);
buf[pos] = '#';
pos = sinus[(bar->tick + 5)%200] / (10000 / check);
buf[pos] = '#';
pos = sinus[(bar->tick + 10)%200] / (10000 / check);
buf[pos] = '#';
pos = sinus[(bar->tick + 15)%200] / (10000 / check);
buf[pos] = '#';

fputs(buf, stderr);

This comment has been minimized.

Copy link
@gvanem

gvanem Jan 24, 2018

Contributor

Shouldn't this be fputs(buf, bar->out);?

This comment has been minimized.

Copy link
@bagder

bagder Jan 24, 2018

Author Member

should indeed, thanks. Fixed now!

bar->tick += 2;
if(bar->tick >= 200)
bar->tick -= 200;

bar->bar += (moved?bar->barmove:0);
if(bar->bar >= (bar->width - 6)) {
bar->barmove = -1;
bar->bar = bar->width - 6;
}
else if(bar->bar < 0) {
bar->barmove = 1;
bar->bar = 0;
}
}

/*
** callback for CURLOPT_XFERINFOFUNCTION
*/
Expand Down Expand Up @@ -74,27 +137,17 @@ int tool_progress_cb(void *clientp,
}
else {
/* total is unknown */
if(bar->prev/1024 == point/1024)
/* the same kilobyte level as last invoke */
return 0;
else if(tvdiff(now, bar->prevtime) < 100L)
if(tvdiff(now, bar->prevtime) < 100L)
/* limit progress-bar updating to 10 Hz */
return 0;
fly(bar, point != bar->prev);
}
}

/* 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++;
}
}
else if(point != bar->prev) {
if((total > 0) && (point != bar->prev)) {
if(point > total)
/* we have got more than the expected total! */
total = point;
Expand All @@ -121,7 +174,6 @@ void progressbarinit(struct ProgressData *bar,
struct OperationConfig *config)
{
char *colp;

memset(bar, 0, sizeof(struct ProgressData));

/* pass this through to progress function so
Expand All @@ -134,14 +186,32 @@ void progressbarinit(struct ProgressData *bar,
if(colp) {
char *endptr;
long num = strtol(colp, &endptr, 10);
if((endptr != colp) && (endptr == colp + strlen(colp)) && (num > 0))
if((endptr != colp) && (endptr == colp + strlen(colp)) && (num > 20))
bar->width = (int)num;
else
bar->width = 79;
curl_free(colp);
}
else

if(!bar->width) {
int cols = 0;

#ifdef TIOCGSIZE
struct ttysize ts;
if(!ioctl(STDIN_FILENO, TIOCGSIZE, &ts))
cols = ts.ts_cols;
#elif defined(TIOCGWINSZ)
struct winsize ts;
if(!ioctl(STDIN_FILENO, TIOCGWINSZ, &ts))
cols = ts.ws_col;
#endif /* TIOCGSIZE */
bar->width = cols;
}

if(!bar->width)
bar->width = 79;
else if(bar->width > MAX_BARLENGTH)
bar->width = MAX_BARLENGTH;

bar->out = config->global->errors;
bar->tick = 150;
bar->barmove = 1;
}
5 changes: 4 additions & 1 deletion src/tool_cb_prg.h
Expand Up @@ -7,7 +7,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
Expand All @@ -33,6 +33,9 @@ struct ProgressData {
int width;
FILE *out; /* where to write everything to */
curl_off_t initial_size;
unsigned int tick;
int bar;
int barmove;
};

void progressbarinit(struct ProgressData *bar,
Expand Down
3 changes: 2 additions & 1 deletion tests/runtests.pl
Expand Up @@ -6,7 +6,7 @@
# | (__| |_| | _ <| |___
# \___|\___/|_| \_\_____|
#
# Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
# Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
Expand Down Expand Up @@ -340,6 +340,7 @@ sub logmsg {
$ENV{'CURL_ENTROPY'}="12345678";
$ENV{'CURL_FORCETIME'}=1; # for debug NTLM magic
$ENV{'HOME'}=$pwd;
$ENV{'COLUMNS'}=79; # screen width!

sub catch_zap {
my $signame = shift;
Expand Down

7 comments on commit 993dd56

@gvanem
Copy link
Contributor

@gvanem gvanem commented on 993dd56 Jan 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A set COLUMNS=120 and a:

curl -o NUL -# rtmp://cp67126.edgefcs.net/ondemand/mp4:mediapm/ovp/content/test/video/spacealonehd_sounas_640_300.mp4 

causes this Windows console wrap-error:

          -=O=-                                                                                                 #   # ##
          -=O=-                                                                                                    # ###
          -=O=-                                                                                                     ####
           -=O=-                                                                                                    # ##
            -=O=-                                                                                                #  # ##

With a set COLUMNS=119, all seems okay. And yes my console is 120 columns wide.
You tested this in bash? Which behaves differently when write a character at right edge.

@gvanem
Copy link
Contributor

@gvanem gvanem commented on 993dd56 Jan 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made a patch for Windows (doesn't rely on COLUMNS):

--- a/tool_cb_prg.c 2018-01-23 18:34:35
+++ b/tool_cb_prg.c 2018-01-23 20:33:11
@@ -202,6 +202,20 @@
     struct winsize ts;
     if(!ioctl(STDIN_FILENO, TIOCGWINSZ, &ts))
       cols = ts.ws_col;
+#elif defined(_WIN32)
+    {
+      HANDLE  stderr_hnd = GetStdHandle(STD_ERROR_HANDLE);
+      CONSOLE_SCREEN_BUFFER_INFO console_info;
+
+      if ((stderr_hnd != INVALID_HANDLE_VALUE) &&
+          GetConsoleScreenBufferInfo(stderr_hnd, &console_info)) {
+        /*
+         * Do not use +1 to get the true screen-width since writing a character at
+         * the right edge will cause a line wrap.
+         */
+         cols = (int) (console_info.srWindow.Right - console_info.srWindow.Left);
+       }
+    }
 #endif /* TIOCGSIZE */
     bar->width = cols;
   }

@bagder
Copy link
Member Author

@bagder bagder commented on 993dd56 Jan 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @gvanem! Pushed in commit a0b5e89

@gvanem
Copy link
Contributor

@gvanem gvanem commented on 993dd56 Jan 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good. I had a hard time finding an URL which total transfer size is unknown. Do you have an rtmp stream with truly infinite size? I used this Chinese one rtmp://ossrs.net/live/livestream, but that host is down.

@bagder
Copy link
Member Author

@bagder bagder commented on 993dd56 Jan 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I had that problem too. I couldn't find any good URL that reliably would send something decently large without a size. I ended up setting up a hacky local HTTP server that respond without size, and then I could use --limit-rate to make it go on for a longer time than it would otherwise.

@jay
Copy link
Member

@jay jay commented on 993dd56 Jan 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find any good URL that reliably would send something decently large without a size.

--ignore-content-length

curld -v -# -H "Connection: close" --ignore-content-length "http://httpbin.org/drip?numbytes=10485760&duration=5&delay=0&code=200" -o NUL

@bagder
Copy link
Member Author

@bagder bagder commented on 993dd56 Jan 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, that's just a lovely use of curl. You rock @jay!

Please sign in to comment.