Skip to content
Myr

Fix colored output on Windows (#2607)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Krisztián Löki · committed by GitHub · parent 53f14d3 · Browse files · 3 files+14 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Andreas Backx <andreas@backx.org>

Diff stats

3 files+14 added−8 removed

  • tests:+11 added−7 removed
  • other:+3 added−1 removed

3 files+14 added−8 removed

modified:src/click/utils.py+1 added-1 removed
309 unmodified lines
310 if should_strip_ansi(file, color):311 out = strip_ansi(out)312 elif WIN:313 if auto_wrap_for_ansi is not None:314 file = auto_wrap_for_ansi(file) # type: ignore314 file = auto_wrap_for_ansi(file, color) # type: ignore315 elif not color:316 out = strip_ansi(out)317318 file.write(out) # type: ignore
306 unmodified lines
modified:tests/test_utils.py+11 added-7 removed
34 unmodified lines
35 assert outstreams[0].getvalue() == b"\x1b[31mx\x1b[39m"363738def test_echo_custom_file():39 import io4041 f = io.StringIO()39 f = StringIO()40 click.echo("hello", file=f)41 assert f.getvalue() == "hello\n"4243
162 unmodified lines
206 out, err = capfd.readouterr()207 assert out == expected_output208209212@pytest.mark.skipif(WIN, reason="Test does not make sense on Windows.")210def test_echo_color_flag(monkeypatch, capfd):211 isatty = True212 monkeypatch.setattr(click._compat, "isatty", lambda x: isatty)213
14 unmodified lines
228 out, err = capfd.readouterr()229 assert out == f"{styled_text}\n"230231 isatty = False235 click.echo(styled_text)236 out, err = capfd.readouterr()237 assert out == f"{text}\n"232 # Faking isatty() is not enough on Windows;233 # the implementation caches the colorama wrapped stream234 # so we have to use a new stream for each test235 stream = StringIO()236 click.echo(styled_text, file=stream)237 assert stream.getvalue() == f"{text}\n"238239 stream = StringIO()240 click.echo(styled_text, file=stream, color=True)241 assert stream.getvalue() == f"{styled_text}\n"242243244def test_prompt_cast_default(capfd, monkeypatch):245 monkeypatch.setattr(sys, "stdin", StringIO("\n"))246 value = click.prompt("value", default="100", type=int)247 capfd.readouterr()
264 unmodified lines
modified:CHANGES.rst+2 added
12 unmodified lines
13- The test runner handles stripping color consistently on Windows.14 :issue:`2705`15- Show correct value for flag default when using ``default_map``.16 :issue:`2632`17- Fix ``click.echo(color=...)`` passing ``color`` to coloroma so it can be18 forced on Windows. :issue:`2606`.192021Version 8.1.722-------------
1102 unmodified lines