diff --git a/progressbar/bar.py b/progressbar/bar.py index 01620f9..4cfc835 100644 --- a/progressbar/bar.py +++ b/progressbar/bar.py @@ -256,10 +256,8 @@ def _determine_enable_colors( else: enable_colors = progressbar.env.ColorSupport.NONE break - else: # pragma: no cover - # This scenario should never occur because `is_ansi_terminal` - # should always be `True` or `False` - raise ValueError('Unable to determine color support') + else: + enable_colors = False elif enable_colors is True: enable_colors = progressbar.env.ColorSupport.XTERM_256 diff --git a/progressbar/terminal/base.py b/progressbar/terminal/base.py index b8d2a97..85971c5 100644 --- a/progressbar/terminal/base.py +++ b/progressbar/terminal/base.py @@ -199,7 +199,24 @@ class WindowsColors(enum.Enum): @staticmethod def from_rgb(rgb: types.Tuple[int, int, int]): - """Find the closest ConsoleColor to the given RGB color.""" + ''' + Find the closest WindowsColors to the given RGB color. + + >>> WindowsColors.from_rgb((0, 0, 0)) + + + >>> WindowsColors.from_rgb((255, 255, 255)) + + + >>> WindowsColors.from_rgb((0, 255, 0)) + + + >>> WindowsColors.from_rgb((45, 45, 45)) + + + >>> WindowsColors.from_rgb((128, 0, 128)) + + ''' def color_distance(rgb1, rgb2): return sum((c1 - c2) ** 2 for c1, c2 in zip(rgb1, rgb2)) @@ -211,6 +228,13 @@ def color_distance(rgb1, rgb2): class WindowsColor: + ''' + Windows compatible color class for when ANSI is not supported. + Currently a no-op because it is not possible to buffer these colors. + + >>> WindowsColor(WindowsColors.RED)('test') + 'test' + ''' __slots__ = 'color', def __init__(self, color: Color): @@ -543,9 +567,6 @@ class DummyColor: def __call__(self, text): return text - def __getattr__(self, item): - return self - def __repr__(self): return 'DummyColor()' diff --git a/pyproject.toml b/pyproject.toml index 04e8fcd..a6d553d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -181,6 +181,7 @@ exclude_lines = [ 'if __name__ == .__main__.:', 'if types.TYPE_CHECKING:', '@typing.overload', + 'if os.name == .nt.:', ] [tool.pyright] diff --git a/tests/test_color.py b/tests/test_color.py index bf76eec..feb962e 100644 --- a/tests/test_color.py +++ b/tests/test_color.py @@ -175,6 +175,7 @@ def test_colors(): assert rgb.hex assert rgb.to_ansi_16 is not None assert rgb.to_ansi_256 is not None + assert rgb.to_windows is not None assert color.underline assert color.fg assert color.bg @@ -304,6 +305,22 @@ def test_apply_colors(text, fg, bg, fg_none, bg_none, percentage, expected, ) +def test_windows_colors(monkeypatch): + monkeypatch.setattr(env, 'COLOR_SUPPORT', env.ColorSupport.WINDOWS) + assert ( + apply_colors( + 'test', + fg=colors.red, + bg=colors.red, + fg_none=colors.red, + bg_none=colors.red, + percentage=1, + ) + == 'test' + ) + colors.red.underline('test') + + def test_ansi_color(monkeypatch): color = progressbar.terminal.Color( colors.red.rgb, diff --git a/tests/test_utils.py b/tests/test_utils.py index c9d9531..2f03062 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -68,7 +68,7 @@ def test_is_ansi_terminal(monkeypatch): monkeypatch.delenv('PROGRESSBAR_IS_TERMINAL', raising=False) monkeypatch.delenv('JPY_PARENT_PID', raising=False) - assert progressbar.env.is_ansi_terminal(fd) is False + assert not progressbar.env.is_ansi_terminal(fd) assert progressbar.env.is_ansi_terminal(fd, True) is True assert progressbar.env.is_ansi_terminal(fd, False) is False @@ -77,16 +77,16 @@ def test_is_ansi_terminal(monkeypatch): monkeypatch.delenv('JPY_PARENT_PID') # Sanity check - assert progressbar.env.is_ansi_terminal(fd) is False + assert not progressbar.env.is_ansi_terminal(fd) monkeypatch.setenv('PROGRESSBAR_IS_TERMINAL', 'true') - assert progressbar.env.is_ansi_terminal(fd) is False + assert not progressbar.env.is_ansi_terminal(fd) monkeypatch.setenv('PROGRESSBAR_IS_TERMINAL', 'false') - assert progressbar.env.is_ansi_terminal(fd) is False + assert not progressbar.env.is_ansi_terminal(fd) monkeypatch.delenv('PROGRESSBAR_IS_TERMINAL') # Sanity check - assert progressbar.env.is_ansi_terminal(fd) is False + assert not progressbar.env.is_ansi_terminal(fd) # Fake TTY mode for environment testing fd.isatty = lambda: True @@ -103,12 +103,9 @@ def test_is_ansi_terminal(monkeypatch): monkeypatch.setenv('ANSICON', 'true') assert progressbar.env.is_ansi_terminal(fd) is True monkeypatch.delenv('ANSICON') - assert progressbar.env.is_ansi_terminal(fd) is False + assert not progressbar.env.is_ansi_terminal(fd) def raise_error(): raise RuntimeError('test') fd.isatty = raise_error - if os.name == 'nt': - assert progressbar.env.is_ansi_terminal(fd) is None - else: - assert progressbar.env.is_ansi_terminal(fd) is False + assert not progressbar.env.is_ansi_terminal(fd)