From 090b3491a61e4076c9ac0b115247d6896c1f00a6 Mon Sep 17 00:00:00 2001 From: Yoav Ram Date: Thu, 15 Dec 2016 10:57:57 +0200 Subject: [PATCH 1/2] beep when done; style changes --- click_spinner/__init__.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/click_spinner/__init__.py b/click_spinner/__init__.py index 9e1beca..38d918b 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,34 @@ def __enter__(self): def __exit__(self, exc_type, exc_val, exc_tb): self.stop() + if self.beep: + print("\7", end='', flush=True) 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: + Parameters + ---------- + beep : bool + Beep when spinner finishes. + force : bool + Force creation of spinner even when stdout is redirected. - 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 From bbce12db75bb413bbab639a3038604320dcd3e6a Mon Sep 17 00:00:00 2001 From: Yoav Ram Date: Thu, 15 Dec 2016 11:02:55 +0200 Subject: [PATCH 2/2] use sys.stdout instead of print to support Py2.7 --- click_spinner/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/click_spinner/__init__.py b/click_spinner/__init__.py index 38d918b..c64a6ee 100644 --- a/click_spinner/__init__.py +++ b/click_spinner/__init__.py @@ -38,7 +38,8 @@ def __enter__(self): def __exit__(self, exc_type, exc_val, exc_tb): self.stop() if self.beep: - print("\7", end='', flush=True) + sys.stdout.write('\7') + sys.stdout.flush() return False