Skip to content

Commit

Permalink
Merge pull request #14 from click-contrib/beep
Browse files Browse the repository at this point in the history
Beep when done + style changes
  • Loading branch information
Yoav Ram authored Dec 15, 2016
2 parents fccafa3 + bbce12d commit df4442d
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions click_spinner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
class Spinner(object):
spinner_cycle = itertools.cycle(['-', '/', '|', '\\'])

def __init__(self, force=False):
self._force = force
def __init__(self, beep=False, force=False):
self.beep = beep
self.force = force
self.stop_running = None
self.spin_thread = None

def start(self):
if sys.stdout.isatty() or self._force:
if sys.stdout.isatty() or self.force:
self.stop_running = threading.Event()
self.spin_thread = threading.Thread(target=self.init_spin)
self.spin_thread.start()
Expand All @@ -36,28 +37,35 @@ def __enter__(self):

def __exit__(self, exc_type, exc_val, exc_tb):
self.stop()
if self.beep:
sys.stdout.write('\7')
sys.stdout.flush()
return False


def spinner(force=False):
def spinner(beep=False, force=False):
"""This function creates a context manager that is used to display a
spinner on stdout as long as the context has not exited.
The spinner is created only if stdout is not redirected, or if the spinner
is forced using the `force` parameter.
Parameters:
force (bool): Force creation of spinner even when stdout is redirected.
Parameters
----------
beep : bool
Beep when spinner finishes.
force : bool
Force creation of spinner even when stdout is redirected.
Example usage::
Example
-------
with spinner():
do_something()
do_something_else()
"""
return Spinner(force)
return Spinner(beep, force)


from ._version import get_versions
Expand Down

0 comments on commit df4442d

Please sign in to comment.