|
12 | 12 | import threading |
13 | 13 | import time |
14 | 14 | from collections.abc import Callable, Generator |
15 | | -from concurrent.futures import Future |
16 | | -from concurrent.futures import TimeoutError as FutureTimeoutError |
17 | 15 | from typing import TYPE_CHECKING, Any, TextIO, TypeVar, cast |
18 | 16 |
|
19 | 17 | from prompt_toolkit.application import Application, create_app_session, run_in_terminal |
20 | | -from prompt_toolkit.enums import EditingMode |
21 | 18 | from prompt_toolkit.filters import Condition, to_filter |
22 | 19 | from prompt_toolkit.input.typeahead import get_typeahead, store_typeahead |
23 | 20 | from prompt_toolkit.key_binding import KeyBindings |
|
27 | 24 | from prompt_toolkit.patch_stdout import StdoutProxy |
28 | 25 | from prompt_toolkit.utils import get_cwidth, suspend_to_background_supported |
29 | 26 |
|
30 | | -from .pager import Pager, output_fits |
31 | | - |
32 | 27 | if TYPE_CHECKING: |
33 | 28 | from .cmd2 import Cmd |
34 | 29 |
|
|
37 | 32 | _ESCAPE_RE = re.compile(r"\x1b(?:\[[0-?]*[ -/]*[@-~]|\][^\x07\x1b]*(?:\x07|\x1b\\)|[@-Z\\-_])") |
38 | 33 |
|
39 | 34 | _F = TypeVar("_F", bound=Callable[..., Any]) |
40 | | -_R = TypeVar("_R") |
41 | 35 |
|
42 | 36 |
|
43 | 37 | def suspend_process_group(suspend_group: bool = True) -> None: |
@@ -347,7 +341,6 @@ def suspend(event: KeyPressEvent) -> None: |
347 | 341 | event.app.suspend_to_background() |
348 | 342 |
|
349 | 343 | self._bindings = bindings |
350 | | - self._suspend_binding = suspend |
351 | 344 |
|
352 | 345 | @staticmethod |
353 | 346 | def _suspend_to_background(suspend_group: bool = True) -> None: |
@@ -405,7 +398,7 @@ def _resume(self) -> None: |
405 | 398 | # has not seen with other bindings. It caches the bindings it merges for each focused |
406 | 399 | # window, application bindings included, and does not notice this attribute being |
407 | 400 | # reassigned, so reusing a window with different bindings would keep the old ones. |
408 | | - # This layout, the main prompt's, and each pager's all have windows of their own. |
| 401 | + # This layout and the main prompt's have windows of their own. |
409 | 402 | for name, value in (("layout", self._layout), ("key_bindings", self._bindings), ("erase_when_done", True)): |
410 | 403 | stack.callback(setattr, self.app, name, getattr(self.app, name)) |
411 | 404 | setattr(self.app, name, value) |
@@ -560,103 +553,6 @@ def is_active(self) -> bool: |
560 | 553 | """Whether the display currently owns the terminal.""" |
561 | 554 | return self._proxy is not None and self.app.is_running |
562 | 555 |
|
563 | | - def _call_in_ui(self, func: Callable[[], _R]) -> _R: |
564 | | - """Change UI state on its event loop, propagating failures to the command.""" |
565 | | - result: Future[_R] = Future() |
566 | | - |
567 | | - def call() -> None: |
568 | | - try: |
569 | | - value = func() |
570 | | - except BaseException as exc: # noqa: BLE001 |
571 | | - result.set_exception(exc) |
572 | | - else: |
573 | | - result.set_result(value) |
574 | | - |
575 | | - if self.app.loop is None: |
576 | | - raise RuntimeError("Toolbar is not running") |
577 | | - self.app.loop.call_soon_threadsafe(call) |
578 | | - while True: |
579 | | - try: |
580 | | - value = result.result(timeout=0.1) |
581 | | - except FutureTimeoutError: |
582 | | - if result.done(): |
583 | | - # The callback finished while this poll was expiring, or raised a |
584 | | - # TimeoutError of its own -- indistinguishable here, because |
585 | | - # concurrent.futures.TimeoutError is TimeoutError on Python 3.11+. |
586 | | - # Ask the future for its outcome rather than re-raising this poll's |
587 | | - # timeout, which would report a failure for a call that succeeded. |
588 | | - return result.result() |
589 | | - self._check_running() |
590 | | - else: |
591 | | - return value |
592 | | - |
593 | | - def _check_running(self) -> None: |
594 | | - if self._thread is None or not self._thread.is_alive(): |
595 | | - if self._error is not None: |
596 | | - raise self._error |
597 | | - raise EOFError |
598 | | - |
599 | | - def page(self, text: str, *, chop: bool) -> None: |
600 | | - """Show a pager above the same toolbar without starting another input reader.""" |
601 | | - size = self.app.output.get_size() |
602 | | - # Measuring the toolbar can invoke its callback; keep that work on the |
603 | | - # UI thread along with rendering and layout changes. |
604 | | - toolbar_height = self._call_in_ui(lambda: self.toolbar.preferred_height(size.columns, size.rows).preferred) |
605 | | - if output_fits(text, size.columns, max(0, size.rows - toolbar_height), chop=chop): |
606 | | - self.cmd.stdout.write(text) |
607 | | - self.cmd.stdout.flush() |
608 | | - return |
609 | | - |
610 | | - pager = Pager(text, chop=chop) |
611 | | - pager.bindings.add( |
612 | | - "c-z", |
613 | | - filter=Condition(lambda: suspend_to_background_supported() and to_filter(self.cmd.main_session.enable_suspend)()), |
614 | | - )(self._suspend_binding) |
615 | | - layout = Layout(HSplit([pager.container, self.toolbar]), focused_element=pager.text) |
616 | | - previous = (self.app.layout, self.app.key_bindings, self.app.editing_mode, self.app.full_screen) |
617 | | - entered = False |
618 | | - |
619 | | - def enter() -> None: |
620 | | - nonlocal entered |
621 | | - entered = True |
622 | | - self.app.renderer.erase() |
623 | | - self.app.layout = layout |
624 | | - self.app.key_bindings = pager.bindings |
625 | | - self.app.editing_mode = EditingMode.EMACS |
626 | | - self.app.full_screen = self.app.renderer.full_screen = True |
627 | | - self.app.invalidate() |
628 | | - |
629 | | - def leave() -> None: |
630 | | - nonlocal entered |
631 | | - if not entered: |
632 | | - return |
633 | | - entered = False |
634 | | - self.app.renderer.erase() |
635 | | - self.app.layout, self.app.key_bindings, self.app.editing_mode, self.app.full_screen = previous |
636 | | - self.app.renderer.full_screen = self.app.full_screen |
637 | | - self.app.renderer.request_absolute_cursor_position() |
638 | | - self.app.invalidate() |
639 | | - |
640 | | - def close() -> None: |
641 | | - # Switch bindings before the next key is processed, preserving |
642 | | - # typeahead sent in the same terminal read as the pager's quit key. |
643 | | - leave() |
644 | | - pager.closed.set() |
645 | | - |
646 | | - pager.on_close = close |
647 | | - |
648 | | - try: |
649 | | - self._call_in_ui(enter) |
650 | | - while not pager.closed.wait(0.1): |
651 | | - self._check_running() |
652 | | - finally: |
653 | | - if self._thread is not None and self._thread.is_alive(): |
654 | | - self._call_in_ui(leave) |
655 | | - else: |
656 | | - # The application's shutdown already reset the renderer. |
657 | | - self.app.layout, self.app.key_bindings, self.app.editing_mode, self.app.full_screen = previous |
658 | | - self.app.renderer.full_screen = self.app.full_screen |
659 | | - |
660 | 556 | @contextlib.contextmanager |
661 | 557 | def suspend(self) -> Generator[None]: |
662 | 558 | """Temporarily restore ordinary terminal access, including nested suspensions.""" |
|
0 commit comments