7
7
class Spinner (object ):
8
8
spinner_cycle = itertools .cycle (['-' , '/' , '|' , '\\ ' ])
9
9
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
12
13
self .stop_running = None
13
14
self .spin_thread = None
14
15
15
16
def start (self ):
16
- if sys .stdout .isatty () or self ._force :
17
+ if sys .stdout .isatty () or self .force :
17
18
self .stop_running = threading .Event ()
18
19
self .spin_thread = threading .Thread (target = self .init_spin )
19
20
self .spin_thread .start ()
@@ -36,28 +37,34 @@ def __enter__(self):
36
37
37
38
def __exit__ (self , exc_type , exc_val , exc_tb ):
38
39
self .stop ()
40
+ if self .beep :
41
+ print ("\7 " , end = '' , flush = True )
39
42
return False
40
43
41
44
42
- def spinner (force = False ):
45
+ def spinner (beep = False , force = False ):
43
46
"""This function creates a context manager that is used to display a
44
47
spinner on stdout as long as the context has not exited.
45
48
46
49
The spinner is created only if stdout is not redirected, or if the spinner
47
50
is forced using the `force` parameter.
48
51
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.
50
58
51
- force (bool): Force creation of spinner even when stdout is redirected.
52
-
53
- Example usage::
59
+ Example
60
+ -------
54
61
55
62
with spinner():
56
63
do_something()
57
64
do_something_else()
58
65
59
66
"""
60
- return Spinner (force )
67
+ return Spinner (beep , force )
61
68
62
69
63
70
from ._version import get_versions
0 commit comments