diff --git a/CHANGES.rst b/CHANGES.rst index c52c16007..e6e8e9bf3 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,6 +7,8 @@ Unreleased - Fix issue with ``Path(resolve_path=True)`` type creating invalid paths. :issue:`2088` +- Importing ``readline`` does not cause the ``confirm()`` prompt to + disappear when pressing backspace. :issue:`2092` Version 8.0.2 diff --git a/src/click/termui.py b/src/click/termui.py index a023f42bd..cf8d5f132 100644 --- a/src/click/termui.py +++ b/src/click/termui.py @@ -231,8 +231,10 @@ def confirm( try: # Write the prompt separately so that we get nice # coloring through colorama on Windows - echo(prompt, nl=False, err=err) - value = visible_prompt_func("").lower().strip() + echo(prompt.rstrip(" "), nl=False, err=err) + # Echo a space to stdout to work around an issue where + # readline causes backspace to clear the whole line. + value = visible_prompt_func(" ").lower().strip() except (KeyboardInterrupt, EOFError): raise Abort() from None if value in ("y", "yes"): diff --git a/tests/test_utils.py b/tests/test_utils.py index d21b246c2..271177d2d 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -275,8 +275,8 @@ def emulate_input(text): emulate_input("y\n") click.confirm("Prompt to stderr", err=True) out, err = capfd.readouterr() - assert out == "" - assert err == "Prompt to stderr [y/N]: " + assert out == " " + assert err == "Prompt to stderr [y/N]:" monkeypatch.setattr(click.termui, "isatty", lambda x: True) monkeypatch.setattr(click.termui, "getchar", lambda: " ")