diff --git a/halo/_utils.py b/halo/_utils.py index d78ef8a..2e7c5e4 100644 --- a/halo/_utils.py +++ b/halo/_utils.py @@ -4,12 +4,23 @@ import codecs import platform import six +import sys from colorama import init from shutil import get_terminal_size from termcolor import colored -init(autoreset=True) + +wrap_stream_for_win = None + +if sys.platform.startswith("win"): + try: + import colorama + except ImportError: + pass + else: + def wrap_stream_for_win(stream): # pylint: disable=function-redefined + return colorama.AnsiToWin32(stream, autoreset=True).stream def is_supported(): diff --git a/halo/halo.py b/halo/halo.py index e25b250..c8368e7 100644 --- a/halo/halo.py +++ b/halo/halo.py @@ -23,6 +23,7 @@ is_supported, is_text_type, encode_utf_8_text, + wrap_stream_for_win, ) @@ -87,6 +88,8 @@ def __init__( self._interval = ( int(interval) if int(interval) > 0 else self._spinner["interval"] ) + if wrap_stream_for_win: + stream = wrap_stream_for_win(stream) self._stream = stream self.placement = placement diff --git a/requirements.txt b/requirements.txt index b7f6879..2c7c323 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ log_symbols>=0.0.14 spinners>=0.0.24 termcolor>=1.1.0 -colorama>=0.3.9 +colorama>=0.3.9; sys_platform == "win32" six>=1.12.0 diff --git a/tests/test_halo.py b/tests/test_halo.py index f49504e..b83f5c4 100644 --- a/tests/test_halo.py +++ b/tests/test_halo.py @@ -95,6 +95,7 @@ def test_basic_spinner(self): self.assertEqual(output[1], '{} foo'.format(frames[1])) self.assertEqual(output[2], '{} foo'.format(frames[2])) + @unittest.skipIf(sys.platform.startswith("win"), "ANSI not supported on Windows") def test_text_spinner_color(self): """Test basic spinner with available colors color (both spinner and text) """ @@ -590,6 +591,7 @@ def test_animation_setter(self): spinner.animation = "marquee" self.assertEqual("marquee", spinner.animation) + @unittest.skipIf(sys.platform.startswith("win"), "ANSI not supported on Windows") def test_spinner_color(self): """Test ANSI escape characters are present """