Skip to content

Commit 07f059c

Browse files
committed
Improved some readline-related comments
1 parent f217861 commit 07f059c

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

cmd2/cmd2.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3066,9 +3066,10 @@ def _set_up_cmd2_readline(self) -> _SavedReadlineSettings:
30663066

30673067
# Set up readline for our tab completion needs
30683068
if rl_type == RlType.GNU:
3069-
# Set GNU readline's rl_basic_quote_characters to NULL so it won't automatically add a closing quote
3070-
# We don't need to worry about setting rl_completion_suppress_quote since we never declared
3071-
# rl_completer_quote_characters.
3069+
# GNU readline automatically adds a closing quote if the text being completed has an opening quote.
3070+
# We don't want this behavior since cmd2 only adds a closing quote when self.allow_closing_quote is True.
3071+
# To fix this behavior, set readline's rl_basic_quote_characters to NULL. We don't need to worry about setting
3072+
# rl_completion_suppress_quote since we never declared rl_completer_quote_characters.
30723073
readline_settings.basic_quotes = cast(bytes, ctypes.cast(rl_basic_quote_characters, ctypes.c_void_p).value)
30733074
rl_basic_quote_characters.value = None
30743075

cmd2/rl_utils.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,29 @@
1010
Union,
1111
)
1212

13-
# Prefer statically linked gnureadline if available (for macOS compatibility due to issues with libedit)
13+
#########################################################################################################################
14+
# NOTE ON LIBEDIT:
15+
#
16+
# On Linux/Mac, the underlying readline API may be implemented by libedit instead of GNU readline.
17+
# We don't support libedit because it doesn't implement all the readline features cmd2 needs.
18+
#
19+
# For example:
20+
# cmd2 sets a custom display function using Python's readline.set_completion_display_matches_hook() to
21+
# support many of its advanced tab completion features (e.g. tab completion tables, displaying path basenames,
22+
# colored results, etc.). This function "sets or clears the rl_completion_display_matches_hook callback in the
23+
# underlying library". libedit has never implemented rl_completion_display_matches_hook. It merely sets it to NULL
24+
# and never references it.
25+
#
26+
# The workaround for Python environments using libedit is to install the gnureadline Python library.
27+
#########################################################################################################################
28+
29+
# Prefer statically linked gnureadline if available due to issues with libedit
1430
try:
1531
# noinspection PyPackageRequirements
1632
import gnureadline as readline # type: ignore[import]
1733
except ImportError:
18-
# Try to import readline, but allow failure for convenience in Windows unit testing
19-
# Note: If this actually fails, you should install readline on Linux or Mac or pyreadline on Windows
34+
# Try to import readline, but allow failure for convenience in Windows unit testing.
35+
# Note: If this actually fails, you should install gnureadline on Linux/Mac or pyreadline on Windows.
2036
try:
2137
# noinspection PyUnresolvedReferences
2238
import readline # type: ignore[no-redef]
@@ -125,7 +141,7 @@ def pyreadline_remove_history_item(pos: int) -> None:
125141
readline.remove_history_item = pyreadline_remove_history_item
126142

127143
elif 'gnureadline' in sys.modules or 'readline' in sys.modules:
128-
# We don't support libedit
144+
# We don't support libedit. See top of this file for why.
129145
if 'libedit' not in readline.__doc__:
130146
try:
131147
# Load the readline lib so we can access members of it
@@ -146,7 +162,7 @@ def pyreadline_remove_history_item(pos: int) -> None:
146162
if not _rl_warn_reason:
147163
_rl_warn_reason = (
148164
"no supported version of readline was found. To resolve this, install\n"
149-
"pyreadline on Windows or gnureadline on Mac."
165+
"pyreadline on Windows or gnureadline on Linux/Mac."
150166
)
151167
rl_warning = "Readline features including tab completion have been disabled because\n" + _rl_warn_reason + '\n\n'
152168
else:

0 commit comments

Comments
 (0)