Skip to content

Commit

Permalink
Support for spinners with an embedded timer :-)
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Nov 16, 2014
1 parent a6a8010 commit a0581ba
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions humanfriendly.py
Expand Up @@ -5,7 +5,7 @@
# URL: https://humanfriendly.readthedocs.org

# Semi-standard module versioning.
__version__ = '1.12'
__version__ = '1.13'

# Standard library modules.
import math
Expand Down Expand Up @@ -454,7 +454,7 @@ class Spinner(object):
| Downloading: 1.00% # travels up to 100%...
"""

def __init__(self, label=None, total=0, stream=sys.stderr, interactive=None):
def __init__(self, label=None, total=0, stream=sys.stderr, interactive=None, timer=None):
"""
Initialize a spinner.
Expand All @@ -465,6 +465,9 @@ def __init__(self, label=None, total=0, stream=sys.stderr, interactive=None):
:param interactive: If this is ``False`` then the spinner doesn't write
to the output stream at all. It defaults to the
return value of ``stream.isatty()``.
:param timer: A :py:class:`Timer` object (optional). If this is given
the spinner will show the elapsed time according to the
timer.
"""
self.label = label
self.total = total
Expand All @@ -478,6 +481,7 @@ def __init__(self, label=None, total=0, stream=sys.stderr, interactive=None):
except Exception:
interactive = False
self.interactive = interactive
self.timer = timer

def step(self, progress=0, label=None):
"""
Expand All @@ -497,7 +501,9 @@ def step(self, progress=0, label=None):
raise Exception("No label set for spinner!")
elif self.total and progress:
label = "%s: %.2f%%" % (label, progress/(self.total/100.0))
self.stream.write("\r %s %s " % (state, label))
elif self.timer and self.timer.elapsed_time > 2:
label = "%s (%s)" % (label, self.timer.rounded)
self.stream.write("\r %s %s .. " % (state, label))
self.counter += 1

def clear(self):
Expand Down

0 comments on commit a0581ba

Please sign in to comment.