From f31d564ff26532151b6e45f2be163ceea247a440 Mon Sep 17 00:00:00 2001 From: Alex Ball Date: Sun, 10 Oct 2021 16:42:07 +0100 Subject: [PATCH] click.confirm preserves prompt when readline is imported same fix as for click.prompt in 8.0.0 --- CHANGES.rst | 2 ++ src/click/termui.py | 6 ++++-- tests/test_utils.py | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) 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: " ")