Skip to content

Commit fd9d206

Browse files
committed
remove colorama
1 parent bc36cd1 commit fd9d206

10 files changed

Lines changed: 26 additions & 95 deletions

File tree

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ Version 8.5.0
55

66
Unreleased
77

8+
- Supported versions of Windows enable ANSI terminal styles by default.
9+
Colorama is no longer a dependency and is not used. :issue:`2986`
10+
811

912
Version 8.4.2
1013
-------------

docs/quickstart.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ Some standalone examples of Click applications are packaged with Click. They are
2424
performs custom validation of parameters in different ways.
2525
- [naval](https://github.com/pallets/click/tree/main/examples/naval) : Port of the [docopt](http://docopt.org/) naval
2626
example.
27-
- [colors](https://github.com/pallets/click/tree/main/examples/colors) : A simple example that colorizes text. Uses
28-
colorama on Windows.
27+
- [colors](https://github.com/pallets/click/tree/main/examples/colors) : A simple example that colorizes text.
2928
- [aliases](https://github.com/pallets/click/tree/main/examples/aliases) : An advanced example that implements
3029
{ref}`aliases`.
3130
- [imagepipe](https://github.com/pallets/click/tree/main/examples/imagepipe) : A complex example that implements some
@@ -81,7 +80,7 @@ What this means is that the {func}`echo` function applies some error correction
8180
instead of dying with a {exc}`UnicodeError`.
8281

8382
The echo function also supports color and other styles in output. It will automatically remove styles if the output
84-
stream is a file. On Windows, colorama is automatically installed and used. See {ref}`ansi-colors`.
83+
stream is a file. See {ref}`ansi-colors`.
8584

8685
If you don't need this, you can also use the `print()` construct / function.
8786

docs/utils.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,21 @@ click.echo('Hello World!', err=True)
5252
```{versionadded} 2.0
5353
```
5454

55-
The {func}`echo` function supports ANSI colors and styles. On Windows this uses [colorama](https://pypi.org/project/colorama/).
55+
The {func}`echo` function supports ANSI colors and styles.
5656

5757
Primarily this means that:
5858

5959
- Click's {func}`echo` function will automatically strip ANSI color codes if the stream is not connected to a terminal.
6060
- the {func}`echo` function will transparently connect to the terminal on Windows and translate ANSI codes to terminal
6161
API calls. This means that colors will work on Windows the same way they do on other operating systems.
6262

63-
On Windows, Click uses colorama without calling `colorama.init()`. You can still call that in your code, but it's not
64-
required for Click.
63+
:::{admonition} Older Windows Support
64+
:class: note
65+
66+
Recent Windows 11 supports ANSI styling by default, in both Terminal and cmd.exe.
67+
If you need to support color output on older versions of Windows, install
68+
[colorama](https://pypi.org/project/colorama/) and call `colorama.init()`.
69+
:::
6570

6671
For styling a string, the {func}`style` function can be used:
6772

examples/colors/README

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ $ colors_
33
colors is a simple example that shows how you can
44
colorize text.
55

6-
Uses colorama on Windows.
7-
86
Usage:
97

108
$ pip install --editable .

pyproject.toml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ classifiers = [
1414
"Typing :: Typed",
1515
]
1616
requires-python = ">=3.10"
17-
dependencies = [
18-
"colorama; platform_system == 'Windows'",
19-
]
2017

2118
[project.urls]
2219
Donate = "https://palletsprojects.com/donate"
@@ -114,12 +111,6 @@ show_error_codes = true
114111
pretty = true
115112
strict = true
116113

117-
[[tool.mypy.overrides]]
118-
module = [
119-
"colorama.*",
120-
]
121-
ignore_missing_imports = true
122-
123114
[tool.pyright]
124115
pythonVersion = "3.10"
125116
include = ["src", "tests/typing"]

src/click/_compat.py

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
CYGWIN = sys.platform.startswith("cygwin")
1414
WIN = sys.platform.startswith("win")
1515
MAC = sys.platform == "darwin"
16-
auto_wrap_for_ansi: t.Callable[[t.TextIO], t.TextIO] | None = None
1716
_ansi_re = re.compile(r"\033\[[;?0-9]*[a-zA-Z]")
1817

1918

@@ -511,9 +510,7 @@ def should_strip_ansi(
511510
return not color
512511

513512

514-
# On Windows, wrap the output streams with colorama to support ANSI
515-
# color codes.
516-
# NOTE: double check is needed so mypy does not analyze this on Linux
513+
# double check is needed so mypy does not analyze this on Linux
517514
if sys.platform.startswith("win") and WIN:
518515
from ._winconsole import _get_windows_console_stream
519516

@@ -522,43 +519,6 @@ def _get_argv_encoding() -> str:
522519

523520
return locale.getpreferredencoding()
524521

525-
_ansi_stream_wrappers: cabc.MutableMapping[t.TextIO, t.TextIO] = WeakKeyDictionary()
526-
527-
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 a
529-
stream with colorama.
530-
"""
531-
try:
532-
cached = _ansi_stream_wrappers.get(stream)
533-
except Exception:
534-
cached = None
535-
536-
if cached is not None:
537-
return cached
538-
539-
import colorama
540-
541-
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.write
545-
546-
def _safe_write(s: str) -> int:
547-
try:
548-
return _write(s)
549-
except BaseException:
550-
ansi_wrapper.reset_all()
551-
raise
552-
553-
rv.write = _safe_write # type: ignore[method-assign]
554-
555-
try:
556-
_ansi_stream_wrappers[stream] = rv
557-
except Exception:
558-
pass
559-
560-
return rv
561-
562522
else:
563523

564524
def _get_argv_encoding() -> str:

src/click/termui.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
from ._compat import isatty
1515
from ._compat import strip_ansi
16-
from ._compat import WIN
1716
from .exceptions import Abort
1817
from .exceptions import UsageError
1918
from .globals import resolve_color_default
@@ -84,17 +83,7 @@ def hidden_prompt_func(prompt: str) -> str:
8483
def _readline_prompt(func: t.Callable[[str], str], text: str, err: bool) -> str:
8584
"""Call a prompt function, passing the full prompt on non-Windows so
8685
readline can handle line editing and cursor positioning correctly.
87-
88-
On Windows the prompt is written separately via :func:`echo` for
89-
colorama support, with only the last character passed to *func*.
9086
"""
91-
if WIN:
92-
# Write the prompt separately so that we get nice coloring
93-
# through colorama on Windows.
94-
echo(text[:-1], nl=False, err=err)
95-
# Echo the last character to stdout to work around an issue
96-
# where readline causes backspace to clear the whole line.
97-
return func(text[-1:])
9887
if err:
9988
with redirect_stdout(sys.stderr):
10089
return func(text)

src/click/utils.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from ._compat import _default_text_stderr
1414
from ._compat import _default_text_stdout
1515
from ._compat import _find_binary_writer
16-
from ._compat import auto_wrap_for_ansi
1716
from ._compat import binary_streams
1817
from ._compat import open_stream
1918
from ._compat import should_strip_ansi
@@ -262,6 +261,9 @@ def echo(
262261
default Click will remove color if the output does not look like
263262
an interactive terminal.
264263
264+
.. versionchanged:: 8.5
265+
Colorama is no longer used for color on Windows.
266+
265267
.. versionchanged:: 6.0
266268
Support Unicode output on the Windows console. Click does not
267269
modify ``sys.stdout``, so ``sys.stdout.write()`` and ``print()``
@@ -319,16 +321,8 @@ def echo(
319321

320322
# ANSI style code support. For no message or bytes, nothing happens.
321323
# When outputting to a file instead of a terminal, strip codes.
322-
else:
323-
color = resolve_color_default(color)
324-
325-
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: ignore
330-
elif not color:
331-
out = strip_ansi(out)
324+
elif should_strip_ansi(file, resolve_color_default(color)):
325+
out = strip_ansi(out)
332326

333327
file.write(out) # type: ignore
334328
file.flush()

tests/test_utils.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -472,22 +472,18 @@ def test_echo_color_flag(monkeypatch, capfd):
472472
out, err = capfd.readouterr()
473473
assert out == f"{styled_text}\n"
474474

475-
isatty = True
476475
click.echo(styled_text)
477476
out, err = capfd.readouterr()
478477
assert out == f"{styled_text}\n"
479478

480479
isatty = False
481-
# Faking isatty() is not enough on Windows;
482-
# the implementation caches the colorama wrapped stream
483-
# so we have to use a new stream for each test
484-
stream = StringIO()
485-
click.echo(styled_text, file=stream)
486-
assert stream.getvalue() == f"{text}\n"
487-
488-
stream = StringIO()
489-
click.echo(styled_text, file=stream, color=True)
490-
assert stream.getvalue() == f"{styled_text}\n"
480+
click.echo(styled_text)
481+
out, err = capfd.readouterr()
482+
assert out == f"{text}\n"
483+
484+
click.echo(styled_text, color=True)
485+
out, err = capfd.readouterr()
486+
assert out == f"{styled_text}\n"
491487

492488

493489
def test_prompt_cast_default(capfd, monkeypatch):

uv.lock

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)