| 1 |
"""IPython terminal interface using prompt_toolkit""" |
| 2 |
|
| 3 |
import os |
| 4 |
import sys |
| 5 |
import inspect |
| 6 |
from warnings import warn |
| 7 |
from typing import Union as UnionType, Optional |
| 8 |
|
| 9 |
from IPython.core.async_helpers import get_asyncio_loop |
| 10 |
from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC |
| 11 |
from IPython.utils.py3compat import input |
| 12 |
from IPython.utils.PyColorize import theme_table |
| 13 |
from IPython.utils.terminal import toggle_set_term_title, set_term_title, restore_term_title |
| 14 |
from IPython.utils.process import abbrev_cwd |
| 15 |
from 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 |
) |
| 32 |
from traitlets.utils.importstring import import_item |
| 33 |
|
| 34 |
|
| 35 |
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory |
| 36 |
from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode |
| 37 |
from prompt_toolkit.filters import HasFocus, Condition, IsDone |
| 38 |
from prompt_toolkit.formatted_text import PygmentsTokens |
| 39 |
from prompt_toolkit.history import History |
| 40 |
from prompt_toolkit.layout.processors import ConditionalProcessor, HighlightMatchingBracketProcessor |
| 41 |
from prompt_toolkit.output import ColorDepth |
| 42 |
from prompt_toolkit.patch_stdout import patch_stdout |
| 43 |
from prompt_toolkit.shortcuts import PromptSession, CompleteStyle, print_formatted_text |
| 44 |
from prompt_toolkit.styles import DynamicStyle, merge_styles |
| 45 |
from prompt_toolkit.styles.pygments import style_from_pygments_cls, style_from_pygments_dict |
| 46 |
from pygments.styles import get_style_by_name |
| 47 |
from pygments.style import Style |
| 48 |
|
| 49 |
from .debugger import TerminalPdb, Pdb |
| 50 |
from .magics import TerminalMagics |
| 51 |
from .pt_inputhooks import get_inputhook_name_and_func |
| 52 |
from .prompts import Prompts, ClassicPrompts, RichPromptDisplayHook |
| 53 |
from .ptutils import IPythonPTCompleter, IPythonPTLexer |
| 54 |
from .shortcuts import ( |
| 55 |
KEY_BINDINGS, |
| 56 |
UNASSIGNED_ALLOWED_COMMANDS, |
| 57 |
create_ipython_shortcuts, |
| 58 |
create_identifier, |
| 59 |
RuntimeBinding, |
| 60 |
add_binding, |
| 61 |
) |
| 62 |
from .shortcuts.filters import KEYBINDING_FILTERS, filter_from_string |
| 63 |
from .shortcuts.auto_suggest import ( |
| 64 |
NavigableAutoSuggestFromHistory, |
| 65 |
AppendAutoSuggestionInAnyLine, |
| 66 |
) |
| 67 |
|
| 68 |
|
| 69 |
class _NoStyle(Style): |
| 70 |
pass |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
def _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 |
|
| 101 |
def 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 |
| 120 |
for _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 |
| 130 |
else: |
| 131 |
_is_tty = True |
| 132 |
|
| 133 |
|
| 134 |
_use_simple_prompt = ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or (not _is_tty) |
| 135 |
|
| 136 |
def 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 |
|
| 150 |
def 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 |
|
| 166 |
class 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 |
|
| 203 |
class 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 if not explicitly configured |
| 769 |
config = self.display_formatter._trait_values["config"] |
| 770 |
if not ( |
| 771 |
"DisplayFormatter" in config |
| 772 |
and "active_types" in config["DisplayFormatter"] |
| 773 |
): |
| 774 |
self.display_formatter.active_types = ["text/plain"] |
| 775 |
|
| 776 |
def init_prompt_toolkit_cli(self): |
| 777 |
if self.simple_prompt: |
| 778 |
# Fall back to plain non-interactive output for tests. |
| 779 |
# This is very limited. |
| 780 |
def prompt(): |
| 781 |
prompt_text = "".join(x[1] for x in self.prompts.in_prompt_tokens()) |
| 782 |
lines = [input(prompt_text)] |
| 783 |
prompt_continuation = "".join( |
| 784 |
x[1] for x in self.prompts.continuation_prompt_tokens() |
| 785 |
) |
| 786 |
while self.check_complete("\n".join(lines))[0] == "incomplete": |
| 787 |
lines.append(input(prompt_continuation)) |
| 788 |
return "\n".join(lines) |
| 789 |
|
| 790 |
self.prompt_for_code = prompt |
| 791 |
return |
| 792 |
|
| 793 |
# Set up keyboard shortcuts |
| 794 |
key_bindings = self._merge_shortcuts(user_shortcuts=self.shortcuts) |
| 795 |
|
| 796 |
# Pre-populate history from IPython's history database |
| 797 |
history = PtkHistoryAdapter(self) |
| 798 |
|
| 799 |
self.refresh_style() |
| 800 |
ptk_s = DynamicStyle(lambda: self._style) |
| 801 |
|
| 802 |
editing_mode = getattr(EditingMode, self.editing_mode.upper()) |
| 803 |
|
| 804 |
self._use_asyncio_inputhook = False |
| 805 |
self.pt_app = PromptSession( |
| 806 |
auto_suggest=self.auto_suggest, |
| 807 |
editing_mode=editing_mode, |
| 808 |
key_bindings=key_bindings, |
| 809 |
history=history, |
| 810 |
completer=IPythonPTCompleter(shell=self), |
| 811 |
enable_history_search=self.enable_history_search, |
| 812 |
style=ptk_s, |
| 813 |
include_default_pygments_style=False, |
| 814 |
mouse_support=self.mouse_support, |
| 815 |
enable_open_in_editor=self.extra_open_editor_shortcuts, |
| 816 |
color_depth=self.color_depth, |
| 817 |
tempfile_suffix=".py", |
| 818 |
**self._extra_prompt_options(), |
| 819 |
) |
| 820 |
if isinstance(self.auto_suggest, NavigableAutoSuggestFromHistory): |
| 821 |
self.auto_suggest.connect(self.pt_app) |
| 822 |
|
| 823 |
def _make_style_from_name_or_cls(self, name_or_cls): |
| 824 |
""" |
| 825 |
Small wrapper that make an IPython compatible style from a style name |
| 826 |
|
| 827 |
We need that to add style for prompt ... etc. |
| 828 |
""" |
| 829 |
assert name_or_cls == "legacy" |
| 830 |
legacy = self.colors.lower() |
| 831 |
|
| 832 |
theme = theme_table.get(legacy, None) |
| 833 |
assert theme is not None, legacy |
| 834 |
|
| 835 |
if legacy == "nocolor": |
| 836 |
style_overrides = {} |
| 837 |
style_cls = _NoStyle |
| 838 |
else: |
| 839 |
style_overrides = {**theme.extra_style, **self.highlighting_style_overrides} |
| 840 |
if theme.base is not None: |
| 841 |
style_cls = get_style_by_name(theme.base) |
| 842 |
else: |
| 843 |
style_cls = _NoStyle |
| 844 |
|
| 845 |
style = merge_styles( |
| 846 |
[ |
| 847 |
style_from_pygments_cls(style_cls), |
| 848 |
style_from_pygments_dict(style_overrides), |
| 849 |
] |
| 850 |
) |
| 851 |
|
| 852 |
return style |
| 853 |
|
| 854 |
@property |
| 855 |
def pt_complete_style(self): |
| 856 |
return { |
| 857 |
'multicolumn': CompleteStyle.MULTI_COLUMN, |
| 858 |
'column': CompleteStyle.COLUMN, |
| 859 |
'readlinelike': CompleteStyle.READLINE_LIKE, |
| 860 |
}[self.display_completions] |
| 861 |
|
| 862 |
@property |
| 863 |
def color_depth(self): |
| 864 |
return (ColorDepth.TRUE_COLOR if self.true_color else None) |
| 865 |
|
| 866 |
def _ptk_prompt_cont(self, width: int, line_number: int, wrap_count: int): |
| 867 |
return PygmentsTokens( |
| 868 |
_backward_compat_continuation_prompt_tokens( |
| 869 |
self.prompts.continuation_prompt_tokens, |
| 870 |
width, |
| 871 |
lineno=line_number, |
| 872 |
wrap_count=wrap_count, |
| 873 |
) |
| 874 |
) |
| 875 |
|
| 876 |
def _extra_prompt_options(self): |
| 877 |
""" |
| 878 |
Return the current layout option for the current Terminal InteractiveShell |
| 879 |
""" |
| 880 |
def get_message(): |
| 881 |
return PygmentsTokens(self.prompts.in_prompt_tokens()) |
| 882 |
|
| 883 |
if self.editing_mode == "emacs" and self.prompt_line_number_format == "": |
| 884 |
# with emacs mode the prompt is (usually) static, so we call only |
| 885 |
# the function once. With VI mode it can toggle between [ins] and |
| 886 |
# [nor] so we can't precompute. |
| 887 |
# here I'm going to favor the default keybinding which almost |
| 888 |
# everybody uses to decrease CPU usage. |
| 889 |
# if we have issues with users with custom Prompts we can see how to |
| 890 |
# work around this. |
| 891 |
get_message = get_message() |
| 892 |
|
| 893 |
options = { |
| 894 |
"complete_in_thread": False, |
| 895 |
"lexer": IPythonPTLexer(), |
| 896 |
"reserve_space_for_menu": self.space_for_menu, |
| 897 |
"message": get_message, |
| 898 |
"prompt_continuation": self._ptk_prompt_cont, |
| 899 |
"multiline": True, |
| 900 |
"complete_style": self.pt_complete_style, |
| 901 |
"input_processors": [ |
| 902 |
# Highlight matching brackets, but only when this setting is |
| 903 |
# enabled, and only when the DEFAULT_BUFFER has the focus. |
| 904 |
ConditionalProcessor( |
| 905 |
processor=HighlightMatchingBracketProcessor(chars="[](){}"), |
| 906 |
filter=HasFocus(DEFAULT_BUFFER) |
| 907 |
& ~IsDone() |
| 908 |
& Condition(lambda: self.highlight_matching_brackets), |
| 909 |
), |
| 910 |
# Show auto-suggestion in lines other than the last line. |
| 911 |
ConditionalProcessor( |
| 912 |
processor=AppendAutoSuggestionInAnyLine(), |
| 913 |
filter=HasFocus(DEFAULT_BUFFER) |
| 914 |
& ~IsDone() |
| 915 |
& Condition( |
| 916 |
lambda: isinstance( |
| 917 |
self.auto_suggest, |
| 918 |
NavigableAutoSuggestFromHistory, |
| 919 |
) |
| 920 |
), |
| 921 |
), |
| 922 |
], |
| 923 |
} |
| 924 |
|
| 925 |
return options |
| 926 |
|
| 927 |
def prompt_for_code(self): |
| 928 |
if self.rl_next_input: |
| 929 |
default = self.rl_next_input |
| 930 |
self.rl_next_input = None |
| 931 |
else: |
| 932 |
default = '' |
| 933 |
|
| 934 |
# In order to make sure that asyncio code written in the |
| 935 |
# interactive shell doesn't interfere with the prompt, we run the |
| 936 |
# prompt in a different event loop. |
| 937 |
# If we don't do this, people could spawn coroutine with a |
| 938 |
# while/true inside which will freeze the prompt. |
| 939 |
|
| 940 |
with patch_stdout(raw=True): |
| 941 |
if self._use_asyncio_inputhook: |
| 942 |
# When we integrate the asyncio event loop, run the UI in the |
| 943 |
# same event loop as the rest of the code. don't use an actual |
| 944 |
# input hook. (Asyncio is not made for nesting event loops.) |
| 945 |
asyncio_loop = get_asyncio_loop() |
| 946 |
text = asyncio_loop.run_until_complete( |
| 947 |
self.pt_app.prompt_async( |
| 948 |
default=default, **self._extra_prompt_options() |
| 949 |
) |
| 950 |
) |
| 951 |
else: |
| 952 |
text = self.pt_app.prompt( |
| 953 |
default=default, |
| 954 |
inputhook=self._inputhook, |
| 955 |
**self._extra_prompt_options(), |
| 956 |
) |
| 957 |
|
| 958 |
return text |
| 959 |
|
| 960 |
def init_io(self): |
| 961 |
if sys.platform not in {'win32', 'cli'}: |
| 962 |
return |
| 963 |
|
| 964 |
import colorama |
| 965 |
colorama.init() |
| 966 |
|
| 967 |
def init_magics(self): |
| 968 |
super(TerminalInteractiveShell, self).init_magics() |
| 969 |
self.register_magics(TerminalMagics) |
| 970 |
|
| 971 |
def init_alias(self): |
| 972 |
# The parent class defines aliases that can be safely used with any |
| 973 |
# frontend. |
| 974 |
super(TerminalInteractiveShell, self).init_alias() |
| 975 |
|
| 976 |
# Now define aliases that only make sense on the terminal, because they |
| 977 |
# need direct access to the console in a way that we can't emulate in |
| 978 |
# GUI or web frontend |
| 979 |
if os.name == 'posix': |
| 980 |
for cmd in ('clear', 'more', 'less', 'man'): |
| 981 |
self.alias_manager.soft_define_alias(cmd, cmd) |
| 982 |
|
| 983 |
def __init__(self, *args, **kwargs) -> None: |
| 984 |
super().__init__(*args, **kwargs) |
| 985 |
self._set_autosuggestions(self.autosuggestions_provider) |
| 986 |
self.init_prompt_toolkit_cli() |
| 987 |
self.init_term_title() |
| 988 |
self.keep_running = True |
| 989 |
self._set_formatter(self.autoformatter) |
| 990 |
|
| 991 |
def ask_exit(self): |
| 992 |
self.keep_running = False |
| 993 |
|
| 994 |
rl_next_input = None |
| 995 |
|
| 996 |
def interact(self): |
| 997 |
self.keep_running = True |
| 998 |
while self.keep_running: |
| 999 |
print(self.separate_in, end='') |
| 1000 |
|
| 1001 |
try: |
| 1002 |
code = self.prompt_for_code() |
| 1003 |
except EOFError: |
| 1004 |
if (not self.confirm_exit) \ |
| 1005 |
or self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'): |
| 1006 |
self.ask_exit() |
| 1007 |
|
| 1008 |
else: |
| 1009 |
if code: |
| 1010 |
self.run_cell(code, store_history=True) |
| 1011 |
|
| 1012 |
def mainloop(self): |
| 1013 |
# An extra layer of protection in case someone mashing Ctrl-C breaks |
| 1014 |
# out of our internal code. |
| 1015 |
while True: |
| 1016 |
try: |
| 1017 |
self.interact() |
| 1018 |
break |
| 1019 |
except KeyboardInterrupt as e: |
| 1020 |
print("\n%s escaped interact()\n" % type(e).__name__) |
| 1021 |
finally: |
| 1022 |
# An interrupt during the eventloop will mess up the |
| 1023 |
# internal state of the prompt_toolkit library. |
| 1024 |
# Stopping the eventloop fixes this, see |
| 1025 |
# https://github.com/ipython/ipython/pull/9867 |
| 1026 |
if hasattr(self, '_eventloop'): |
| 1027 |
self._eventloop.stop() |
| 1028 |
|
| 1029 |
self.restore_term_title() |
| 1030 |
|
| 1031 |
# try to call some at-exit operation optimistically as some things can't |
| 1032 |
# be done during interpreter shutdown. this is technically inaccurate as |
| 1033 |
# this make mainlool not re-callable, but that should be a rare if not |
| 1034 |
# in existent use case. |
| 1035 |
|
| 1036 |
self._atexit_once() |
| 1037 |
|
| 1038 |
_inputhook = None |
| 1039 |
def inputhook(self, context): |
| 1040 |
warn( |
| 1041 |
"inputkook seem unused, and marked for deprecation/Removal as of IPython 9.0. " |
| 1042 |
"Please open an issue if you are using it.", |
| 1043 |
category=DeprecationWarning, |
| 1044 |
stacklevel=2, |
| 1045 |
) |
| 1046 |
if self._inputhook is not None: |
| 1047 |
self._inputhook(context) |
| 1048 |
|
| 1049 |
active_eventloop: Optional[str] = None |
| 1050 |
|
| 1051 |
def enable_gui(self, gui: Optional[str] = None) -> None: |
| 1052 |
if gui: |
| 1053 |
from ..core.pylabtools import _convert_gui_from_matplotlib |
| 1054 |
|
| 1055 |
gui = _convert_gui_from_matplotlib(gui) |
| 1056 |
|
| 1057 |
if self.simple_prompt is True and gui is not None: |
| 1058 |
print( |
| 1059 |
f'Cannot install event loop hook for "{gui}" when running with `--simple-prompt`.' |
| 1060 |
) |
| 1061 |
print( |
| 1062 |
"NOTE: Tk is supported natively; use Tk apps and Tk backends with `--simple-prompt`." |
| 1063 |
) |
| 1064 |
return |
| 1065 |
|
| 1066 |
if self._inputhook is None and gui is None: |
| 1067 |
print("No event loop hook running.") |
| 1068 |
return |
| 1069 |
|
| 1070 |
if self._inputhook is not None and gui is not None: |
| 1071 |
newev, newinhook = get_inputhook_name_and_func(gui) |
| 1072 |
if self._inputhook == newinhook: |
| 1073 |
# same inputhook, do nothing |
| 1074 |
self.log.info( |
| 1075 |
f"Shell is already running the {self.active_eventloop} eventloop. Doing nothing" |
| 1076 |
) |
| 1077 |
return |
| 1078 |
self.log.warning( |
| 1079 |
f"Shell is already running a different gui event loop for {self.active_eventloop}. " |
| 1080 |
"Call with no arguments to disable the current loop." |
| 1081 |
) |
| 1082 |
return |
| 1083 |
if self._inputhook is not None and gui is None: |
| 1084 |
self.active_eventloop = self._inputhook = None |
| 1085 |
|
| 1086 |
if gui and (gui not in {None, "webagg"}): |
| 1087 |
# This hook runs with each cycle of the `prompt_toolkit`'s event loop. |
| 1088 |
self.active_eventloop, self._inputhook = get_inputhook_name_and_func(gui) |
| 1089 |
else: |
| 1090 |
self.active_eventloop = self._inputhook = None |
| 1091 |
|
| 1092 |
self._use_asyncio_inputhook = gui == "asyncio" |
| 1093 |
|
| 1094 |
# Run !system commands directly, not through pipes, so terminal programs |
| 1095 |
# work correctly. |
| 1096 |
system = InteractiveShell.system_raw |
| 1097 |
|
| 1098 |
def auto_rewrite_input(self, cmd): |
| 1099 |
"""Overridden from the parent class to use fancy rewriting prompt""" |
| 1100 |
if not self.show_rewritten_input: |
| 1101 |
return |
| 1102 |
|
| 1103 |
tokens = self.prompts.rewrite_prompt_tokens() |
| 1104 |
if self.pt_app: |
| 1105 |
print_formatted_text(PygmentsTokens(tokens), end='', |
| 1106 |
style=self.pt_app.app.style) |
| 1107 |
print(cmd) |
| 1108 |
else: |
| 1109 |
prompt = ''.join(s for t, s in tokens) |
| 1110 |
print(prompt, cmd, sep='') |
| 1111 |
|
| 1112 |
_prompts_before = None |
| 1113 |
def switch_doctest_mode(self, mode): |
| 1114 |
"""Switch prompts to classic for %doctest_mode""" |
| 1115 |
if mode: |
| 1116 |
self._prompts_before = self.prompts |
| 1117 |
self.prompts = ClassicPrompts(self) |
| 1118 |
elif self._prompts_before: |
| 1119 |
self.prompts = self._prompts_before |
| 1120 |
self._prompts_before = None |
| 1121 |
# self._update_layout() |
| 1122 |
|
| 1123 |
|
| 1124 |
InteractiveShellABC.register(TerminalInteractiveShell) |
| 1125 |
|
| 1126 |
if __name__ == '__main__': |
| 1127 |
TerminalInteractiveShell.instance().interact() |