Skip to content

Commit 8533c96

Browse files
authored
Fix rendering when prompt_suffix is empty (#3021)
2 parents 5f86603 + 27de74a commit 8533c96

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Unreleased
1111
the ``Context.invoke()`` method. :issue:`3066` :issue:`3065` :pr:`3068`
1212
- Fix conversion of ``Sentinel.UNSET`` happening too early, which caused incorrect
1313
behavior for multiple parameters using the same name. :issue:`3071` :pr:`3079`
14+
- Fix rendering when ``prompt`` and ``confirm`` parameter ``prompt_suffix`` is
15+
empty. :issue:`3019` :pr:`3021`
1416

1517
Version 8.3.0
1618
--------------

src/click/termui.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ def prompt(
119119
show_choices is true and text is "Group by" then the
120120
prompt will be "Group by (day, week): ".
121121
122+
.. versionchanged:: 8.3.1
123+
A space is no longer appended to the prompt.
124+
122125
.. versionadded:: 8.0
123126
``confirmation_prompt`` can be a custom string.
124127
@@ -138,10 +141,10 @@ def prompt_func(text: str) -> str:
138141
try:
139142
# Write the prompt separately so that we get nice
140143
# coloring through colorama on Windows
141-
echo(text.rstrip(" "), nl=False, err=err)
142-
# Echo a space to stdout to work around an issue where
144+
echo(text[:-1], nl=False, err=err)
145+
# Echo the last character to stdout to work around an issue where
143146
# readline causes backspace to clear the whole line.
144-
return f(" ")
147+
return f(text[-1:])
145148
except (KeyboardInterrupt, EOFError):
146149
# getpass doesn't print a newline if the user aborts input with ^C.
147150
# Allegedly this behavior is inherited from getpass(3).
@@ -214,6 +217,9 @@ def confirm(
214217
:param err: if set to true the file defaults to ``stderr`` instead of
215218
``stdout``, the same as with echo.
216219
220+
.. versionchanged:: 8.3.1
221+
A space is no longer appended to the prompt.
222+
217223
.. versionchanged:: 8.0
218224
Repeat until input is given if ``default`` is ``None``.
219225
@@ -231,10 +237,10 @@ def confirm(
231237
try:
232238
# Write the prompt separately so that we get nice
233239
# coloring through colorama on Windows
234-
echo(prompt.rstrip(" "), nl=False, err=err)
235-
# Echo a space to stdout to work around an issue where
240+
echo(prompt[:-1], nl=False, err=err)
241+
# Echo the last character to stdout to work around an issue where
236242
# readline causes backspace to clear the whole line.
237-
value = visible_prompt_func(" ").lower().strip()
243+
value = visible_prompt_func(prompt[-1:]).lower().strip()
238244
except (KeyboardInterrupt, EOFError):
239245
raise Abort() from None
240246
if value in ("y", "yes"):

tests/test_utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,24 +481,48 @@ def emulate_input(text):
481481
assert out == "Prompt to stdin: "
482482
assert err == ""
483483

484+
emulate_input("asdlkj\n")
485+
click.prompt("Prompt to stdin with no suffix", prompt_suffix="")
486+
out, err = capfd.readouterr()
487+
assert out == "Prompt to stdin with no suffix"
488+
assert err == ""
489+
484490
emulate_input("asdlkj\n")
485491
click.prompt("Prompt to stderr", err=True)
486492
out, err = capfd.readouterr()
487493
assert out == " "
488494
assert err == "Prompt to stderr:"
489495

496+
emulate_input("asdlkj\n")
497+
click.prompt("Prompt to stderr with no suffix", prompt_suffix="", err=True)
498+
out, err = capfd.readouterr()
499+
assert out == "x"
500+
assert err == "Prompt to stderr with no suffi"
501+
490502
emulate_input("y\n")
491503
click.confirm("Prompt to stdin")
492504
out, err = capfd.readouterr()
493505
assert out == "Prompt to stdin [y/N]: "
494506
assert err == ""
495507

508+
emulate_input("y\n")
509+
click.confirm("Prompt to stdin with no suffix", prompt_suffix="")
510+
out, err = capfd.readouterr()
511+
assert out == "Prompt to stdin with no suffix [y/N]"
512+
assert err == ""
513+
496514
emulate_input("y\n")
497515
click.confirm("Prompt to stderr", err=True)
498516
out, err = capfd.readouterr()
499517
assert out == " "
500518
assert err == "Prompt to stderr [y/N]:"
501519

520+
emulate_input("y\n")
521+
click.confirm("Prompt to stderr with no suffix", prompt_suffix="", err=True)
522+
out, err = capfd.readouterr()
523+
assert out == "]"
524+
assert err == "Prompt to stderr with no suffix [y/N"
525+
502526
monkeypatch.setattr(click.termui, "isatty", lambda x: True)
503527
monkeypatch.setattr(click.termui, "getchar", lambda: " ")
504528

0 commit comments

Comments
 (0)