From 598b34ced2d93e3910ab0ca427b46437e5471b00 Mon Sep 17 00:00:00 2001 From: Walter Johnson Date: Wed, 9 Sep 2020 08:57:29 -0400 Subject: [PATCH 1/2] Remove colorama init, Only use colorama on windows --- halo/_utils.py | 14 ++++++++++++-- halo/halo.py | 3 +++ requirements.txt | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/halo/_utils.py b/halo/_utils.py index 999a95b..2f5b169 100644 --- a/halo/_utils.py +++ b/halo/_utils.py @@ -4,15 +4,25 @@ import codecs import platform import six +import sys try: from shutil import get_terminal_size except ImportError: from backports.shutil_get_terminal_size import get_terminal_size -from colorama import init 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 9e10b66..f57ff59 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 f314c34..0cc3671 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,5 +2,5 @@ backports.shutil_get_terminal_size>=1.0.0;python_version < '3.3' 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 From 3b50f1516fe0dc67be689b781dd604efc0a58d08 Mon Sep 17 00:00:00 2001 From: Walter Johnson Date: Wed, 9 Sep 2020 10:04:14 -0400 Subject: [PATCH 2/2] Disable color tests on windows --- tests/test_halo.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_halo.py b/tests/test_halo.py index 29918aa..6f204b2 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.assertEquals("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 """