1"""IPython terminal interface using prompt_toolkit"""
2
3import os
4import sys
5import inspect
6from warnings import warn
7from typing import Union as UnionType, Optional
8
9from IPython.core.async_helpers import get_asyncio_loop
10from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
11from IPython.utils.py3compat import input
12from IPython.utils.PyColorize import theme_table
13from IPython.utils.terminal import toggle_set_term_title, set_term_title, restore_term_title
14from IPython.utils.process import abbrev_cwd
15from traitlets import (
16 Any,
17 Bool,
18 Dict,
19 Enum,
20 Float,
21 Instance,
22 Integer,
23 List,
24 Type,
25 Unicode,
26 Union,
27 default,
28 observe,
29 validate,
30 DottedObjectName,
31)
32from traitlets.utils.importstring import import_item
33
34
35from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
36from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode
37from prompt_toolkit.filters import HasFocus, Condition, IsDone
38from prompt_toolkit.formatted_text import PygmentsTokens
39from prompt_toolkit.history import History
40from prompt_toolkit.layout.processors import ConditionalProcessor, HighlightMatchingBracketProcessor
41from prompt_toolkit.output import ColorDepth
42from prompt_toolkit.patch_stdout import patch_stdout
43from prompt_toolkit.shortcuts import PromptSession, CompleteStyle, print_formatted_text
44from prompt_toolkit.styles import DynamicStyle, merge_styles
45from prompt_toolkit.styles.pygments import style_from_pygments_cls, style_from_pygments_dict
46from pygments.styles import get_style_by_name
47from pygments.style import Style
48
49from .debugger import TerminalPdb, Pdb
50from .magics import TerminalMagics
51from .pt_inputhooks import get_inputhook_name_and_func
52from .prompts import Prompts, ClassicPrompts, RichPromptDisplayHook
53from .ptutils import IPythonPTCompleter, IPythonPTLexer
54from .shortcuts import (
55 KEY_BINDINGS,
56 UNASSIGNED_ALLOWED_COMMANDS,
57 create_ipython_shortcuts,
58 create_identifier,
59 RuntimeBinding,
60 add_binding,
61)
62from .shortcuts.filters import KEYBINDING_FILTERS, filter_from_string
63from .shortcuts.auto_suggest import (
64 NavigableAutoSuggestFromHistory,
65 AppendAutoSuggestionInAnyLine,
66)
67
68
69class _NoStyle(Style):
70 pass
71
72
73
74def _backward_compat_continuation_prompt_tokens(
75 method, width: int, *, lineno: int, wrap_count: int
76):
77 """
78 Sagemath use custom prompt and we broke them in 8.19.
79
80 make sure to pass only width if method only support width
81 """
82 sig = inspect.signature(method)
83 extra = {}
84 params = inspect.signature(method).parameters
85 if "lineno" in inspect.signature(method).parameters or any(
86 [p.kind == p.VAR_KEYWORD for p in sig.parameters.values()]
87 ):
88 extra["lineno"] = lineno
89 if "line_number" in inspect.signature(method).parameters or any(
90 [p.kind == p.VAR_KEYWORD for p in sig.parameters.values()]
91 ):
92 extra["line_number"] = lineno
93
94 if "wrap_count" in inspect.signature(method).parameters or any(
95 [p.kind == p.VAR_KEYWORD for p in sig.parameters.values()]
96 ):
97 extra["wrap_count"] = wrap_count
98 return method(width, **extra)
99
100
101def get_default_editor():
102 try:
103 return os.environ['EDITOR']
104 except KeyError:
105 pass
106 except UnicodeError:
107 warn("$EDITOR environment variable is not pure ASCII. Using platform "
108 "default editor.")
109
110 if os.name == 'posix':
111 return 'vi' # the only one guaranteed to be there!
112 else:
113 return "notepad" # same in Windows!
114
115
116# conservatively check for tty
117# overridden streams can result in things like:
118# - sys.stdin = None
119# - no isatty method
120for _name in ('stdin', 'stdout', 'stderr'):
121 _stream = getattr(sys, _name)
122 try:
123 if not _stream or not hasattr(_stream, "isatty") or not _stream.isatty():
124 _is_tty = False
125 break
126 except ValueError:
127 # stream is closed
128 _is_tty = False
129 break
130else:
131 _is_tty = True
132
133
134_use_simple_prompt = ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or (not _is_tty)
135
136def black_reformat_handler(text_before_cursor):
137 """
138 We do not need to protect against error,
139 this is taken care at a higher level where any reformat error is ignored.
140 Indeed we may call reformatting on incomplete code.
141 """
142 import black
143
144 formatted_text = black.format_str(text_before_cursor, mode=black.FileMode())
145 if not text_before_cursor.endswith("\n") and formatted_text.endswith("\n"):
146 formatted_text = formatted_text[:-1]
147 return formatted_text
148
149
150def yapf_reformat_handler(text_before_cursor):
151 from yapf.yapflib import file_resources
152 from yapf.yapflib import yapf_api
153
154 style_config = file_resources.GetDefaultStyleForDir(os.getcwd())
155 formatted_text, was_formatted = yapf_api.FormatCode(
156 text_before_cursor, style_config=style_config
157 )
158 if was_formatted:
159 if not text_before_cursor.endswith("\n") and formatted_text.endswith("\n"):
160 formatted_text = formatted_text[:-1]
161 return formatted_text
162 else:
163 return text_before_cursor
164
165
166class PtkHistoryAdapter(History):
167 """
168 Prompt toolkit has it's own way of handling history, Where it assumes it can
169 Push/pull from history.
170
171 """
172
173 def __init__(self, shell):
174 super().__init__()
175 self.shell = shell
176 self._refresh()
177
178 def append_string(self, string):
179 # we rely on sql for that.
180 self._loaded = False
181 self._refresh()
182
183 def _refresh(self):
184 if not self._loaded:
185 self._loaded_strings = list(self.load_history_strings())
186
187 def load_history_strings(self):
188 last_cell = ""
189 res = []
190 for __, ___, cell in self.shell.history_manager.get_tail(
191 self.shell.history_load_length, include_latest=True
192 ):
193 # Ignore blank lines and consecutive duplicates
194 cell = cell.rstrip()
195 if cell and (cell != last_cell):
196 res.append(cell)
197 last_cell = cell
198 yield from res[::-1]
199
200 def store_string(self, string: str) -> None:
201 pass
202
203class TerminalInteractiveShell(InteractiveShell):
204 mime_renderers = Dict().tag(config=True)
205
206 min_elide = Integer(
207 30, help="minimum characters for filling with ellipsis in file completions"
208 ).tag(config=True)
209 space_for_menu = Integer(
210 6,
211 help="Number of line at the bottom of the screen "
212 "to reserve for the tab completion menu, "
213 "search history, ...etc, the height of "
214 "these menus will at most this value. "
215 "Increase it is you prefer long and skinny "
216 "menus, decrease for short and wide.",
217 ).tag(config=True)
218
219 pt_app: UnionType[PromptSession, None] = None
220 auto_suggest: UnionType[
221 AutoSuggestFromHistory,
222 NavigableAutoSuggestFromHistory,
223 None,
224 ] = None
225 debugger_history = None
226
227 debugger_history_file = Unicode(
228 "~/.pdbhistory", help="File in which to store and read history"
229 ).tag(config=True)
230
231 simple_prompt = Bool(_use_simple_prompt,
232 help="""Use `raw_input` for the REPL, without completion and prompt colors.
233
234 Useful when controlling IPython as a subprocess, and piping
235 STDIN/OUT/ERR. Known usage are: IPython's own testing machinery,
236 and emacs' inferior-python subprocess (assuming you have set
237 `python-shell-interpreter` to "ipython") available through the
238 built-in `M-x run-python` and third party packages such as elpy.
239
240 This mode default to `True` if the `IPY_TEST_SIMPLE_PROMPT`
241 environment variable is set, or the current terminal is not a tty.
242 Thus the Default value reported in --help-all, or config will often
243 be incorrectly reported.
244 """,
245 ).tag(config=True)
246
247 @property
248 def debugger_cls(self):
249 return Pdb if self.simple_prompt else TerminalPdb
250
251 confirm_exit = Bool(True,
252 help="""
253 Set to confirm when you try to exit IPython with an EOF (Control-D
254 in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
255 you can force a direct exit without any confirmation.""",
256 ).tag(config=True)
257
258 editing_mode = Unicode('emacs',
259 help="Shortcut style to use at the prompt. 'vi' or 'emacs'.",
260 ).tag(config=True)
261
262 emacs_bindings_in_vi_insert_mode = Bool(
263 True,
264 help="Add shortcuts from 'emacs' insert mode to 'vi' insert mode.",
265 ).tag(config=True)
266
267 modal_cursor = Bool(
268 True,
269 help="""
270 Cursor shape changes depending on vi mode: beam in vi insert mode,
271 block in nav mode, underscore in replace mode.""",
272 ).tag(config=True)
273
274 ttimeoutlen = Float(
275 0.01,
276 help="""The time in milliseconds that is waited for a key code
277 to complete.""",
278 ).tag(config=True)
279
280 timeoutlen = Float(
281 0.5,
282 help="""The time in milliseconds that is waited for a mapped key
283 sequence to complete.""",
284 ).tag(config=True)
285
286 autoformatter = Unicode(
287 None,
288 help="Autoformatter to reformat Terminal code. Can be `'black'`, `'yapf'` or `None`",
289 allow_none=True
290 ).tag(config=True)
291
292 auto_match = Bool(
293 False,
294 help="""
295 Automatically add/delete closing bracket or quote when opening bracket or quote is entered/deleted.
296 Brackets: (), [], {}
297 Quotes: '', \"\"
298 """,
299 ).tag(config=True)
300
301 mouse_support = Bool(False,
302 help="Enable mouse support in the prompt\n(Note: prevents selecting text with the mouse)"
303 ).tag(config=True)
304
305 # We don't load the list of styles for the help string, because loading
306 # Pygments plugins takes time and can cause unexpected errors.
307 highlighting_style = Union(
308 [Unicode("legacy"), Type(klass=Style)],
309 help="""Deprecated, and has not effect, use IPython themes
310
311 The name or class of a Pygments style to use for syntax
312 highlighting. To see available styles, run `pygmentize -L styles`.""",
313 ).tag(config=True)
314
315 @validate('editing_mode')
316 def _validate_editing_mode(self, proposal):
317 if proposal['value'].lower() == 'vim':
318 proposal['value']= 'vi'
319 elif proposal['value'].lower() == 'default':
320 proposal['value']= 'emacs'
321
322 if hasattr(EditingMode, proposal['value'].upper()):
323 return proposal['value'].lower()
324
325 return self.editing_mode
326
327 @observe('editing_mode')
328 def _editing_mode(self, change):
329 if self.pt_app:
330 self.pt_app.editing_mode = getattr(EditingMode, change.new.upper())
331
332 def _set_formatter(self, formatter):
333 if formatter is None:
334 self.reformat_handler = lambda x:x
335 elif formatter == 'black':
336 self.reformat_handler = black_reformat_handler
337 elif formatter == "yapf":
338 self.reformat_handler = yapf_reformat_handler
339 else:
340 raise ValueError
341
342 @observe("autoformatter")
343 def _autoformatter_changed(self, change):
344 formatter = change.new
345 self._set_formatter(formatter)
346
347 @observe('highlighting_style')
348 @observe('colors')
349 def _highlighting_style_changed(self, change):
350 assert change.new == change.new.lower()
351 if change.new != "legacy":
352 warn(
353 "highlighting_style is deprecated since 9.0 and have no effect, use themeing."
354 )
355 return
356
357 def refresh_style(self):
358 self._style = self._make_style_from_name_or_cls("legacy")
359
360 # TODO: deprecate this
361 highlighting_style_overrides = Dict(
362 help="Override highlighting format for specific tokens"
363 ).tag(config=True)
364
365 true_color = Bool(False,
366 help="""Use 24bit colors instead of 256 colors in prompt highlighting.
367 If your terminal supports true color, the following command should
368 print ``TRUECOLOR`` in orange::
369
370 printf \"\\x1b[38;2;255;100;0mTRUECOLOR\\x1b[0m\\n\"
371 """,
372 ).tag(config=True)
373
374 editor = Unicode(get_default_editor(),
375 help="Set the editor used by IPython (default to $EDITOR/vi/notepad)."
376 ).tag(config=True)
377
378 prompts_class = Type(Prompts, help='Class used to generate Prompt token for prompt_toolkit').tag(config=True)
379
380 prompts = Instance(Prompts)
381
382 @default('prompts')
383 def _prompts_default(self):
384 return self.prompts_class(self)
385
386# @observe('prompts')
387# def _(self, change):
388# self._update_layout()
389
390 @default('displayhook_class')
391 def _displayhook_class_default(self):
392 return RichPromptDisplayHook
393
394 term_title = Bool(True,
395 help="Automatically set the terminal title"
396 ).tag(config=True)
397
398 term_title_format = Unicode("IPython: {cwd}",
399 help="Customize the terminal title format. This is a python format string. " +
400 "Available substitutions are: {cwd}."
401 ).tag(config=True)
402
403 display_completions = Enum(('column', 'multicolumn','readlinelike'),
404 help= ( "Options for displaying tab completions, 'column', 'multicolumn', and "
405 "'readlinelike'. These options are for `prompt_toolkit`, see "
406 "`prompt_toolkit` documentation for more information."
407 ),
408 default_value='multicolumn').tag(config=True)
409
410 highlight_matching_brackets = Bool(True,
411 help="Highlight matching brackets.",
412 ).tag(config=True)
413
414 extra_open_editor_shortcuts = Bool(False,
415 help="Enable vi (v) or Emacs (C-X C-E) shortcuts to open an external editor. "
416 "This is in addition to the F2 binding, which is always enabled."
417 ).tag(config=True)
418
419 handle_return = Any(None,
420 help="Provide an alternative handler to be called when the user presses "
421 "Return. This is an advanced option intended for debugging, which "
422 "may be changed or removed in later releases."
423 ).tag(config=True)
424
425 enable_history_search = Bool(True,
426 help="Allows to enable/disable the prompt toolkit history search"
427 ).tag(config=True)
428
429 autosuggestions_provider = Unicode(
430 "NavigableAutoSuggestFromHistory",
431 help="Specifies from which source automatic suggestions are provided. "
432 "Can be set to ``'NavigableAutoSuggestFromHistory'`` (:kbd:`up` and "
433 ":kbd:`down` swap suggestions), ``'AutoSuggestFromHistory'``, "
434 " or ``None`` to disable automatic suggestions. "
435 "Default is `'NavigableAutoSuggestFromHistory`'.",
436 allow_none=True,
437 ).tag(config=True)
438 _autosuggestions_provider: Any
439
440 llm_constructor_kwargs = Dict(
441 {},
442 help="""
443 Extra arguments to pass to `llm_provider_class` constructor.
444
445 This is used to – for example – set the `model_id`""",
446 ).tag(config=True)
447
448 llm_prefix_from_history = DottedObjectName(
449 "input_history",
450 help="""\
451 Fully Qualifed name of a function that takes an IPython history manager and
452 return a prefix to pass the llm provider in addition to the current buffer
453 text.
454
455 You can use:
456
457 - no_prefix
458 - input_history
459
460 As default value. `input_history` (default), will use all the input history
461 of current IPython session
462
463 """,
464 ).tag(config=True)
465 _llm_prefix_from_history: Any
466
467 @observe("llm_prefix_from_history")
468 def _llm_prefix_from_history_changed(self, change):
469 name = change.new
470 self._llm_prefix_from_history = name
471 self._set_autosuggestions()
472
473 llm_provider_class = DottedObjectName(
474 None,
475 allow_none=True,
476 help="""\
477 Provisional:
478 This is a provisional API in IPython 8.32, before stabilisation
479 in 9.0, it may change without warnings.
480
481 class to use for the `NavigableAutoSuggestFromHistory` to request
482 completions from a LLM, this should inherit from
483 `jupyter_ai_magics:BaseProvider` and implement
484 `stream_inline_completions`
485 """,
486 ).tag(config=True)
487 _llm_provider_class: Any = None
488
489 @observe("llm_provider_class")
490 def _llm_provider_class_changed(self, change):
491 provider_class = change.new
492 self._llm_provider_class = provider_class
493 self._set_autosuggestions()
494
495 def _set_autosuggestions(self, provider=None):
496 if provider is None:
497 provider = self.autosuggestions_provider
498 # disconnect old handler
499 if self.auto_suggest and isinstance(
500 self.auto_suggest, NavigableAutoSuggestFromHistory
501 ):
502 self.auto_suggest.disconnect()
503 if provider is None:
504 self.auto_suggest = None
505 elif provider == "AutoSuggestFromHistory":
506 self.auto_suggest = AutoSuggestFromHistory()
507 elif provider == "NavigableAutoSuggestFromHistory":
508 # LLM stuff are all Provisional in 8.32
509 if self._llm_provider_class:
510
511 def init_llm_provider():
512 llm_provider_constructor = import_item(self._llm_provider_class)
513 return llm_provider_constructor(**self.llm_constructor_kwargs)
514
515 else:
516 init_llm_provider = None
517 self.auto_suggest = NavigableAutoSuggestFromHistory()
518 # Provisinal in 8.32
519 self.auto_suggest._init_llm_provider = init_llm_provider
520
521 name = self.llm_prefix_from_history
522
523 if name == "no_prefix":
524
525 def no_prefix(history_manager):
526 return ""
527
528 fun = no_prefix
529
530 elif name == "input_history":
531
532 def input_history(history_manager):
533 return "\n".join([s[2] for s in history_manager.get_range()]) + "\n"
534
535 fun = input_history
536
537 else:
538 fun = import_item(name)
539 self.auto_suggest._llm_prefixer = fun
540 else:
541 raise ValueError("No valid provider.")
542 if self.pt_app:
543 self.pt_app.auto_suggest = self.auto_suggest
544
545 @observe("autosuggestions_provider")
546 def _autosuggestions_provider_changed(self, change):
547 provider = change.new
548 self._set_autosuggestions(provider)
549
550 shortcuts = List(
551 trait=Dict(
552 key_trait=Enum(
553 [
554 "command",
555 "match_keys",
556 "match_filter",
557 "new_keys",
558 "new_filter",
559 "create",
560 ]
561 ),
562 per_key_traits={
563 "command": Unicode(),
564 "match_keys": List(Unicode()),
565 "match_filter": Unicode(),
566 "new_keys": List(Unicode()),
567 "new_filter": Unicode(),
568 "create": Bool(False),
569 },
570 ),
571 help="""
572 Add, disable or modifying shortcuts.
573
574 Each entry on the list should be a dictionary with ``command`` key
575 identifying the target function executed by the shortcut and at least
576 one of the following:
577
578 - ``match_keys``: list of keys used to match an existing shortcut,
579 - ``match_filter``: shortcut filter used to match an existing shortcut,
580 - ``new_keys``: list of keys to set,
581 - ``new_filter``: a new shortcut filter to set
582
583 The filters have to be composed of pre-defined verbs and joined by one
584 of the following conjunctions: ``&`` (and), ``|`` (or), ``~`` (not).
585 The pre-defined verbs are:
586
587 {filters}
588
589 To disable a shortcut set ``new_keys`` to an empty list.
590 To add a shortcut add key ``create`` with value ``True``.
591
592 When modifying/disabling shortcuts, ``match_keys``/``match_filter`` can
593 be omitted if the provided specification uniquely identifies a shortcut
594 to be modified/disabled. When modifying a shortcut ``new_filter`` or
595 ``new_keys`` can be omitted which will result in reuse of the existing
596 filter/keys.
597
598 Only shortcuts defined in IPython (and not default prompt-toolkit
599 shortcuts) can be modified or disabled. The full list of shortcuts,
600 command identifiers and filters is available under
601 :ref:`terminal-shortcuts-list`.
602
603 Here is an example:
604
605 .. code::
606
607 c.TerminalInteractiveShell.shortcuts = [
608 {{
609 "new_keys": ["c-q"],
610 "command": "prompt_toolkit:named_commands.capitalize_word",
611 "create": True,
612 }},
613 {{
614 "new_keys": ["c-j"],
615 "command": "prompt_toolkit:named_commands.beginning_of_line",
616 "create": True,
617 }},
618 ]
619
620
621 """.format(
622 filters="\n ".join([f" - ``{k}``" for k in KEYBINDING_FILTERS])
623 ),
624 ).tag(config=True)
625
626 @observe("shortcuts")
627 def _shortcuts_changed(self, change):
628 if self.pt_app:
629 self.pt_app.key_bindings = self._merge_shortcuts(user_shortcuts=change.new)
630
631 def _merge_shortcuts(self, user_shortcuts):
632 # rebuild the bindings list from scratch
633 key_bindings = create_ipython_shortcuts(self)
634
635 # for now we only allow adding shortcuts for a specific set of
636 # commands; this is a security precution.
637 allowed_commands = {
638 create_identifier(binding.command): binding.command
639 for binding in KEY_BINDINGS
640 }
641 allowed_commands.update(
642 {
643 create_identifier(command): command
644 for command in UNASSIGNED_ALLOWED_COMMANDS
645 }
646 )
647 shortcuts_to_skip = []
648 shortcuts_to_add = []
649
650 for shortcut in user_shortcuts:
651 command_id = shortcut["command"]
652 if command_id not in allowed_commands:
653 allowed_commands = "\n - ".join(allowed_commands)
654 raise ValueError(
655 f"{command_id} is not a known shortcut command."
656 f" Allowed commands are: \n - {allowed_commands}"
657 )
658 old_keys = shortcut.get("match_keys", None)
659 old_filter = (
660 filter_from_string(shortcut["match_filter"])
661 if "match_filter" in shortcut
662 else None
663 )
664 matching = [
665 binding
666 for binding in KEY_BINDINGS
667 if (
668 (old_filter is None or binding.filter == old_filter)
669 and (old_keys is None or [k for k in binding.keys] == old_keys)
670 and create_identifier(binding.command) == command_id
671 )
672 ]
673
674 new_keys = shortcut.get("new_keys", None)
675 new_filter = shortcut.get("new_filter", None)
676
677 command = allowed_commands[command_id]
678
679 creating_new = shortcut.get("create", False)
680 modifying_existing = not creating_new and (
681 new_keys is not None or new_filter
682 )
683
684 if creating_new and new_keys == []:
685 raise ValueError("Cannot add a shortcut without keys")
686
687 if modifying_existing:
688 specification = {
689 key: shortcut[key]
690 for key in ["command", "filter"]
691 if key in shortcut
692 }
693 if len(matching) == 0:
694 raise ValueError(
695 f"No shortcuts matching {specification} found in {KEY_BINDINGS}"
696 )
697 elif len(matching) > 1:
698 raise ValueError(
699 f"Multiple shortcuts matching {specification} found,"
700 f" please add keys/filter to select one of: {matching}"
701 )
702
703 matched = matching[0]
704 old_filter = matched.filter
705 old_keys = list(matched.keys)
706 shortcuts_to_skip.append(
707 RuntimeBinding(
708 command,
709 keys=old_keys,
710 filter=old_filter,
711 )
712 )
713
714 if new_keys != []:
715 shortcuts_to_add.append(
716 RuntimeBinding(
717 command,
718 keys=new_keys or old_keys,
719 filter=(
720 filter_from_string(new_filter)
721 if new_filter is not None
722 else (
723 old_filter
724 if old_filter is not None
725 else filter_from_string("always")
726 )
727 ),
728 )
729 )
730
731 # rebuild the bindings list from scratch
732 key_bindings = create_ipython_shortcuts(self, skip=shortcuts_to_skip)
733 for binding in shortcuts_to_add:
734 add_binding(key_bindings, binding)
735
736 return key_bindings
737
738 prompt_includes_vi_mode = Bool(True,
739 help="Display the current vi mode (when using vi editing mode)."
740 ).tag(config=True)
741
742 prompt_line_number_format = Unicode(
743 "",
744 help="The format for line numbering, will be passed `line` (int, 1 based)"
745 " the current line number and `rel_line` the relative line number."
746 " for example to display both you can use the following template string :"
747 " c.TerminalInteractiveShell.prompt_line_number_format='{line: 4d}/{rel_line:+03d} | '"
748 " This will display the current line number, with leading space and a width of at least 4"
749 " character, as well as the relative line number 0 padded and always with a + or - sign."
750 " Note that when using Emacs mode the prompt of the first line may not update.",
751 ).tag(config=True)
752
753 @observe('term_title')
754 def init_term_title(self, change=None):
755 # Enable or disable the terminal title.
756 if self.term_title and _is_tty:
757 toggle_set_term_title(True)
758 set_term_title(self.term_title_format.format(cwd=abbrev_cwd()))
759 else:
760 toggle_set_term_title(False)
761
762 def restore_term_title(self):
763 if self.term_title and _is_tty:
764 restore_term_title()
765
766 def init_display_formatter(self):
767 super(TerminalInteractiveShell, self).init_display_formatter()
768 # terminal only supports plain text
769 self.display_formatter.active_types = ["text/plain"]
770
771 def init_prompt_toolkit_cli(self):
772 if self.simple_prompt:
773 # Fall back to plain non-interactive output for tests.
774 # This is very limited.
775 def prompt():
776 prompt_text = "".join(x[1] for x in self.prompts.in_prompt_tokens())
777 lines = [input(prompt_text)]
778 prompt_continuation = "".join(
779 x[1] for x in self.prompts.continuation_prompt_tokens()
780 )
781 while self.check_complete("\n".join(lines))[0] == "incomplete":
782 lines.append(input(prompt_continuation))
783 return "\n".join(lines)
784
785 self.prompt_for_code = prompt
786 return
787
788 # Set up keyboard shortcuts
789 key_bindings = self._merge_shortcuts(user_shortcuts=self.shortcuts)
790
791 # Pre-populate history from IPython's history database
792 history = PtkHistoryAdapter(self)
793
794 self.refresh_style()
795 ptk_s = DynamicStyle(lambda: self._style)
796
797 editing_mode = getattr(EditingMode, self.editing_mode.upper())
798
799 self._use_asyncio_inputhook = False
800 self.pt_app = PromptSession(
801 auto_suggest=self.auto_suggest,
802 editing_mode=editing_mode,
803 key_bindings=key_bindings,
804 history=history,
805 completer=IPythonPTCompleter(shell=self),
806 enable_history_search=self.enable_history_search,
807 style=ptk_s,
808 include_default_pygments_style=False,
809 mouse_support=self.mouse_support,
810 enable_open_in_editor=self.extra_open_editor_shortcuts,
811 color_depth=self.color_depth,
812 tempfile_suffix=".py",
813 **self._extra_prompt_options(),
814 )
815 if isinstance(self.auto_suggest, NavigableAutoSuggestFromHistory):
816 self.auto_suggest.connect(self.pt_app)
817
818 def _make_style_from_name_or_cls(self, name_or_cls):
819 """
820 Small wrapper that make an IPython compatible style from a style name
821
822 We need that to add style for prompt ... etc.
823 """
824 assert name_or_cls == "legacy"
825 legacy = self.colors.lower()
826
827 theme = theme_table.get(legacy, None)
828 assert theme is not None, legacy
829
830 if legacy == "nocolor":
831 style_overrides = {}
832 style_cls = _NoStyle
833 else:
834 style_overrides = {**theme.extra_style, **self.highlighting_style_overrides}
835 if theme.base is not None:
836 style_cls = get_style_by_name(theme.base)
837 else:
838 style_cls = _NoStyle
839
840 style = merge_styles(
841 [
842 style_from_pygments_cls(style_cls),
843 style_from_pygments_dict(style_overrides),
844 ]
845 )
846
847 return style
848
849 @property
850 def pt_complete_style(self):
851 return {
852 'multicolumn': CompleteStyle.MULTI_COLUMN,
853 'column': CompleteStyle.COLUMN,
854 'readlinelike': CompleteStyle.READLINE_LIKE,
855 }[self.display_completions]
856
857 @property
858 def color_depth(self):
859 return (ColorDepth.TRUE_COLOR if self.true_color else None)
860
861 def _ptk_prompt_cont(self, width: int, line_number: int, wrap_count: int):
862 return PygmentsTokens(
863 _backward_compat_continuation_prompt_tokens(
864 self.prompts.continuation_prompt_tokens,
865 width,
866 lineno=line_number,
867 wrap_count=wrap_count,
868 )
869 )
870
871 def _extra_prompt_options(self):
872 """
873 Return the current layout option for the current Terminal InteractiveShell
874 """
875 def get_message():
876 return PygmentsTokens(self.prompts.in_prompt_tokens())
877
878 if self.editing_mode == "emacs" and self.prompt_line_number_format == "":
879 # with emacs mode the prompt is (usually) static, so we call only
880 # the function once. With VI mode it can toggle between [ins] and
881 # [nor] so we can't precompute.
882 # here I'm going to favor the default keybinding which almost
883 # everybody uses to decrease CPU usage.
884 # if we have issues with users with custom Prompts we can see how to
885 # work around this.
886 get_message = get_message()
887
888 options = {
889 "complete_in_thread": False,
890 "lexer": IPythonPTLexer(),
891 "reserve_space_for_menu": self.space_for_menu,
892 "message": get_message,
893 "prompt_continuation": self._ptk_prompt_cont,
894 "multiline": True,
895 "complete_style": self.pt_complete_style,
896 "input_processors": [
897 # Highlight matching brackets, but only when this setting is
898 # enabled, and only when the DEFAULT_BUFFER has the focus.
899 ConditionalProcessor(
900 processor=HighlightMatchingBracketProcessor(chars="[](){}"),
901 filter=HasFocus(DEFAULT_BUFFER)
902 & ~IsDone()
903 & Condition(lambda: self.highlight_matching_brackets),
904 ),
905 # Show auto-suggestion in lines other than the last line.
906 ConditionalProcessor(
907 processor=AppendAutoSuggestionInAnyLine(),
908 filter=HasFocus(DEFAULT_BUFFER)
909 & ~IsDone()
910 & Condition(
911 lambda: isinstance(
912 self.auto_suggest,
913 NavigableAutoSuggestFromHistory,
914 )
915 ),
916 ),
917 ],
918 }
919
920 return options
921
922 def prompt_for_code(self):
923 if self.rl_next_input:
924 default = self.rl_next_input
925 self.rl_next_input = None
926 else:
927 default = ''
928
929 # In order to make sure that asyncio code written in the
930 # interactive shell doesn't interfere with the prompt, we run the
931 # prompt in a different event loop.
932 # If we don't do this, people could spawn coroutine with a
933 # while/true inside which will freeze the prompt.
934
935 with patch_stdout(raw=True):
936 if self._use_asyncio_inputhook:
937 # When we integrate the asyncio event loop, run the UI in the
938 # same event loop as the rest of the code. don't use an actual
939 # input hook. (Asyncio is not made for nesting event loops.)
940 asyncio_loop = get_asyncio_loop()
941 text = asyncio_loop.run_until_complete(
942 self.pt_app.prompt_async(
943 default=default, **self._extra_prompt_options()
944 )
945 )
946 else:
947 text = self.pt_app.prompt(
948 default=default,
949 inputhook=self._inputhook,
950 **self._extra_prompt_options(),
951 )
952
953 return text
954
955 def init_io(self):
956 if sys.platform not in {'win32', 'cli'}:
957 return
958
959 import colorama
960 colorama.init()
961
962 def init_magics(self):
963 super(TerminalInteractiveShell, self).init_magics()
964 self.register_magics(TerminalMagics)
965
966 def init_alias(self):
967 # The parent class defines aliases that can be safely used with any
968 # frontend.
969 super(TerminalInteractiveShell, self).init_alias()
970
971 # Now define aliases that only make sense on the terminal, because they
972 # need direct access to the console in a way that we can't emulate in
973 # GUI or web frontend
974 if os.name == 'posix':
975 for cmd in ('clear', 'more', 'less', 'man'):
976 self.alias_manager.soft_define_alias(cmd, cmd)
977
978 def __init__(self, *args, **kwargs) -> None:
979 super().__init__(*args, **kwargs)
980 self._set_autosuggestions(self.autosuggestions_provider)
981 self.init_prompt_toolkit_cli()
982 self.init_term_title()
983 self.keep_running = True
984 self._set_formatter(self.autoformatter)
985
986 def ask_exit(self):
987 self.keep_running = False
988
989 rl_next_input = None
990
991 def interact(self):
992 self.keep_running = True
993 while self.keep_running:
994 print(self.separate_in, end='')
995
996 try:
997 code = self.prompt_for_code()
998 except EOFError:
999 if (not self.confirm_exit) \
1000 or self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'):
1001 self.ask_exit()
1002
1003 else:
1004 if code:
1005 self.run_cell(code, store_history=True)
1006
1007 def mainloop(self):
1008 # An extra layer of protection in case someone mashing Ctrl-C breaks
1009 # out of our internal code.
1010 while True:
1011 try:
1012 self.interact()
1013 break
1014 except KeyboardInterrupt as e:
1015 print("\n%s escaped interact()\n" % type(e).__name__)
1016 finally:
1017 # An interrupt during the eventloop will mess up the
1018 # internal state of the prompt_toolkit library.
1019 # Stopping the eventloop fixes this, see
1020 # https://github.com/ipython/ipython/pull/9867
1021 if hasattr(self, '_eventloop'):
1022 self._eventloop.stop()
1023
1024 self.restore_term_title()
1025
1026 # try to call some at-exit operation optimistically as some things can't
1027 # be done during interpreter shutdown. this is technically inaccurate as
1028 # this make mainlool not re-callable, but that should be a rare if not
1029 # in existent use case.
1030
1031 self._atexit_once()
1032
1033 _inputhook = None
1034 def inputhook(self, context):
1035 warn(
1036 "inputkook seem unused, and marked for deprecation/Removal as of IPython 9.0. "
1037 "Please open an issue if you are using it.",
1038 category=PendingDeprecationWarning,
1039 stacklevel=2,
1040 )
1041 if self._inputhook is not None:
1042 self._inputhook(context)
1043
1044 active_eventloop: Optional[str] = None
1045
1046 def enable_gui(self, gui: Optional[str] = None) -> None:
1047 if gui:
1048 from ..core.pylabtools import _convert_gui_from_matplotlib
1049
1050 gui = _convert_gui_from_matplotlib(gui)
1051
1052 if self.simple_prompt is True and gui is not None:
1053 print(
1054 f'Cannot install event loop hook for "{gui}" when running with `--simple-prompt`.'
1055 )
1056 print(
1057 "NOTE: Tk is supported natively; use Tk apps and Tk backends with `--simple-prompt`."
1058 )
1059 return
1060
1061 if self._inputhook is None and gui is None:
1062 print("No event loop hook running.")
1063 return
1064
1065 if self._inputhook is not None and gui is not None:
1066 newev, newinhook = get_inputhook_name_and_func(gui)
1067 if self._inputhook == newinhook:
1068 # same inputhook, do nothing
1069 self.log.info(
1070 f"Shell is already running the {self.active_eventloop} eventloop. Doing nothing"
1071 )
1072 return
1073 self.log.warning(
1074 f"Shell is already running a different gui event loop for {self.active_eventloop}. "
1075 "Call with no arguments to disable the current loop."
1076 )
1077 return
1078 if self._inputhook is not None and gui is None:
1079 self.active_eventloop = self._inputhook = None
1080
1081 if gui and (gui not in {None, "webagg"}):
1082 # This hook runs with each cycle of the `prompt_toolkit`'s event loop.
1083 self.active_eventloop, self._inputhook = get_inputhook_name_and_func(gui)
1084 else:
1085 self.active_eventloop = self._inputhook = None
1086
1087 self._use_asyncio_inputhook = gui == "asyncio"
1088
1089 # Run !system commands directly, not through pipes, so terminal programs
1090 # work correctly.
1091 system = InteractiveShell.system_raw
1092
1093 def auto_rewrite_input(self, cmd):
1094 """Overridden from the parent class to use fancy rewriting prompt"""
1095 if not self.show_rewritten_input:
1096 return
1097
1098 tokens = self.prompts.rewrite_prompt_tokens()
1099 if self.pt_app:
1100 print_formatted_text(PygmentsTokens(tokens), end='',
1101 style=self.pt_app.app.style)
1102 print(cmd)
1103 else:
1104 prompt = ''.join(s for t, s in tokens)
1105 print(prompt, cmd, sep='')
1106
1107 _prompts_before = None
1108 def switch_doctest_mode(self, mode):
1109 """Switch prompts to classic for %doctest_mode"""
1110 if mode:
1111 self._prompts_before = self.prompts
1112 self.prompts = ClassicPrompts(self)
1113 elif self._prompts_before:
1114 self.prompts = self._prompts_before
1115 self._prompts_before = None
1116# self._update_layout()
1117
1118
1119InteractiveShellABC.register(TerminalInteractiveShell)
1120
1121if __name__ == '__main__':
1122 TerminalInteractiveShell.instance().interact()