diff --git a/halo/halo.py b/halo/halo.py index 9f9bc8b..4bd3fe1 100644 --- a/halo/halo.py +++ b/halo/halo.py @@ -5,6 +5,7 @@ from __future__ import unicode_literals, absolute_import, print_function import sys +import signal import threading import time import functools @@ -64,6 +65,13 @@ def __init__(self, text='', color='cyan', spinner=None, animation=None, interval self._spinner_id = None self._enabled = enabled # Need to check for stream + def handle_keyboard_interrupt(signal, frame): + """Handle KeyboardInterrupt without try-except statement""" + self.fail(self.text) + raise KeyboardInterrupt + + signal.signal(signal.SIGINT, handle_keyboard_interrupt) + def __enter__(self): """Starts the spinner on a separate thread. For use in context managers. Returns diff --git a/tests/test_halo.py b/tests/test_halo.py index b508524..6a9ea65 100644 --- a/tests/test_halo.py +++ b/tests/test_halo.py @@ -5,6 +5,7 @@ import unittest import time import sys +import signal import io import logging import os