From d17e02792b5a2150f7be0ace1effcd361b0b5fd1 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Fri, 12 Apr 2013 21:49:27 -0300 Subject: [PATCH] Fixed length progress bar. --- README.rst | 4 ++-- httpie/downloads.py | 29 ++++++++++++++++++++--------- httpie/utils.py | 2 +- tests/tests.py | 10 +++++++--- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/README.rst b/README.rst index 9ac5afc7..9a76d88d 100644 --- a/README.rst +++ b/README.rst @@ -846,8 +846,8 @@ is being saved to a file. Server: GitHub.com Vary: Accept-Encoding - Saving to "jkbr-httpie-0.4.1-20-g40bd8f6.tar.gz" - / 37.27% (184.00 kB) of 493.68 kB (181.69 kB/s) ETA 0:00:01 + Downloading 494.89 kB to "jkbr-httpie-0.4.1-33-gfc4f70a.tar.gz" + / 104.00 kB 21.01% 47.55 kB/s 0:00:08 ETA If not provided via ``--output, -o``, the output filename will be determined diff --git a/httpie/downloads.py b/httpie/downloads.py index 43a04921..c6770adb 100644 --- a/httpie/downloads.py +++ b/httpie/downloads.py @@ -10,7 +10,6 @@ import sys import mimetypes import threading from time import time -from datetime import timedelta from .output import RawStream from .models import HTTPResponse @@ -22,10 +21,14 @@ PARTIAL_CONTENT = 206 CLEAR_LINE = '\r\033[K' -PROGRESS = ('{percentage: 6.2f}% ({downloaded})' - ' of {total} ({speed}/s) ETA {eta}') -PROGRESS_NO_CONTENT_LENGTH = '{downloaded} ({speed}/s) ETA {eta}' -SUMMARY = 'Done. {downloaded} of {total} in {time:0.5f}s ({speed}/s)\n' +PROGRESS = ( + '{downloaded: >9}' + ' {percentage: 5.2f}%' + ' {speed: >10}/s' + ' {eta: >8} ETA' +) +PROGRESS_NO_CONTENT_LENGTH = '{downloaded: >10} {speed: >10}/s' +SUMMARY = 'Done. {downloaded} in {time:0.5f}s ({speed}/s)\n' SPINNER = '|/-\\' @@ -241,7 +244,11 @@ class Download(object): ) self._progress_reporter.output.write( - 'Saving to "%s"\n' % self._output_file.name) + 'Downloading %s to "%s"\n' % ( + self._progress.total_size_humanized, + self._output_file.name + ) + ) self._progress_reporter.report() return stream, self._output_file @@ -348,11 +355,14 @@ class ProgressReporter(object): # TODO: Use a longer interval for the speed/eta calculation? speed = ((downloaded - self._prev_bytes) / (now - self._prev_time)) - eta = int((self.progress.total_size - downloaded) / speed) - eta = str(timedelta(seconds=eta)) + s = int((self.progress.total_size - downloaded) / speed) except ZeroDivisionError: speed = 0 - eta = '?' + eta = '-:--:--' + else: + h, s = divmod(s, 60 * 60) + m, s = divmod(s, 60) + eta = '{}:{:0>2}:{:0>2}'.format(h, m, s) self._status_line = template.format( percentage=percentage, @@ -367,6 +377,7 @@ class ProgressReporter(object): self.output.write( CLEAR_LINE + + ' ' + SPINNER[self._spinner_pos] + ' ' + self._status_line diff --git a/httpie/utils.py b/httpie/utils.py index c6f9214b..811417a7 100644 --- a/httpie/utils.py +++ b/httpie/utils.py @@ -33,7 +33,7 @@ def humanize_bytes(n, precision=2): (1 << 30, 'GB'), (1 << 20, 'MB'), (1 << 10, 'kB'), - (1, 'bytes') + (1, 'B') ] if n == 1: diff --git a/tests/tests.py b/tests/tests.py index f076abbd..e8543471 100755 --- a/tests/tests.py +++ b/tests/tests.py @@ -31,6 +31,7 @@ import shutil try: from urllib.request import urlopen except ImportError: + # noinspection PyUnresolvedReferences from urllib2 import urlopen try: from unittest import skipIf, skip @@ -1433,9 +1434,7 @@ class SessionTest(BaseTestCase): self.assertDictEqual(r1.json, r3.json) -class DownloadsTest(BaseTestCase): - - # TODO: Actual download tests +class DownloadUtilsTest(BaseTestCase): def test_Content_Range_parsing(self): @@ -1517,5 +1516,10 @@ class DownloadsTest(BaseTestCase): ) +class DownloadTest(BaseTestCase): + # TODO: Download tests. + pass + + if __name__ == '__main__': unittest.main()