Skip to content
Myr

remove colorama

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

authored by David Lord · committed by David Lord · parent bc36cd1 · Browse files · 10 files+25 added−94 removed
This commit

State

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

Diff stats

10 files+25 added−94 removed

  • tests:+6 added−10 removed
  • other:+19 added−84 removed

10 files+25 added−94 removed

modified:docs/quickstart.md+2 added-3 removed
modified:docs/utils.md+8 added-3 removed
50 unmodified lines
5152```{versionadded} 2.053```5455The {func}`echo` function supports ANSI colors and styles. On Windows this uses [colorama](https://pypi.org/project/colorama/).55The {func}`echo` function supports ANSI colors and styles.5657Primarily this means that:5859- Click's {func}`echo` function will automatically strip ANSI color codes if the stream is not connected to a terminal.60- the {func}`echo` function will transparently connect to the terminal on Windows and translate ANSI codes to terminal61 API calls. This means that colors will work on Windows the same way they do on other operating systems.6263On Windows, Click uses colorama without calling `colorama.init()`. You can still call that in your code, but it's not64required for Click.63:::{admonition} Older Windows Support64:class: note6566Recent Windows 11 supports ANSI styling by default, in both Terminal and cmd.exe.67If you need to support color output on older versions of Windows, install68[colorama](https://pypi.org/project/colorama/) and call `colorama.init()`.69:::7071For styling a string, the {func}`style` function can be used:7273```python74import click
336 unmodified lines
modified:examples/colors/README-2 removed
1 unmodified line
23 colors is a simple example that shows how you can4 colorize text.56 Uses colorama on Windows.76Usage:78 $ pip install --editable .9 $ colors
modified:src/click/_compat.py+1 added-41 removed
11 unmodified lines
1213CYGWIN = sys.platform.startswith("cygwin")14WIN = sys.platform.startswith("win")15MAC = sys.platform == "darwin"16auto_wrap_for_ansi: t.Callable[[t.TextIO], t.TextIO] | None = None16_ansi_re = re.compile(r"\033\[[;?0-9]*[a-zA-Z]")171819def _make_text_stream(
489 unmodified lines
509 return not isatty(stream) and not _is_jupyter_kernel_output(stream)510 return not color511512514# On Windows, wrap the output streams with colorama to support ANSI515# color codes.516# NOTE: double check is needed so mypy does not analyze this on Linux513# double check is needed so mypy does not analyze this on Linux514if sys.platform.startswith("win") and WIN:515 from ._winconsole import _get_windows_console_stream516517 def _get_argv_encoding() -> str:518 import locale519520 return locale.getpreferredencoding()521525 _ansi_stream_wrappers: cabc.MutableMapping[t.TextIO, t.TextIO] = WeakKeyDictionary()526527 def auto_wrap_for_ansi(stream: t.TextIO, color: bool | None = None) -> t.TextIO:528 """Support ANSI color and style codes on Windows by wrapping a529 stream with colorama.530 """531 try:532 cached = _ansi_stream_wrappers.get(stream)533 except Exception:534 cached = None535536 if cached is not None:537 return cached538539 import colorama540541 strip = should_strip_ansi(stream, color)542 ansi_wrapper = colorama.AnsiToWin32(stream, strip=strip)543 rv = t.cast(t.TextIO, ansi_wrapper.stream)544 _write = rv.write545546 def _safe_write(s: str) -> int:547 try:548 return _write(s)549 except BaseException:550 ansi_wrapper.reset_all()551 raise552553 rv.write = _safe_write # type: ignore[method-assign]554555 try:556 _ansi_stream_wrappers[stream] = rv557 except Exception:558 pass559560 return rv561522else:523524 def _get_argv_encoding() -> str:525 return getattr(sys.stdin, "encoding", None) or sys.getfilesystemencoding()
62 unmodified lines
modified:src/click/termui.py-11 removed
11 unmodified lines
12from gettext import gettext as _1314from ._compat import isatty15from ._compat import strip_ansi16from ._compat import WIN16from .exceptions import Abort17from .exceptions import UsageError18from .globals import resolve_color_default19from .types import Choice
62 unmodified lines
8283def _readline_prompt(func: t.Callable[[str], str], text: str, err: bool) -> str:84 """Call a prompt function, passing the full prompt on non-Windows so85 readline can handle line editing and cursor positioning correctly.8788 On Windows the prompt is written separately via :func:`echo` for89 colorama support, with only the last character passed to *func*.86 """91 if WIN:92 # Write the prompt separately so that we get nice coloring93 # through colorama on Windows.94 echo(text[:-1], nl=False, err=err)95 # Echo the last character to stdout to work around an issue96 # where readline causes backspace to clear the whole line.97 return func(text[-1:])87 if err:88 with redirect_stdout(sys.stderr):89 return func(text)90 return func(text)
840 unmodified lines
modified:src/click/utils.py+5 added-11 removed
11 unmodified lines
1213from ._compat import _default_text_stderr14from ._compat import _default_text_stdout15from ._compat import _find_binary_writer16from ._compat import auto_wrap_for_ansi16from ._compat import binary_streams17from ._compat import open_stream18from ._compat import should_strip_ansi19from ._compat import strip_ansi
240 unmodified lines
260 :param color: Force showing or hiding colors and other styles. By261 default Click will remove color if the output does not look like262 an interactive terminal.263264 .. versionchanged:: 8.5265 Colorama is no longer used for color on Windows.266267 .. versionchanged:: 6.0268 Support Unicode output on the Windows console. Click does not269 modify ``sys.stdout``, so ``sys.stdout.write()`` and ``print()``270 will still not support Unicode.
49 unmodified lines
320 return321322 # ANSI style code support. For no message or bytes, nothing happens.323 # When outputting to a file instead of a terminal, strip codes.322 else:323 color = resolve_color_default(color)324 elif should_strip_ansi(file, resolve_color_default(color)):325 out = strip_ansi(out)326325 if should_strip_ansi(file, color):326 out = strip_ansi(out)327 elif WIN:328 if auto_wrap_for_ansi is not None:329 file = auto_wrap_for_ansi(file, color) # type: ignore330 elif not color:331 out = strip_ansi(out)332327 file.write(out) # type: ignore328 file.flush()329330
305 unmodified lines
modified:tests/test_utils.py+6 added-10 removed
470 unmodified lines
471 click.echo(styled_text, color=True)472 out, err = capfd.readouterr()473 assert out == f"{styled_text}\n"474475 isatty = True475 click.echo(styled_text)476 out, err = capfd.readouterr()477 assert out == f"{styled_text}\n"478479 isatty = False481 # Faking isatty() is not enough on Windows;482 # the implementation caches the colorama wrapped stream483 # so we have to use a new stream for each test484 stream = StringIO()485 click.echo(styled_text, file=stream)486 assert stream.getvalue() == f"{text}\n"480 click.echo(styled_text)481 out, err = capfd.readouterr()482 assert out == f"{text}\n"483488 stream = StringIO()489 click.echo(styled_text, file=stream, color=True)490 assert stream.getvalue() == f"{styled_text}\n"484 click.echo(styled_text, color=True)485 out, err = capfd.readouterr()486 assert out == f"{styled_text}\n"487488489def test_prompt_cast_default(capfd, monkeypatch):490 monkeypatch.setattr(sys, "stdin", StringIO("\n"))
298 unmodified lines
modified:CHANGES.rst+3 added
3 unmodified lines
4-------------56Unreleased78- Supported versions of Windows enable ANSI terminal styles by default.9 Colorama is no longer a dependency and is not used. :issue:`2986` :pr:`3505`101112Version 8.4.213-------------1415Unreleased
1479 unmodified lines
modified:pyproject.toml-9 removed
12 unmodified lines
13 "Programming Language :: Python",14 "Typing :: Typed",15]16requires-python = ">=3.10"17dependencies = [18 "colorama; platform_system == 'Windows'",19]1718[project.urls]19Donate = "https://palletsprojects.com/donate"20Documentation = "https://click.palletsprojects.com/"
89 unmodified lines
110show_error_codes = true111pretty = true112strict = true113117[[tool.mypy.overrides]]118module = [119 "colorama.*",120]121ignore_missing_imports = true122114[tool.pyright]115pythonVersion = "3.10"116include = ["src", "tests/typing"]117typeCheckingMode = "basic"
115 unmodified lines
modified:uv.lock-4 removed
173 unmodified lines
174[[package]]175name = "click"176version = "8.5.0.dev0"177source = { editable = "." }178dependencies = [179 { name = "colorama", marker = "sys_platform == 'win32'" },180]178179[package.dev-dependencies]180dev = [181 { name = "ruff" },
35 unmodified lines
217 { name = "pytest" },218]219220[package.metadata]224requires-dist = [{ name = "colorama", marker = "sys_platform == 'win32'" }]221222[package.metadata.requires-dev]223dev = [224 { name = "ruff" },
1437 unmodified lines