Skip to content
Myr

Fix rendering when `prompt_suffix` is empty

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This changes the implementation of the fixes in #1836 and #2093 to address the problem that they created when `click.prompt` or `click.confirm` has a `prompt_suffix` value which is empty. This still fixes the problems reported in #665 and #2092 but does so without modifying the prompt and adding a trailing space. Previously, the problem was solved by trimming all trailing space characters from the prompt and then adding a single trailing space to every prompt. This was only visible if the last character in the prompt wasn't a space character. In those cases a prompt like "This is my prompt" would render as "This is my prompt ". Additionally, in the previous solution, if the `err=True` argument was passed, that final trailing space character that was added would be sent to STDOUT while the rest of the prompt would go to STDERR. This also wasn't very apparent since it was a space character. This different implementation uses the last character of the prompt to work around #665 and #2092 instead of adding a space character. This fixes most of the potentially confusing behavior. It does retain one confusing behavior which is described above, where if you use `err=True` the last character of the prompt is sent to STDOUT instead of STDERR. Previously, this last character sent to STDOUT would always be a space character. Now if the last character of the prompt is not a space character, that final character will go to STDOUT. For example if you call ```python click.prompt("bar", prompt_suffix="", err=True) ``` The `ba` will go to STDERR and the `r` will go to STDOUT. Previously `bar` would go to STDERR and an added ` ` would go to STDOUT. This odd behavior, which is already present but just applies to a space character, is worth accepting so that the `prompt_suffix` correctly renders the suffix as it did in version 7.1.2 and prior. This also adds unit tests for cases where `prompt_suffix` is empty. This fixes #3019

authored by Gene Wood · committed by Gene Wood · parent a8b41c0 · Browse files · 3 files+32 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+32 added−6 removed

  • tests:+24 added−0 removed
  • other:+8 added−6 removed

3 files+32 added−6 removed

modified:src/click/termui.py+6 added-6 removed
136 unmodified lines
137 f = hidden_prompt_func if hide_input else visible_prompt_func138 try:139 # Write the prompt separately so that we get nice140 # coloring through colorama on Windows141 echo(text.rstrip(" "), nl=False, err=err)142 # Echo a space to stdout to work around an issue where141 echo(text[:-1], nl=False, err=err)142 # Echo the last character to stdout to work around an issue where143 # readline causes backspace to clear the whole line.144 return f(" ")144 return f(text[-1:])145 except (KeyboardInterrupt, EOFError):146 # getpass doesn't print a newline if the user aborts input with ^C.147 # Allegedly this behavior is inherited from getpass(3).148 # A doc bug has been filed at https://bugs.python.org/issue24711
81 unmodified lines
230 while True:231 try:232 # Write the prompt separately so that we get nice233 # coloring through colorama on Windows234 echo(prompt.rstrip(" "), nl=False, err=err)235 # Echo a space to stdout to work around an issue where234 echo(prompt[:-1], nl=False, err=err)235 # Echo the last character to stdout to work around an issue where236 # readline causes backspace to clear the whole line.237 value = visible_prompt_func(" ").lower().strip()237 value = visible_prompt_func(prompt[-1:]).lower().strip()238 except (KeyboardInterrupt, EOFError):239 raise Abort() from None240 if value in ("y", "yes"):241 rv = True
636 unmodified lines
modified:tests/test_utils.py+24 added
429 unmodified lines
430 assert out == "Prompt to stdin: "431 assert err == ""432433 emulate_input("asdlkj\n")434 click.prompt("Prompt to stdin with no suffix", prompt_suffix="")435 out, err = capfd.readouterr()436 assert out == "Prompt to stdin with no suffix"437 assert err == ""438439 emulate_input("asdlkj\n")440 click.prompt("Prompt to stderr", err=True)441 out, err = capfd.readouterr()442 assert out == " "443 assert err == "Prompt to stderr:"444445 emulate_input("asdlkj\n")446 click.prompt("Prompt to stderr with no suffix", prompt_suffix="", err=True)447 out, err = capfd.readouterr()448 assert out == "x"449 assert err == "Prompt to stderr with no suffi"450451 emulate_input("y\n")452 click.confirm("Prompt to stdin")453 out, err = capfd.readouterr()454 assert out == "Prompt to stdin [y/N]: "455 assert err == ""456457 emulate_input("y\n")458 click.confirm("Prompt to stdin with no suffix", prompt_suffix="")459 out, err = capfd.readouterr()460 assert out == "Prompt to stdin with no suffix [y/N]"461 assert err == ""462463 emulate_input("y\n")464 click.confirm("Prompt to stderr", err=True)465 out, err = capfd.readouterr()466 assert out == " "467 assert err == "Prompt to stderr [y/N]:"468469 emulate_input("y\n")470 click.confirm("Prompt to stderr with no suffix", prompt_suffix="", err=True)471 out, err = capfd.readouterr()472 assert out == "]"473 assert err == "Prompt to stderr with no suffix [y/N"474475 monkeypatch.setattr(click.termui, "isatty", lambda x: True)476 monkeypatch.setattr(click.termui, "getchar", lambda: " ")477478 click.pause("Pause to stdout")
217 unmodified lines
modified:CHANGES.rst+2 added
13 unmodified lines
14- Use the value of ``Enum`` choices to render their default value in help15 screen. Refs :issue:`2911` :pr:`3004`16- Fix completion for the Z shell (``zsh``) for completion items containing17 colons. :issue:`2703` :pr:`2846`18- Fix rendering when ``prompt`` and ``confirm`` parameter ``prompt_suffix`` is19 empty. :issue:`3019`2021Version 8.2.122-------------23
1242 unmodified lines