Fixed length progress bar.

This commit is contained in:
Jakub Roztocil 2013-04-12 21:49:27 -03:00
parent fc4f70a900
commit d17e02792b
4 changed files with 30 additions and 15 deletions

View File

@ -846,8 +846,8 @@ is being saved to a file.
Server: GitHub.com Server: GitHub.com
Vary: Accept-Encoding Vary: Accept-Encoding
Saving to "jkbr-httpie-0.4.1-20-g40bd8f6.tar.gz" Downloading 494.89 kB to "jkbr-httpie-0.4.1-33-gfc4f70a.tar.gz"
/ 37.27% (184.00 kB) of 493.68 kB (181.69 kB/s) ETA 0:00:01 / 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 If not provided via ``--output, -o``, the output filename will be determined

View File

@ -10,7 +10,6 @@ import sys
import mimetypes import mimetypes
import threading import threading
from time import time from time import time
from datetime import timedelta
from .output import RawStream from .output import RawStream
from .models import HTTPResponse from .models import HTTPResponse
@ -22,10 +21,14 @@ PARTIAL_CONTENT = 206
CLEAR_LINE = '\r\033[K' CLEAR_LINE = '\r\033[K'
PROGRESS = ('{percentage: 6.2f}% ({downloaded})' PROGRESS = (
' of {total} ({speed}/s) ETA {eta}') '{downloaded: >9}'
PROGRESS_NO_CONTENT_LENGTH = '{downloaded} ({speed}/s) ETA {eta}' ' {percentage: 5.2f}%'
SUMMARY = 'Done. {downloaded} of {total} in {time:0.5f}s ({speed}/s)\n' ' {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 = '|/-\\' SPINNER = '|/-\\'
@ -241,7 +244,11 @@ class Download(object):
) )
self._progress_reporter.output.write( 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() self._progress_reporter.report()
return stream, self._output_file return stream, self._output_file
@ -348,11 +355,14 @@ class ProgressReporter(object):
# TODO: Use a longer interval for the speed/eta calculation? # TODO: Use a longer interval for the speed/eta calculation?
speed = ((downloaded - self._prev_bytes) speed = ((downloaded - self._prev_bytes)
/ (now - self._prev_time)) / (now - self._prev_time))
eta = int((self.progress.total_size - downloaded) / speed) s = int((self.progress.total_size - downloaded) / speed)
eta = str(timedelta(seconds=eta))
except ZeroDivisionError: except ZeroDivisionError:
speed = 0 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( self._status_line = template.format(
percentage=percentage, percentage=percentage,
@ -367,6 +377,7 @@ class ProgressReporter(object):
self.output.write( self.output.write(
CLEAR_LINE CLEAR_LINE
+ ' '
+ SPINNER[self._spinner_pos] + SPINNER[self._spinner_pos]
+ ' ' + ' '
+ self._status_line + self._status_line

View File

@ -33,7 +33,7 @@ def humanize_bytes(n, precision=2):
(1 << 30, 'GB'), (1 << 30, 'GB'),
(1 << 20, 'MB'), (1 << 20, 'MB'),
(1 << 10, 'kB'), (1 << 10, 'kB'),
(1, 'bytes') (1, 'B')
] ]
if n == 1: if n == 1:

View File

@ -31,6 +31,7 @@ import shutil
try: try:
from urllib.request import urlopen from urllib.request import urlopen
except ImportError: except ImportError:
# noinspection PyUnresolvedReferences
from urllib2 import urlopen from urllib2 import urlopen
try: try:
from unittest import skipIf, skip from unittest import skipIf, skip
@ -1433,9 +1434,7 @@ class SessionTest(BaseTestCase):
self.assertDictEqual(r1.json, r3.json) self.assertDictEqual(r1.json, r3.json)
class DownloadsTest(BaseTestCase): class DownloadUtilsTest(BaseTestCase):
# TODO: Actual download tests
def test_Content_Range_parsing(self): def test_Content_Range_parsing(self):
@ -1517,5 +1516,10 @@ class DownloadsTest(BaseTestCase):
) )
class DownloadTest(BaseTestCase):
# TODO: Download tests.
pass
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()