Skip to content
Myr

Strip ANSI from `confirm()` and `prompt()` when the stream don't support them

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

Closes #3572 Refs #2969

authored by Kevin Deldycke · committed by Kevin Deldycke · parent 240603f · Browse files · 3 files+68 added−0 removed
This commit

State

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

Diff stats

3 files+68 added−0 removed

  • tests:+50 added−0 removed
  • other:+18 added−0 removed

3 files+68 added−0 removed

modified:src/click/termui.py+14 added
9 unmodified lines
10from contextlib import AbstractContextManager11from contextlib import redirect_stdout12from gettext import gettext as _1314from . import _compat15from ._compat import isatty16from ._compat import strip_ansi17from .exceptions import Abort18from .exceptions import UsageError
64 unmodified lines
8384def _readline_prompt(func: t.Callable[[str], str], text: str, err: bool) -> str:85 """Call a prompt function, passing the full prompt on non-Windows so86 readline can handle line editing and cursor positioning correctly.8788 The prompt is handed to *func* (such as :func:`input`) rather than89 written through :func:`echo`, so it has to strip ANSI color and style90 codes itself when the destination stream does not support them. Without91 this the prompt would keep codes that :func:`echo` removes from the92 rest of the output.93 """94 stream = sys.stderr if err else sys.stdout9596 # Look up ``should_strip_ansi`` on the module so that ``CliRunner``,97 # which patches it there during test isolation, is honored.98 if _compat.should_strip_ansi(stream, resolve_color_default()):99 text = strip_ansi(text)100101 if err:102 with redirect_stdout(sys.stderr):103 return func(text)104 return func(text)
844 unmodified lines
modified:tests/test_termui.py+50 added
1020 unmodified lines
1021 if prompt == "Confirm Password":1022 assert "Confirm Password: " in result.output102310241025@pytest.mark.parametrize(1026 ("call", "user_input", "color", "expect"),1027 [1028 pytest.param(1029 lambda: click.confirm(click.style("Hello World!", fg="green")),1030 "y",1031 False,1032 "Hello World! [y/N]: y\n",1033 id="confirm-no-color",1034 ),1035 pytest.param(1036 lambda: click.confirm(click.style("Hello World!", fg="green")),1037 "y",1038 True,1039 "\x1b[32mHello World!\x1b[0m [y/N]: y\n",1040 id="confirm-color",1041 ),1042 pytest.param(1043 lambda: click.prompt(click.style("Name", fg="green")),1044 "Bob",1045 False,1046 "Name: Bob\n",1047 id="prompt-no-color",1048 ),1049 pytest.param(1050 lambda: click.prompt(click.style("Name", fg="green")),1051 "Bob",1052 True,1053 "\x1b[32mName\x1b[0m: Bob\n",1054 id="prompt-color",1055 ),1056 ],1057)1058def test_prompt_and_confirm_ansi_respects_color(1059 runner, call, user_input, color, expect1060):1061 """``confirm`` and ``prompt`` strip ANSI color and style codes from the1062 prompt when color is disabled and keep them when it is enabled, like1063 ``echo``. The prompt is written by ``input`` rather than ``echo``, so it1064 used to keep the codes regardless (issue 3572).1065 """10661067 @click.command()1068 def cli():1069 call()10701071 result = runner.invoke(cli, input=user_input, color=color)1072 assert result.output == expect107310741075def test_false_show_default_cause_no_default_display_in_prompt(runner):1076 @click.command()1077 @click.option("--arg1", show_default=False, prompt=True, default="my-default-value")1078 def cmd(arg1):
534 unmodified lines
modified:CHANGES.md+4 added
4 unmodified lines
5- Supported versions of Windows enable ANSI terminal styles by default.6 Colorama is no longer a dependency and is not used. {issue}`2986` {pr}`3505`7- {class}`Argument` accepts a `help` parameter, and help output includes8 a `Positional arguments` section when argument help is available. {issue}`2983` {pr}`3473`9- `confirm()` and `prompt()` strip ANSI color and style codes from the10 prompt when the output stream does not support them, matching `echo()`.11 This stripping was lost in `8.4.0` when {pr}`2969` began writing the12 prompt with `input()` directly. {issue}`3572` {pr}`3653`1314## Version 8.4.21516Released 2026-06-24
1561 unmodified lines