fix: guard modal cursor import for prompt toolkit (#4758)

We shouldn't fail to start if the available PTK version doesn't support
modal cursor stuff.
(We should probably also make modal cursor stuff optional, but that can
happen later)
This commit is contained in:
Gil Forsyth 2022-04-15 13:08:43 +02:00 committed by GitHub
parent 8ec73c763a
commit 6a8b059789
Failed to generate hash of commit
2 changed files with 32 additions and 2 deletions

View file

@ -0,0 +1,24 @@
**Added:**
* <news item>
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* Allow xonsh to start gracefully even if modal cursors aren't in the available
prompt_toolkit version
**Security:**
* <news item>

View file

@ -7,7 +7,6 @@ from types import MethodType
from prompt_toolkit import ANSI
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.cursor_shapes import ModalCursorShapeConfig
from prompt_toolkit.enums import EditingMode
from prompt_toolkit.formatted_text import PygmentsTokens, to_formatted_text
from prompt_toolkit.history import ThreadedHistory
@ -44,6 +43,13 @@ try:
except ImportError:
HAVE_SYS_CLIPBOARD = False
try:
from prompt_toolkit.cursor_shapes import ModalCursorShapeConfig
HAVE_CURSOR_SHAPE = True
except ImportError:
HAVE_CURSOR_SHAPE = False
CAPITAL_PATTERN = re.compile(r"([a-z])([A-Z])")
Token = _TokenType()
@ -356,7 +362,7 @@ class PromptToolkitShell(BaseShell):
for attr, val in self.get_lazy_ptk_kwargs():
prompt_args[attr] = val
if editing_mode == EditingMode.VI:
if editing_mode == EditingMode.VI and HAVE_CURSOR_SHAPE:
prompt_args["cursor"] = ModalCursorShapeConfig()
events.on_pre_prompt.fire()
line = self.prompter.prompt(**prompt_args)