Skip to content

Commit

Permalink
Merge pull request #1836 from MLH-Fellowship/fix-prompt
Browse files Browse the repository at this point in the history
Fixed click.prompt not working with readline module
  • Loading branch information
davidism authored Apr 6, 2021
2 parents b625b9f + 4aff026 commit 68b3d4f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/click/termui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
8 changes: 4 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 68b3d4f

Please sign in to comment.