From 1ec0802fb00383401ca8fea6367b6286bddc8ae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20B=C3=A4hr?= Date: Wed, 22 May 2024 17:44:28 +0200 Subject: [PATCH] App: add readline-based tab-completion When executing FreeCAD in interactive console-mode, the init-script tries to load readline for a better input experience and enables tab-completion. This code is neither executed when running in GUI mode, nor when the console is not itneractive, e.g. when reading a script from stdin. Note that readline is not available on Windows, so we ignore import errors for now. With pyreadline3 there may be an alternative for Windows, but I cannot verify this on my setup. --- src/App/FreeCADInit.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/App/FreeCADInit.py b/src/App/FreeCADInit.py index d7dfd7c5a1..36e7c2a23a 100644 --- a/src/App/FreeCADInit.py +++ b/src/App/FreeCADInit.py @@ -957,7 +957,25 @@ class ReturnType(IntEnum): App.ReturnType = ReturnType +def TrySetupTabCompletion(): + """Tries to setup readline-based tab-completion + + Call this function only if you are in a tty-based REPL environment. + """ + try: + import readline + import rlcompleter + readline.parse_and_bind("tab: complete") + except ImportError as exc: + # Note: As there is no readline on Windows, we just ignore import errors here + pass + +# Note: just checking whether stdin is a TTY is not enough, as the GUI is set up only aftert this +# script has run. And checking only the RunMode is not enough, as we are maybe not interactive. +if App.ConfigGet('RunMode') == 'Cmd' and hasattr(sys.stdin, 'isatty') and sys.stdin.isatty(): + TrySetupTabCompletion() + # clean up namespace -del(InitApplications) +del InitApplications, TrySetupTabCompletion Log ('Init: App::FreeCADInit.py done\n')