diff --git a/InquirerPy/base/complex.py b/InquirerPy/base/complex.py index a636053..03175f9 100644 --- a/InquirerPy/base/complex.py +++ b/InquirerPy/base/complex.py @@ -204,6 +204,10 @@ def _run(self) -> Any: """Run the application.""" return self.application.run() + async def _run_async(self) -> None: + """Run the application asynchronously.""" + return await self.application.run_async() + @property def application(self) -> Application: """Get the application. diff --git a/InquirerPy/base/simple.py b/InquirerPy/base/simple.py index 42e30a8..35cbae1 100644 --- a/InquirerPy/base/simple.py +++ b/InquirerPy/base/simple.py @@ -301,7 +301,16 @@ def _get_prompt_message( def _run(self) -> Any: """Abstractmethod to enforce a run function is implemented. - All prompt instance require a `_run` call to initialise and run an instance of + All prompt instance requires a `_run` call to initialise and run an instance of + `PromptSession` or `Application`. + """ + pass + + @abstractmethod + async def _run_async(self) -> Any: + """Abstractmethod to enforce a run function is implemented. + + All prompt instance requires a `_run_async` call to initialise and run an instance of `PromptSession` or `Application`. """ pass @@ -329,6 +338,22 @@ def execute(self, raise_keyboard_interrupt: bool = None) -> Any: return result return self._filter(result) + async def execute_async(self) -> None: + """Run the prompt asynchronously and get the result. + + Returns: + Value of the user answer. Types varies depending on the prompt. + + Raises: + KeyboardInterrupt: When `ctrl-c` is pressed and `raise_keyboard_interrupt` is True. + """ + result = await self._run_async() + if result == INQUIRERPY_KEYBOARD_INTERRUPT: + raise KeyboardInterrupt + if not self._filter: + return result + return self._filter(result) + @property def instruction(self) -> str: """str: Instruction to display next to question.""" diff --git a/InquirerPy/prompts/confirm.py b/InquirerPy/prompts/confirm.py index a5702fa..3507070 100644 --- a/InquirerPy/prompts/confirm.py +++ b/InquirerPy/prompts/confirm.py @@ -190,3 +190,6 @@ def _get_prompt_message(self) -> List[Tuple[str, str]]: def _run(self) -> bool: return self._session.prompt() + + async def _run_async(self) -> Any: + return await self._session.prompt_async() diff --git a/InquirerPy/prompts/input.py b/InquirerPy/prompts/input.py index 1e3dcf4..c823704 100644 --- a/InquirerPy/prompts/input.py +++ b/InquirerPy/prompts/input.py @@ -247,3 +247,6 @@ def _get_prompt_message( def _run(self) -> str: return self._session.prompt(default=self._default) + + async def _run_async(self) -> Any: + return await self._session.prompt_async(default=self._default) diff --git a/examples/async.py b/examples/async.py new file mode 100644 index 0000000..1cf6da2 --- /dev/null +++ b/examples/async.py @@ -0,0 +1,13 @@ +import asyncio + +from InquirerPy import inquirer + + +async def main(): + name = await inquirer.text(message="Name:").execute_async() + number = await inquirer.number(message="Number:").execute_async() + confirm = await inquirer.confirm(message="Confirm?").execute_async() + + +if __name__ == "__main__": + asyncio.run(main())