Hello, 

i am often sitting behind a very slow internet link and thought, it
might be useful to show the current bandwidth while downloading
something with ftp(1).

This looks like: 

$ ftp http://url
 60% |*************         |   105 MB  0116 KB/s   11:22 ETA

Is this of interest for others?  Any comments on that? 

Christian


diff --git util.c util.c
index 1c82f3fb478..a260db74375 100644
--- util.c
+++ util.c
@@ -748,6 +748,17 @@ static struct timespec start;
 
 char *action;
 
+static int
+find_prefix_index(off_t* sz, off_t maxidx) {
+       int j = 0;
+
+       while (*sz  >= 100000 && j < maxidx - 1) {
+               j++;
+               *sz >>= 10;
+       }
+       return j;
+}
+
 void
 progressmeter(int flag, const char *filename)
 {
@@ -761,9 +772,9 @@ progressmeter(int flag, const char *filename)
        static off_t lastsize;
        static char *title = NULL;
        struct timespec now, td, wait;
-       off_t cursize, abbrevsize;
+       off_t cursize, abbrevsize, abbrevsize_bw = 0;
        double elapsed;
-       int ratio, barlength, i, remaining, overhead = 30;
+       int ratio, barlength, i, j, remaining, overhead = 40;
        char buf[512];
 
        if (flag == -1) {
@@ -841,12 +852,9 @@ progressmeter(int flag, const char *filename)
                    barlength - i, "");
        }
 
-       i = 0;
        abbrevsize = cursize;
-       while (abbrevsize >= 100000 && i < sizeof(prefixes)-1) {
-               i++;
-               abbrevsize >>= 10;
-       }
+       i = find_prefix_index(&abbrevsize, sizeof(prefixes));
+
        snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
            " %5lld %c%c ", (long long)abbrevsize, prefixes[i],
            prefixes[i] == ' ' ? ' ' : 'B');
@@ -878,7 +886,7 @@ progressmeter(int flag, const char *filename)
                    "%02d:%02d    ", i / 60, i % 60);
        } else if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
-                   "   --:-- ETA");
+                   " 0000  B/s   --:-- ETA");
        } else if (wait.tv_sec >= STALLTIME) {
                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
                    " - stalled -");
@@ -886,6 +894,13 @@ progressmeter(int flag, const char *filename)
                remaining = (int)((filesize - restart_point) /
                                  (bytes / elapsed) - elapsed);
                i = remaining / 3600;
+
+               if (elapsed)
+                       abbrevsize_bw = cursize / elapsed;
+               j = find_prefix_index(&abbrevsize_bw, sizeof(prefixes));
+
+               snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+                           " %04lld %cB/s", abbrevsize_bw, prefixes[j]);
                if (i)
                        snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
                            "%2d:", i);

Reply via email to