Skip to content

Commit 909ebbf

Browse files
committed
Fix readline backspace/line-wrapping on linux
termui contains a workaround for windows where it prints a readline prompt itself. On linux, this confuses `readline` so things like backspace/ctrl-backspace and line wrapping don't work correctly. On platforms other than windows, just let readline handle the prompt.
1 parent 81a482f commit 909ebbf

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

src/click/termui.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from ._compat import isatty
1313
from ._compat import strip_ansi
14+
from ._compat import WIN
1415
from .exceptions import Abort
1516
from .exceptions import UsageError
1617
from .globals import resolve_color_default
@@ -136,12 +137,17 @@ def prompt(
136137
def prompt_func(text: str) -> str:
137138
f = hidden_prompt_func if hide_input else visible_prompt_func
138139
try:
139-
# Write the prompt separately so that we get nice
140-
# 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
143-
# readline causes backspace to clear the whole line.
144-
return f(" ")
140+
if WIN:
141+
# Write the prompt separately so that we get nice
142+
# coloring through colorama on Windows
143+
echo(text.rstrip(" "), nl=False, err=err)
144+
# Echo a space to stdout to work around an issue where
145+
# readline causes backspace to clear the whole line.
146+
return f(" ")
147+
else:
148+
# On non-Windows platforms, pass the full prompt to readline
149+
# so it can properly handle line editing and cursor positioning
150+
return f(text)
145151
except (KeyboardInterrupt, EOFError):
146152
# getpass doesn't print a newline if the user aborts input with ^C.
147153
# Allegedly this behavior is inherited from getpass(3).

0 commit comments

Comments
 (0)