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
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

View File

@ -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

View File

@ -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:

View File

@ -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()