From 4aff02676c0d9628e095731c124b647ed256ee5a Mon Sep 17 00:00:00 2001 From: Saif807380 Date: Tue, 6 Apr 2021 20:51:19 +0530 Subject: [PATCH] fix backspace in prompt with readline enabled --- CHANGES.rst | 2 ++ src/click/termui.py | 6 ++++-- tests/test_utils.py | 8 ++++---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 24eabda85..c6aa8100c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -201,6 +201,8 @@ Unreleased applications to translate Click's built-in strings. :issue:`303` - Writing invalid characters to ``stderr`` when using the test runner does not raise a ``UnicodeEncodeError``. :issue:`848` +- Fix an issue where ``readline`` would clear the entire ``prompt()`` + line instead of only the input when pressing backspace. :issue:`665` Version 7.1.2 diff --git a/src/click/termui.py b/src/click/termui.py index 3a7e0850e..9850cf57a 100644 --- a/src/click/termui.py +++ b/src/click/termui.py @@ -127,8 +127,10 @@ def prompt_func(text): try: # Write the prompt separately so that we get nice # coloring through colorama on Windows - echo(text, nl=False, err=err) - return f("") + echo(text.rstrip(" "), nl=False, err=err) + # Echo a space to stdout to work around an issue where + # readline causes backspace to clear the whole line. + return f(" ") except (KeyboardInterrupt, EOFError): # getpass doesn't print a newline if the user aborts input with ^C. # Allegedly this behavior is inherited from getpass(3). diff --git a/tests/test_utils.py b/tests/test_utils.py index 3150b09c8..b3cfe7027 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -154,10 +154,10 @@ def f(_): try: click.prompt("Password", hide_input=True) except click.Abort: - click.echo("Screw you.") + click.echo("interrupted") out, err = capsys.readouterr() - assert out == "Password: \nScrew you.\n" + assert out == "Password:\ninterrupted\n" def _test_gen_func(): @@ -255,8 +255,8 @@ def emulate_input(text): emulate_input("asdlkj\n") click.prompt("Prompt to stderr", err=True) out, err = capfd.readouterr() - assert out == "" - assert err == "Prompt to stderr: " + assert out == " " + assert err == "Prompt to stderr:" emulate_input("y\n") click.confirm("Prompt to stdin")