55See the file 'LICENSE' for copying permission
66"""
77
8+ import time
9+
810from lib .core .common import getUnicode
911from lib .core .common import dataToStdout
1012from lib .core .data import conf
@@ -17,13 +19,12 @@ class ProgressBar(object):
1719
1820 def __init__ (self , minValue = 0 , maxValue = 10 , totalWidth = None ):
1921 self ._progBar = "[]"
20- self ._oldProgBar = ""
2122 self ._min = int (minValue )
2223 self ._max = int (maxValue )
2324 self ._span = max (self ._max - self ._min , 0.001 )
2425 self ._width = totalWidth if totalWidth else conf .progressWidth
2526 self ._amount = 0
26- self ._times = []
27+ self ._start = None
2728 self .update ()
2829
2930 def _convertSeconds (self , value ):
@@ -52,7 +53,7 @@ def update(self, newAmount=0):
5253 percentDone = min (100 , int (percentDone ))
5354
5455 # 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 ))
5657 numHashes = (percentDone / 100.0 ) * allFull
5758 numHashes = int (round (numHashes ))
5859
@@ -68,19 +69,18 @@ def update(self, newAmount=0):
6869 percentString = getUnicode (percentDone ) + "%"
6970 self ._progBar = "%s %s" % (percentString , self ._progBar )
7071
71- def progress (self , deltaTime , newAmount ):
72+ def progress (self , newAmount ):
7273 """
7374 This method saves item delta time and shows updated progress bar with calculated eta
7475 """
7576
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 ()
7779 eta = None
7880 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
8283
83- self ._times .append (deltaTime )
8484 self .update (newAmount )
8585 self .draw (eta )
8686
@@ -89,15 +89,13 @@ def draw(self, eta=None):
8989 This method draws the progress bar if it has changed
9090 """
9191
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 " )
10199
102100 def __str__ (self ):
103101 """
0 commit comments