Skip to content

Commit 090b349

Browse files
author
Yoav Ram
authored
beep when done; style changes
1 parent fccafa3 commit 090b349

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

click_spinner/__init__.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
class Spinner(object):
88
spinner_cycle = itertools.cycle(['-', '/', '|', '\\'])
99

10-
def __init__(self, force=False):
11-
self._force = force
10+
def __init__(self, beep=False, force=False):
11+
self.beep = beep
12+
self.force = force
1213
self.stop_running = None
1314
self.spin_thread = None
1415

1516
def start(self):
16-
if sys.stdout.isatty() or self._force:
17+
if sys.stdout.isatty() or self.force:
1718
self.stop_running = threading.Event()
1819
self.spin_thread = threading.Thread(target=self.init_spin)
1920
self.spin_thread.start()
@@ -36,28 +37,34 @@ def __enter__(self):
3637

3738
def __exit__(self, exc_type, exc_val, exc_tb):
3839
self.stop()
40+
if self.beep:
41+
print("\7", end='', flush=True)
3942
return False
4043

4144

42-
def spinner(force=False):
45+
def spinner(beep=False, force=False):
4346
"""This function creates a context manager that is used to display a
4447
spinner on stdout as long as the context has not exited.
4548
4649
The spinner is created only if stdout is not redirected, or if the spinner
4750
is forced using the `force` parameter.
4851
49-
Parameters:
52+
Parameters
53+
----------
54+
beep : bool
55+
Beep when spinner finishes.
56+
force : bool
57+
Force creation of spinner even when stdout is redirected.
5058
51-
force (bool): Force creation of spinner even when stdout is redirected.
52-
53-
Example usage::
59+
Example
60+
-------
5461
5562
with spinner():
5663
do_something()
5764
do_something_else()
5865
5966
"""
60-
return Spinner(force)
67+
return Spinner(beep, force)
6168

6269

6370
from ._version import get_versions

0 commit comments

Comments
 (0)