Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ Easy progress reporting for Python
Bars
----

There are 6 progress bars to choose from:
There are 7 progress bars to choose from:

- ``Bar``
- ``ChargingBar``
- ``FillingSquaresBar``
- ``FillingCirclesBar``
- ``IncrementalBar``
- ``ShadyBar``
- ``AdaptiveBar``

To use them, just call ``next`` to advance and ``finish`` to finish:

Expand Down
40 changes: 39 additions & 1 deletion progress/bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
10 changes: 8 additions & 2 deletions test_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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()
Expand Down