[~] Refactor

This commit is contained in:
Siarhei Siniak 2022-08-27 13:04:43 +03:00
parent 1530fcf108
commit d4ed93961b
3 changed files with 258 additions and 240 deletions

@ -210,6 +210,11 @@ class TerminalInteractiveShell(InteractiveShell):
help="Shortcut style to use at the prompt. 'vi' or 'emacs'.", help="Shortcut style to use at the prompt. 'vi' or 'emacs'.",
).tag(config=True) ).tag(config=True)
fast_prompt = Bool(
False,
help="fast prompt with dynamic slow down",
).tag(config=True)
emacs_bindings_in_vi_insert_mode = Bool( emacs_bindings_in_vi_insert_mode = Bool(
True, True,
help="Add shortcuts from 'emacs' insert mode to 'vi' insert mode.", help="Add shortcuts from 'emacs' insert mode to 'vi' insert mode.",
@ -430,8 +435,10 @@ class TerminalInteractiveShell(InteractiveShell):
return return
# Set up keyboard shortcuts # Set up keyboard shortcuts
key_bindings = create_ipython_shortcuts(self) key_bindings = create_ipython_shortcuts(
self,
enable_escape_shortcuts=not self.fast_prompt,
)
# Pre-populate history from IPython's history database # Pre-populate history from IPython's history database
history = PtkHistoryAdapter(self) history = PtkHistoryAdapter(self)
@ -601,9 +608,12 @@ class TerminalInteractiveShell(InteractiveShell):
policy.set_event_loop(self.pt_loop) policy.set_event_loop(self.pt_loop)
try: try:
with patch_stdout(raw=True): with patch_stdout(raw=True):
extra_options = self._extra_prompt_options()
text = self.pt_app.prompt( text = self.pt_app.prompt(
default=default, default=default,
**self._extra_prompt_options()) **extra_options
)
finally: finally:
# Restore the original event loop. # Restore the original event loop.
if old_loop is not None and old_loop is not self.pt_loop: if old_loop is not None and old_loop is not self.pt_loop:

@ -48,7 +48,13 @@ def _apply_autosuggest(event):
else: else:
nc.end_of_line(event) nc.end_of_line(event)
def create_ipython_shortcuts(shell): def create_ipython_shortcuts(
shell,
enable_escape_shortcuts=None,
):
if enable_escape_shortcuts is None:
enable_escape_shortcuts = True
"""Set up the prompt_toolkit keyboard shortcuts for IPython""" """Set up the prompt_toolkit keyboard shortcuts for IPython"""
kb = KeyBindings() kb = KeyBindings()
@ -64,6 +70,7 @@ def create_ipython_shortcuts(shell):
& insert_mode & insert_mode
))(return_handler) ))(return_handler)
if enable_escape_shortcuts:
def reformat_and_execute(event): def reformat_and_execute(event):
reformat_text_before_cursor(event.current_buffer, event.current_buffer.document, shell) reformat_text_before_cursor(event.current_buffer, event.current_buffer.document, shell)
event.current_buffer.validate_and_handle() event.current_buffer.validate_and_handle()

@ -5,3 +5,4 @@ c.InteractiveShell.history_load_length = 100 * 1000 * 1000
c.InteractiveShell.pdb = True c.InteractiveShell.pdb = True
c.TerminalInteractiveShell.editing_mode = 'vi' c.TerminalInteractiveShell.editing_mode = 'vi'
c.TerminalInteractiveShell.modal_cursor = False c.TerminalInteractiveShell.modal_cursor = False
c.TerminalInteractiveShell.fast_prompt = True