|
87 | 87 | from prompt_toolkit.output import DummyOutput, Output, create_output |
88 | 88 | from prompt_toolkit.patch_stdout import patch_stdout |
89 | 89 | from prompt_toolkit.shortcuts import CompleteStyle, PromptSession, choice |
| 90 | +from prompt_toolkit.shortcuts.choice_input import ChoiceInput |
90 | 91 | from prompt_toolkit.styles import DynamicStyle |
91 | 92 | from rich.console import ( |
92 | 93 | Group, |
@@ -2201,13 +2202,41 @@ def _require_terminal_ownership(self) -> None: |
2201 | 2202 | return |
2202 | 2203 | raise RuntimeError("the bottom toolbar's display has not released the terminal") |
2203 | 2204 |
|
| 2205 | + def _await_terminal_ownership(self) -> bool: |
| 2206 | + """Wait for a display that would not stop to release the terminal before the main prompt. |
| 2207 | +
|
| 2208 | + Prompting while its thread is still inside the application would put two readers on one |
| 2209 | + terminal, so the command loop waits rather than failing. The thread usually finishes once |
| 2210 | + its toolbar callback returns. If it never does, Ctrl-C gives up on it and ends the loop. |
| 2211 | +
|
| 2212 | + :return: whether the terminal is ours to prompt on |
| 2213 | + """ |
| 2214 | + display = self._display_holding_terminal |
| 2215 | + if display is None: |
| 2216 | + return True |
| 2217 | + try: |
| 2218 | + # The display left the terminal in raw mode, where Ctrl-C is only a key, and the binding |
| 2219 | + # that would signal it runs on the event loop the stuck callback is blocking. Cook the |
| 2220 | + # terminal so its driver sends SIGINT again. This is not undone: a display that finishes |
| 2221 | + # restores the modes it found, and one given up on should leave the terminal cooked. |
| 2222 | + # From here on Ctrl-C can arrive at any point, including while the notice is printing. |
| 2223 | + self.main_session.app.input.cooked_mode().__enter__() |
| 2224 | + self.perror("Waiting for the bottom toolbar to release the terminal. Press Ctrl-C to quit.") |
| 2225 | + while display.thread_is_alive: |
| 2226 | + time.sleep(0.05) |
| 2227 | + except KeyboardInterrupt: |
| 2228 | + return False |
| 2229 | + self._require_terminal_ownership() |
| 2230 | + return True |
| 2231 | + |
2204 | 2232 | @contextlib.contextmanager |
2205 | 2233 | def suspend_bottom_toolbar(self) -> Generator[None]: |
2206 | 2234 | """Temporarily hide the command toolbar and give exclusive access to the terminal. |
2207 | 2235 |
|
2208 | 2236 | Use this context manager around application-specific calls to ``input()``, other |
2209 | 2237 | terminal UIs, or subprocesses that inherit the terminal. cmd2 automatically suspends |
2210 | | - its toolbar for its own input prompts, external pagers, and shell commands. |
| 2238 | + its toolbar for external pagers and shell commands. Managed input prompts borrow |
| 2239 | + the reservation without releasing the rows. |
2211 | 2240 |
|
2212 | 2241 | In reserved mode this also gives the reserved rows back, because a program that |
2213 | 2242 | inherits the terminal knows nothing about a scroll region and would find its output |
@@ -2308,6 +2337,11 @@ def _command_toolbar_context(self) -> Generator[None]: |
2308 | 2337 | with self.sigint_protection: |
2309 | 2338 | try: |
2310 | 2339 | toolbar.stop() |
| 2340 | + except command_toolbar._DisplayStillRunningError as exc: |
| 2341 | + # The display has already disabled itself and kept hold of the terminal, |
| 2342 | + # which the next prompt waits for. Like a display that cannot start, it |
| 2343 | + # must not escape cmdloop() over a toolbar callback that will not return. |
| 2344 | + self.perror(f"Disabling the bottom toolbar during commands: {exc}") |
2311 | 2345 | finally: |
2312 | 2346 | # Always forget a toolbar that has been torn down. Keeping a failed |
2313 | 2347 | # one would disable the toolbar for the rest of the session. |
@@ -3896,21 +3930,23 @@ def _read_raw_input( |
3896 | 3930 | The command display is stopped either way, but only some prompts give the terminal |
3897 | 3931 | away with it. The main prompt is the one the reservation exists for: it renders |
3898 | 3932 | through the reserved output, and the toolbar has to still be there while the user is |
3899 | | - typing -- that is what "stable across ordinary commands" means. Any other session is |
3900 | | - an application prompt cmd2 has not bound to the reservation, so it gets the terminal |
3901 | | - to itself, rows included. |
| 3933 | + typing. Managed nested prompts on the same terminal borrow that reservation with |
| 3934 | + their own bridge after the command input reader stops. Other terminals and prompts |
| 3935 | + inside an external handoff retain the exclusive-terminal path. |
3902 | 3936 |
|
3903 | 3937 | :param prompt: the prompt text or a callable that returns the prompt. |
3904 | 3938 | :param session: the PromptSession instance to use for reading. |
3905 | 3939 | :param prompt_kwargs: additional arguments passed directly to session.prompt(). |
3906 | 3940 | :return: the stripped input string. |
3907 | 3941 | :raises EOFError: if the input stream is closed or the user signals EOF (e.g., Ctrl+D) |
3908 | 3942 | """ |
3909 | | - owns_the_reservation = session is self.main_session |
| 3943 | + reserved = self._reserved_toolbar |
| 3944 | + owns_the_reservation = session is self.main_session or (reserved is not None and reserved.can_manage(session)) |
3910 | 3945 | with self._quiesce_bottom_toolbar() if owns_the_reservation else self.suspend_bottom_toolbar(): |
3911 | | - reserved = self._reserved_toolbar |
3912 | 3946 | if owns_the_reservation and reserved is not None and reserved.bridge is not None: |
3913 | 3947 | reserved.bridge.finish_command_output() |
| 3948 | + with reserved.prompt_session(session): |
| 3949 | + return self._read_raw_input_now(prompt, session, **prompt_kwargs) |
3914 | 3950 | return self._read_raw_input_now(prompt, session, **prompt_kwargs) |
3915 | 3951 |
|
3916 | 3952 | def _read_raw_input_now( |
@@ -4043,7 +4079,7 @@ def read_input( |
4043 | 4079 |
|
4044 | 4080 | temp_session: PromptSession[str] = PromptSession( |
4045 | 4081 | auto_suggest=self.main_session.auto_suggest, |
4046 | | - bottom_toolbar=self.get_bottom_toolbar if self.main_session.bottom_toolbar is not None else None, |
| 4082 | + bottom_toolbar=self.main_session.bottom_toolbar, |
4047 | 4083 | color_depth=self.main_session.color_depth, |
4048 | 4084 | complete_style=self.main_session.complete_style, |
4049 | 4085 | complete_in_thread=self.main_session.complete_in_thread, |
@@ -4072,7 +4108,7 @@ def read_secret( |
4072 | 4108 | :raises Exception: any other exceptions raised by prompt() |
4073 | 4109 | """ |
4074 | 4110 | temp_session: PromptSession[str] = PromptSession( |
4075 | | - bottom_toolbar=self.get_bottom_toolbar if self.main_session.bottom_toolbar is not None else None, |
| 4111 | + bottom_toolbar=self.main_session.bottom_toolbar, |
4076 | 4112 | color_depth=self.main_session.color_depth, |
4077 | 4113 | enable_suspend=self.main_session.enable_suspend, |
4078 | 4114 | input=self.main_session.input, |
@@ -4207,6 +4243,9 @@ def _cmdloop(self) -> None: |
4207 | 4243 | self._startup_commands.clear() |
4208 | 4244 |
|
4209 | 4245 | while not stop: |
| 4246 | + if not self._await_terminal_ownership(): |
| 4247 | + break |
| 4248 | + |
4210 | 4249 | # Get commands from user |
4211 | 4250 | try: |
4212 | 4251 | line = self._read_command_line(self.prompt) |
@@ -5048,7 +5087,7 @@ def do_quit(self, _: argparse.Namespace) -> bool | None: |
5048 | 5087 | self.last_result = True |
5049 | 5088 | return True |
5050 | 5089 |
|
5051 | | - @command_toolbar.suspend_toolbar |
| 5090 | + @command_toolbar.quiesce_toolbar |
5052 | 5091 | def select(self, opts: str | Iterable[str] | Iterable[tuple[Any, str | None]], prompt: str = "Your choice? ") -> Any: |
5053 | 5092 | """Present a menu to the user. |
5054 | 5093 |
|
@@ -5085,7 +5124,22 @@ def select(self, opts: str | Iterable[str] | Iterable[tuple[Any, str | None]], p |
5085 | 5124 | with create_app_session(input=self.main_session.input, output=self.main_session.output) as app_session: |
5086 | 5125 | self._select_app_session = app_session |
5087 | 5126 | try: |
5088 | | - result = choice(message=prompt, options=fulloptions) |
| 5127 | + reserved = self._reserved_toolbar |
| 5128 | + if reserved is not None and reserved.can_manage(self.main_session): |
| 5129 | + if reserved.bridge is not None: |
| 5130 | + reserved.bridge.finish_command_output() |
| 5131 | + # ChoiceInput.prompt() constructs and runs in one step. Binding |
| 5132 | + # its application first gives the initial frame reserved geometry. |
| 5133 | + selection = ChoiceInput( |
| 5134 | + message=prompt, |
| 5135 | + options=fulloptions, |
| 5136 | + style=self.main_session.style, |
| 5137 | + enable_suspend=self.main_session.enable_suspend, |
| 5138 | + )._create_application() |
| 5139 | + with reserved.prompt_application(selection): |
| 5140 | + result = selection.run() |
| 5141 | + else: |
| 5142 | + result = choice(message=prompt, options=fulloptions) |
5089 | 5143 | finally: |
5090 | 5144 | self._select_app_session = None |
5091 | 5145 | self._flush_pending_window_title_quietly() |
|
0 commit comments