diff --git a/README.rst b/README.rst index cce3023..402579a 100644 --- a/README.rst +++ b/README.rst @@ -12,7 +12,7 @@ Easy progress reporting for Python Bars ---- -There are 6 progress bars to choose from: +There are 7 progress bars to choose from: - ``Bar`` - ``ChargingBar`` @@ -20,6 +20,7 @@ There are 6 progress bars to choose from: - ``FillingCirclesBar`` - ``IncrementalBar`` - ``ShadyBar`` +- ``AdaptiveBar`` To use them, just call ``next`` to advance and ``finish`` to finish: diff --git a/progress/bar.py b/progress/bar.py index 26008e9..12d60bb 100644 --- a/progress/bar.py +++ b/progress/bar.py @@ -15,7 +15,7 @@ # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. from __future__ import unicode_literals -from . import Progress +from . import Progress, Infinite from .helpers import WritelnMixin @@ -82,3 +82,41 @@ def update(self): class ShadyBar(IncrementalBar): phases = (' ', '░', '▒', '▓', '█') + + +class AdaptiveBar(WritelnMixin, Infinite): + phases = (' ', '▏', '▎', '▍', '▌', '▋', '▊', '▉', '█') + suffix = '%(index)d%%' + _count = 0 + width = 50 + bar_prefix = ' |' + bar_suffix = '| ' + empty_fill = ' ' + + def update(self): + import math + self.percent = float(math.pow(self._count, 2)/(math.pow(self._count, 2)+1000)) + self.index = int(self.percent * 100) + self._count += 1 + nphases = len(self.phases) + filled_len = self.width * self.percent + nfull = int(filled_len) # Number of full chars + phase = int((filled_len - nfull) * nphases) # Phase of last char + nempty = self.width - nfull # Number of empty chars + + message = self.message % self + bar = self.phases[-1] * nfull + current = self.phases[phase] if phase > 0 else '' + empty = self.empty_fill * max(0, nempty - len(current)) + suffix = self.suffix % self + line = ''.join([message, self.bar_prefix, bar, current, empty, + self.bar_suffix, suffix]) + self.writeln(line) + + def finish(self): + self.index = 100 + message = self.message % self + suffix = self.suffix % self + bar = bar = self.phases[-1] * self.width + line = ''.join([message, self.bar_prefix, bar, self.bar_suffix, suffix]) + self.writeln(line) diff --git a/test_progress.py b/test_progress.py index ae7bb19..e46e76e 100755 --- a/test_progress.py +++ b/test_progress.py @@ -5,7 +5,7 @@ import random import time -from progress.bar import (Bar, ChargingBar, FillingSquaresBar, +from progress.bar import (Bar, ChargingBar, FillingSquaresBar, AdaptiveBar, FillingCirclesBar, IncrementalBar, ShadyBar) from progress.spinner import Spinner, PieSpinner, MoonSpinner, LineSpinner from progress.counter import Counter, Countdown, Stack, Pie @@ -16,7 +16,6 @@ def sleep(): t += t * random.uniform(-0.1, 0.1) # Add some variance time.sleep(t) - for bar_cls in (Bar, ChargingBar, FillingSquaresBar, FillingCirclesBar): suffix = '%(index)d/%(max)d [%(elapsed)d / %(eta)d]' bar = bar_cls(bar_cls.__name__, suffix=suffix) @@ -29,6 +28,13 @@ def sleep(): for i in bar.iter(range(200)): sleep() +for bar_cls in [AdaptiveBar]: + suffix = '%(index)d%% [%(elapsed_td)s]' + bar = bar_cls(bar_cls.__name__, suffix=suffix) + for i in bar.iter(range(200)): + sleep() + print() + for spin in (Spinner, PieSpinner, MoonSpinner, LineSpinner): for i in spin(spin.__name__ + ' ').iter(range(100)): sleep()