diff --git a/click_spinner/__init__.py b/click_spinner/__init__.py index 9e1beca..c64a6ee 100644 --- a/click_spinner/__init__.py +++ b/click_spinner/__init__.py @@ -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() @@ -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