]> code.delx.au - youtube-cgi/commitdiff
improved CLI status display
authorJames Bunton <jamesbunton@delx.net.au>
Mon, 3 Dec 2012 06:02:41 +0000 (17:02 +1100)
committerJames Bunton <jamesbunton@delx.net.au>
Mon, 3 Dec 2012 06:02:41 +0000 (17:02 +1100)
youtube.cgi

index 8b103bee7847de97bbaa5ae68350d6c98e358e76..f5845a4e9e95d424abc972895e94e11994f1bf86 100755 (executable)
@@ -238,27 +238,32 @@ def pp_size(size):
 
 def copy_with_progress(content_length, infile, outfile):
        def print_status():
 
 def copy_with_progress(content_length, infile, outfile):
        def print_status():
+               rate = 0
+               if now != last_ts:
+                       rate = last_bytes_read / (now - last_ts)
                sys.stdout.write("\33[2K\r")
                sys.stdout.write("%s / %s (%s/sec)" % (
                        pp_size(bytes_read),
                        pp_size(content_length),
                sys.stdout.write("\33[2K\r")
                sys.stdout.write("%s / %s (%s/sec)" % (
                        pp_size(bytes_read),
                        pp_size(content_length),
-                       pp_size(bytes_read / (now - start_ts)),
+                       pp_size(rate),
                ))
                sys.stdout.flush()
 
                ))
                sys.stdout.flush()
 
-       start_ts = time.time()
        last_ts = 0
        last_ts = 0
+       last_bytes_read = 0
        bytes_read = 0
        while True:
                now = time.time()
                if now - last_ts > 0.5:
        bytes_read = 0
        while True:
                now = time.time()
                if now - last_ts > 0.5:
-                       last_ts = now
                        print_status()
                        print_status()
+                       last_ts = now
+                       last_bytes_read = 0
 
                buf = infile.read(32768)
                if not buf:
                        break
                outfile.write(buf)
 
                buf = infile.read(32768)
                if not buf:
                        break
                outfile.write(buf)
+               last_bytes_read += len(buf)
                bytes_read += len(buf)
 
        # Newline at the end
                bytes_read += len(buf)
 
        # Newline at the end