|
3 | 3 | import asyncio |
4 | 4 | import os |
5 | 5 | import sys |
6 | | -import warnings |
7 | 6 | from warnings import warn |
8 | 7 |
|
9 | 8 | from IPython.core.async_helpers import get_asyncio_loop |
10 | 9 | from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC |
11 | | -from IPython.utils import io |
12 | 10 | from IPython.utils.py3compat import input |
13 | 11 | from IPython.utils.terminal import toggle_set_term_title, set_term_title, restore_term_title |
14 | 12 | from IPython.utils.process import abbrev_cwd |
|
32 | 30 | from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode |
33 | 31 | from prompt_toolkit.filters import (HasFocus, Condition, IsDone) |
34 | 32 | from prompt_toolkit.formatted_text import PygmentsTokens |
35 | | -from prompt_toolkit.history import InMemoryHistory |
| 33 | +from prompt_toolkit.history import History |
36 | 34 | from prompt_toolkit.layout.processors import ConditionalProcessor, HighlightMatchingBracketProcessor |
37 | 35 | from prompt_toolkit.output import ColorDepth |
38 | 36 | from prompt_toolkit.patch_stdout import patch_stdout |
@@ -132,6 +130,43 @@ def yapf_reformat_handler(text_before_cursor): |
132 | 130 | return text_before_cursor |
133 | 131 |
|
134 | 132 |
|
| 133 | +class PtkHistoryAdapter(History): |
| 134 | + """ |
| 135 | + Prompt toolkit has it's own way of handling history, Where it assumes it can |
| 136 | + Push/pull from history. |
| 137 | +
|
| 138 | + """ |
| 139 | + |
| 140 | + def __init__(self, shell): |
| 141 | + super().__init__() |
| 142 | + self.shell = shell |
| 143 | + self._refresh() |
| 144 | + |
| 145 | + def append_string(self, string): |
| 146 | + # we rely on sql for that. |
| 147 | + self._loaded = False |
| 148 | + self._refresh() |
| 149 | + |
| 150 | + def _refresh(self): |
| 151 | + if not self._loaded: |
| 152 | + self._loaded_strings = list(self.load_history_strings()) |
| 153 | + |
| 154 | + def load_history_strings(self): |
| 155 | + last_cell = "" |
| 156 | + res = [] |
| 157 | + for __, ___, cell in self.shell.history_manager.get_tail( |
| 158 | + self.shell.history_load_length, include_latest=True |
| 159 | + ): |
| 160 | + # Ignore blank lines and consecutive duplicates |
| 161 | + cell = cell.rstrip() |
| 162 | + if cell and (cell != last_cell): |
| 163 | + res.append(cell) |
| 164 | + last_cell = cell |
| 165 | + yield from res[::-1] |
| 166 | + |
| 167 | + def store_string(self, string: str) -> None: |
| 168 | + pass |
| 169 | + |
135 | 170 | class TerminalInteractiveShell(InteractiveShell): |
136 | 171 | mime_renderers = Dict().tag(config=True) |
137 | 172 |
|
@@ -397,16 +432,9 @@ def prompt(): |
397 | 432 | # Set up keyboard shortcuts |
398 | 433 | key_bindings = create_ipython_shortcuts(self) |
399 | 434 |
|
| 435 | + |
400 | 436 | # Pre-populate history from IPython's history database |
401 | | - history = InMemoryHistory() |
402 | | - last_cell = u"" |
403 | | - for __, ___, cell in self.history_manager.get_tail(self.history_load_length, |
404 | | - include_latest=True): |
405 | | - # Ignore blank lines and consecutive duplicates |
406 | | - cell = cell.rstrip() |
407 | | - if cell and (cell != last_cell): |
408 | | - history.append_string(cell) |
409 | | - last_cell = cell |
| 437 | + history = PtkHistoryAdapter(self) |
410 | 438 |
|
411 | 439 | self._style = self._make_style_from_name_or_cls(self.highlighting_style) |
412 | 440 | self.style = DynamicStyle(lambda: self._style) |
@@ -586,7 +614,6 @@ def prompt_for_code(self): |
586 | 614 | def enable_win_unicode_console(self): |
587 | 615 | # Since IPython 7.10 doesn't support python < 3.6 and PEP 528, Python uses the unicode APIs for the Windows |
588 | 616 | # console by default, so WUC shouldn't be needed. |
589 | | - from warnings import warn |
590 | 617 | warn("`enable_win_unicode_console` is deprecated since IPython 7.10, does not do anything and will be removed in the future", |
591 | 618 | DeprecationWarning, |
592 | 619 | stacklevel=2) |
|
0 commit comments