77class 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,35 @@ def __enter__(self):
3637
3738 def __exit__ (self , exc_type , exc_val , exc_tb ):
3839 self .stop ()
40+ if self .beep :
41+ sys .stdout .write ('\7 ' )
42+ sys .stdout .flush ()
3943 return False
4044
4145
42- def spinner (force = False ):
46+ def spinner (beep = False , force = False ):
4347 """This function creates a context manager that is used to display a
4448 spinner on stdout as long as the context has not exited.
4549
4650 The spinner is created only if stdout is not redirected, or if the spinner
4751 is forced using the `force` parameter.
4852
49- Parameters:
50-
51- force (bool): Force creation of spinner even when stdout is redirected.
53+ Parameters
54+ ----------
55+ beep : bool
56+ Beep when spinner finishes.
57+ force : bool
58+ Force creation of spinner even when stdout is redirected.
5259
53- Example usage::
60+ Example
61+ -------
5462
5563 with spinner():
5664 do_something()
5765 do_something_else()
5866
5967 """
60- return Spinner (force )
68+ return Spinner (beep , force )
6169
6270
6371from ._version import get_versions
0 commit comments