diff --git a/CHANGES.rst b/CHANGES.rst index 95d6556df..f53001bf9 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -11,6 +11,8 @@ Unreleased the ``Context.invoke()`` method. :issue:`3066` :issue:`3065` :pr:`3068` - Fix conversion of ``Sentinel.UNSET`` happening too early, which caused incorrect behavior for multiple parameters using the same name. :issue:`3071` :pr:`3079` +- Fix rendering when ``prompt`` and ``confirm`` parameter ``prompt_suffix`` is + empty. :issue:`3019` :pr:`3021` Version 8.3.0 -------------- diff --git a/src/click/termui.py b/src/click/termui.py index dcbb22216..2e98a0771 100644 --- a/src/click/termui.py +++ b/src/click/termui.py @@ -119,6 +119,9 @@ def prompt( show_choices is true and text is "Group by" then the prompt will be "Group by (day, week): ". + .. versionchanged:: 8.3.1 + A space is no longer appended to the prompt. + .. versionadded:: 8.0 ``confirmation_prompt`` can be a custom string. @@ -138,10 +141,10 @@ def prompt_func(text: str) -> str: try: # Write the prompt separately so that we get nice # coloring through colorama on Windows - echo(text.rstrip(" "), nl=False, err=err) - # Echo a space to stdout to work around an issue where + echo(text[:-1], nl=False, err=err) + # Echo the last character to stdout to work around an issue where # readline causes backspace to clear the whole line. - return f(" ") + return f(text[-1:]) except (KeyboardInterrupt, EOFError): # getpass doesn't print a newline if the user aborts input with ^C. # Allegedly this behavior is inherited from getpass(3). @@ -214,6 +217,9 @@ def confirm( :param err: if set to true the file defaults to ``stderr`` instead of ``stdout``, the same as with echo. + .. versionchanged:: 8.3.1 + A space is no longer appended to the prompt. + .. versionchanged:: 8.0 Repeat until input is given if ``default`` is ``None``. @@ -231,10 +237,10 @@ def confirm( try: # Write the prompt separately so that we get nice # coloring through colorama on Windows - echo(prompt.rstrip(" "), nl=False, err=err) - # Echo a space to stdout to work around an issue where + echo(prompt[:-1], nl=False, err=err) + # Echo the last character to stdout to work around an issue where # readline causes backspace to clear the whole line. - value = visible_prompt_func(" ").lower().strip() + value = visible_prompt_func(prompt[-1:]).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 85d2b11ba..d1d2f6255 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -481,24 +481,48 @@ def emulate_input(text): assert out == "Prompt to stdin: " assert err == "" + emulate_input("asdlkj\n") + click.prompt("Prompt to stdin with no suffix", prompt_suffix="") + out, err = capfd.readouterr() + assert out == "Prompt to stdin with no suffix" + assert err == "" + emulate_input("asdlkj\n") click.prompt("Prompt to stderr", err=True) out, err = capfd.readouterr() assert out == " " assert err == "Prompt to stderr:" + emulate_input("asdlkj\n") + click.prompt("Prompt to stderr with no suffix", prompt_suffix="", err=True) + out, err = capfd.readouterr() + assert out == "x" + assert err == "Prompt to stderr with no suffi" + emulate_input("y\n") click.confirm("Prompt to stdin") out, err = capfd.readouterr() assert out == "Prompt to stdin [y/N]: " assert err == "" + emulate_input("y\n") + click.confirm("Prompt to stdin with no suffix", prompt_suffix="") + out, err = capfd.readouterr() + assert out == "Prompt to stdin with no suffix [y/N]" + assert err == "" + emulate_input("y\n") click.confirm("Prompt to stderr", err=True) out, err = capfd.readouterr() assert out == " " assert err == "Prompt to stderr [y/N]:" + emulate_input("y\n") + click.confirm("Prompt to stderr with no suffix", prompt_suffix="", err=True) + out, err = capfd.readouterr() + assert out == "]" + assert err == "Prompt to stderr with no suffix [y/N" + monkeypatch.setattr(click.termui, "isatty", lambda x: True) monkeypatch.setattr(click.termui, "getchar", lambda: " ")