Skip to content

Commit

Permalink
tab-completion in the development REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffmanr-cshs committed Nov 21, 2024
1 parent da1c646 commit 6596625
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions sanic/cli/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,24 @@ def __init__(self, app: Sanic):
HTTPX_AVAILABLE = False

try:
import readline # noqa
except ImportError:
import readline
import rlcompleter

if sys.version_info >= (3, 13):
# readline.backend added in python 3.13
READLINE_AVAILABLE = readline.backend
else:
# python 3.12 and earlier
readline_doc = getattr(readline, "__doc__", "")
if readline_doc is not None and "libedit" in readline_doc:
READLINE_AVAILABLE = "editline"
else:
READLINE_AVAILABLE = "readline"
assert READLINE_AVAILABLE in ("editline", "readline")
except (AssertionError, ImportError):
READLINE_AVAILABLE = False
print(
"Module 'readline' not available. History navigation will be limited.",
"Module 'readline' not available. REPL navigation may be limited.",
file=sys.stderr,
)

Expand Down Expand Up @@ -135,6 +149,14 @@ def __init__(self, app: Sanic, start: Optional[Default] = None):
"To enable it, install httpx:\n\t"
f"pip install httpx{Colors.END}\n"
)
if READLINE_AVAILABLE:
readline.set_completer(
rlcompleter.Completer(locals_available).complete
)
if READLINE_AVAILABLE == "readline":
readline.parse_and_bind("tab: complete")
elif READLINE_AVAILABLE == "editline":
readline.parse_and_bind("bind ^I rl_complete")
super().__init__(locals=locals_available)
self.compile.compiler.flags |= PyCF_ALLOW_TOP_LEVEL_AWAIT
self.loop = new_event_loop()
Expand Down

0 comments on commit 6596625

Please sign in to comment.