async def unsafe_ask_async(self, patch_stdout: bool = False) -> Any: """Ask the question using asyncio and return user response. Does not catch keyboard interrupts. Args: patch_stdout: Ensure that the prompt renders correctly if other threads are printing to stdout. Returns: `Any`: The answer from the question. """ if not utils.ACTIVATED_ASYNC_MODE: await utils.activate_prompt_toolkit_async_mode() if patch_stdout: with prompt_toolkit.patch_stdout.patch_stdout(): r = self.application.run_async() else: r = self.application.run_async() if utils.is_prompt_toolkit_3(): return await r else: return await r.to_asyncio_future()
async def unsafe_ask_async(self, patch_stdout: bool = False) -> Any: """Ask the question using asyncio and return user response. Does not catch keyboard interrupts.""" if not utils.ACTIVATED_ASYNC_MODE: await utils.activate_prompt_toolkit_async_mode() if patch_stdout: with prompt_toolkit.patch_stdout.patch_stdout(): r = self.application.run_async() else: r = self.application.run_async() if utils.is_prompt_toolkit_3(): return await r else: return await r.to_asyncio_future()
def feed_cli_with_input(_type, message, texts, sleep_time=1, **kwargs): """ Create a Prompt, feed it with the given user input and return the CLI object. You an provide multiple texts, the feeder will async sleep for `sleep_time` This returns a (result, Application) tuple. """ if not isinstance(texts, list): texts = [texts] inp = create_pipe_input() try: prompter = prompt_by_name(_type) application = prompter(message, input=inp, output=DummyOutput(), **kwargs) if is_prompt_toolkit_3(): loop = asyncio.new_event_loop() future_result = loop.create_task(application.unsafe_ask_async()) for i, text in enumerate(texts): # noinspection PyUnresolvedReferences inp.send_text(text) if i != len(texts) - 1: loop.run_until_complete(asyncio.sleep(sleep_time)) result = loop.run_until_complete(future_result) else: for text in texts: inp.send_text(text) result = application.unsafe_ask() return result, application finally: inp.close()