Strip ANSI from `confirm()` and `prompt()` (#3653)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents 6ec99f8, 8008ab9 · 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
Provenance
- Commit
fe3ad76- Tree
6d49eb2- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
6ec99f8- Parent 2
8008ab9
3 files changed+68 added−0 removed
modified:src/click/termui.py+14 added
9 unmodified lines10 from contextlib import AbstractContextManager11 from contextlib import redirect_stdout12 from gettext import gettext as _13 14+from . import _compat15 from ._compat import isatty16 from ._compat import strip_ansi17 from .exceptions import Abort18 from .exceptions import UsageError64 unmodified lines83 84 def _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.87+88+ 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.stdout95+96+ # 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)100+101 if err:102 with redirect_stdout(sys.stderr):103 return func(text)104 return func(text)844 unmodified linesmodified:tests/test_termui.py+50 added
1020 unmodified lines1021 if prompt == "Confirm Password":1022 assert "Confirm Password: " in result.output1023 1024 1025+@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+)1058+def 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+ """1066+1067+ @click.command()1068+ def cli():1069+ call()1070+1071+ result = runner.invoke(cli, input=user_input, color=color)1072+ assert result.output == expect1073+1074+1075 def 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 linesmodified:CHANGES.md+4 added
4 unmodified lines5 - 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`13 - Fix test failures when using pytest >= 9.1. {pr}`3656`14 15 ## Version 8.4.216 1562 unmodified lines