5
5
See the file 'LICENSE' for copying permission
6
6
"""
7
7
8
+ import time
9
+
8
10
from lib .core .common import getUnicode
9
11
from lib .core .common import dataToStdout
10
12
from lib .core .data import conf
@@ -17,13 +19,12 @@ class ProgressBar(object):
17
19
18
20
def __init__ (self , minValue = 0 , maxValue = 10 , totalWidth = None ):
19
21
self ._progBar = "[]"
20
- self ._oldProgBar = ""
21
22
self ._min = int (minValue )
22
23
self ._max = int (maxValue )
23
24
self ._span = max (self ._max - self ._min , 0.001 )
24
25
self ._width = totalWidth if totalWidth else conf .progressWidth
25
26
self ._amount = 0
26
- self ._times = []
27
+ self ._start = None
27
28
self .update ()
28
29
29
30
def _convertSeconds (self , value ):
@@ -52,7 +53,7 @@ def update(self, newAmount=0):
52
53
percentDone = min (100 , int (percentDone ))
53
54
54
55
# Figure out how many hash bars the percentage should be
55
- allFull = self ._width - len ("100%% [] %s/%s ETA 00:00" % (self ._max , self ._max ))
56
+ allFull = self ._width - len ("100%% [] %s/%s ( ETA 00:00) " % (self ._max , self ._max ))
56
57
numHashes = (percentDone / 100.0 ) * allFull
57
58
numHashes = int (round (numHashes ))
58
59
@@ -68,19 +69,18 @@ def update(self, newAmount=0):
68
69
percentString = getUnicode (percentDone ) + "%"
69
70
self ._progBar = "%s %s" % (percentString , self ._progBar )
70
71
71
- def progress (self , deltaTime , newAmount ):
72
+ def progress (self , newAmount ):
72
73
"""
73
74
This method saves item delta time and shows updated progress bar with calculated eta
74
75
"""
75
76
76
- if len (self ._times ) <= ((self ._max * 3 ) / 100 ) or newAmount > self ._max :
77
+ if self ._start is None or newAmount > self ._max :
78
+ self ._start = time .time ()
77
79
eta = None
78
80
else :
79
- midTime = sum (self ._times ) / len (self ._times )
80
- midTimeWithLatest = (midTime + deltaTime ) / 2
81
- eta = midTimeWithLatest * (self ._max - newAmount )
81
+ delta = time .time () - self ._start
82
+ eta = (self ._max - self ._min ) * (1.0 * delta / newAmount ) - delta
82
83
83
- self ._times .append (deltaTime )
84
84
self .update (newAmount )
85
85
self .draw (eta )
86
86
@@ -89,15 +89,13 @@ def draw(self, eta=None):
89
89
This method draws the progress bar if it has changed
90
90
"""
91
91
92
- if self ._progBar != self ._oldProgBar :
93
- self ._oldProgBar = self ._progBar
94
- dataToStdout ("\r %s %d/%d%s" % (self ._progBar , self ._amount , self ._max , (" ETA %s" % self ._convertSeconds (int (eta ))) if eta is not None else "" ))
95
- if self ._amount >= self ._max :
96
- if not conf .liveTest :
97
- dataToStdout ("\r %s\r " % (" " * self ._width ))
98
- kb .prependFlag = False
99
- else :
100
- dataToStdout ("\n " )
92
+ dataToStdout ("\r %s %d/%d%s" % (self ._progBar , self ._amount , self ._max , (" (ETA %s)" % (self ._convertSeconds (int (eta )) if eta is not None else "??:??" ))))
93
+ if self ._amount >= self ._max :
94
+ if not conf .liveTest :
95
+ dataToStdout ("\r %s\r " % (" " * self ._width ))
96
+ kb .prependFlag = False
97
+ else :
98
+ dataToStdout ("\n " )
101
99
102
100
def __str__ (self ):
103
101
"""
0 commit comments