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
Provenance
- Commit
7454647- Tree
aa67dda- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
bc36cd1
10 files changed+25 added−94 removed
modified:docs/quickstart.md+2 added-3 removed
22 unmodified lines232425262728272829303147 unmodified lines79808182848384858687108 unmodified linesExpand all22 unmodified lines- [validation](https://github.com/pallets/click/tree/main/examples/validation) : A simple example of an application that performs custom validation of parameters in different ways.- [naval](https://github.com/pallets/click/tree/main/examples/naval) : Port of the [docopt](http://docopt.org/) naval example.- [colors](https://github.com/pallets/click/tree/main/examples/colors) : A simple example that colorizes text. Uses colorama on Windows.- [colors](https://github.com/pallets/click/tree/main/examples/colors) : A simple example that colorizes text.- [aliases](https://github.com/pallets/click/tree/main/examples/aliases) : An advanced example that implements {ref}`aliases`.- [imagepipe](https://github.com/pallets/click/tree/main/examples/imagepipe) : A complex example that implements some {ref}`command-pipelines` . It chains together image processing instructions. Requires pillow.47 unmodified linesWhat this means is that the {func}`echo` function applies some error correction in case the terminal is misconfiguredinstead of dying with a {exc}`UnicodeError`.
The echo function also supports color and other styles in output. It will automatically remove styles if the outputstream is a file. On Windows, colorama is automatically installed and used. See {ref}`ansi-colors`.stream is a file. See {ref}`ansi-colors`.
If you don't need this, you can also use the `print()` construct / function.
## Nesting Commands108 unmodified linesExpand allmodified:docs/utils.md+8 added-3 removed
50 unmodified lines51 52 ```{versionadded} 2.053 ```54 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.56 57 Primarily this means that:58 59 - 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.62 63-On Windows, Click uses colorama without calling `colorama.init()`. You can still call that in your code, but it's not64-required for Click.63+:::{admonition} Older Windows Support64+:class: note65 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, install68+[colorama](https://pypi.org/project/colorama/) and call `colorama.init()`.69+:::70+71 For styling a string, the {func}`style` function can be used:72 73 ```python74 import click336 unmodified linesmodified:examples/colors/README-2 removed
1 unmodified line2 3 colors is a simple example that shows how you can4 colorize text.5 6- Uses colorama on Windows.7-6 Usage:7 8 $ pip install --editable .9 $ colorsmodified:src/click/_compat.py+1 added-41 removed
11 unmodified lines12 13 CYGWIN = sys.platform.startswith("cygwin")14 WIN = sys.platform.startswith("win")15 MAC = sys.platform == "darwin"16-auto_wrap_for_ansi: t.Callable[[t.TextIO], t.TextIO] | None = None16 _ansi_re = re.compile(r"\033\[[;?0-9]*[a-zA-Z]")17 18 19 def _make_text_stream(489 unmodified lines509 return not isatty(stream) and not _is_jupyter_kernel_output(stream)510 return not color511 512 514-# 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 Linux514 if sys.platform.startswith("win") and WIN:515 from ._winconsole import _get_windows_console_stream516 517 def _get_argv_encoding() -> str:518 import locale519 520 return locale.getpreferredencoding()521 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 a529- stream with colorama.530- """531- try:532- cached = _ansi_stream_wrappers.get(stream)533- except Exception:534- cached = None535-536- if cached is not None:537- return cached538-539- import colorama540-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.write545-546- def _safe_write(s: str) -> int:547- try:548- return _write(s)549- except BaseException:550- ansi_wrapper.reset_all()551- raise552-553- rv.write = _safe_write # type: ignore[method-assign]554-555- try:556- _ansi_stream_wrappers[stream] = rv557- except Exception:558- pass559-560- return rv561-522 else:523 524 def _get_argv_encoding() -> str:525 return getattr(sys.stdin, "encoding", None) or sys.getfilesystemencoding()62 unmodified linesmodified:src/click/termui.py-11 removed
11 unmodified lines12 from gettext import gettext as _13 14 from ._compat import isatty15 from ._compat import strip_ansi16-from ._compat import WIN16 from .exceptions import Abort17 from .exceptions import UsageError18 from .globals import resolve_color_default19 from .types import Choice62 unmodified lines82 83 def _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.87-88- 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 linesmodified:src/click/utils.py+5 added-11 removed
11 unmodified lines12 13 from ._compat import _default_text_stderr14 from ._compat import _default_text_stdout15 from ._compat import _find_binary_writer16-from ._compat import auto_wrap_for_ansi16 from ._compat import binary_streams17 from ._compat import open_stream18 from ._compat import should_strip_ansi19 from ._compat import strip_ansi240 unmodified lines260 :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.263 264+ .. versionchanged:: 8.5265+ Colorama is no longer used for color on Windows.266+267 .. 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 lines320 return321 322 # 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)326 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: ignore330- elif not color:331- out = strip_ansi(out)332-327 file.write(out) # type: ignore328 file.flush()329 330 305 unmodified linesmodified:tests/test_utils.py+6 added-10 removed
470 unmodified lines471 click.echo(styled_text, color=True)472 out, err = capfd.readouterr()473 assert out == f"{styled_text}\n"474 475- isatty = True475 click.echo(styled_text)476 out, err = capfd.readouterr()477 assert out == f"{styled_text}\n"478 479 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"483 488- 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"487 488 489 def test_prompt_cast_default(capfd, monkeypatch):490 monkeypatch.setattr(sys, "stdin", StringIO("\n"))298 unmodified linesmodified:CHANGES.rst+3 added
3 unmodified lines4 -------------5 6 Unreleased7 8+- 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`10 11+12 Version 8.4.213 -------------14 15 Unreleased1479 unmodified linesmodified:pyproject.toml-9 removed
12 unmodified lines13 "Programming Language :: Python",14 "Typing :: Typed",15 ]16 requires-python = ">=3.10"17-dependencies = [18- "colorama; platform_system == 'Windows'",19-]17 18 [project.urls]19 Donate = "https://palletsprojects.com/donate"20 Documentation = "https://click.palletsprojects.com/"89 unmodified lines110 show_error_codes = true111 pretty = true112 strict = true113 117-[[tool.mypy.overrides]]118-module = [119- "colorama.*",120-]121-ignore_missing_imports = true122-114 [tool.pyright]115 pythonVersion = "3.10"116 include = ["src", "tests/typing"]117 typeCheckingMode = "basic"115 unmodified linesmodified:uv.lock-4 removed
173 unmodified lines174 [[package]]175 name = "click"176 version = "8.5.0.dev0"177 source = { editable = "." }178-dependencies = [179- { name = "colorama", marker = "sys_platform == 'win32'" },180-]178 179 [package.dev-dependencies]180 dev = [181 { name = "ruff" },35 unmodified lines217 { name = "pytest" },218 ]219 220 [package.metadata]224-requires-dist = [{ name = "colorama", marker = "sys_platform == 'win32'" }]221 222 [package.metadata.requires-dev]223 dev = [224 { name = "ruff" },1437 unmodified lines