Skip to content

Commit

Permalink
feat: added async support for alternate syntax #30
Browse files Browse the repository at this point in the history
  • Loading branch information
kazhala committed Dec 9, 2021
1 parent 6eab367 commit 75070c3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
4 changes: 4 additions & 0 deletions InquirerPy/base/complex.py
Expand Up @@ -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.
Expand Down
27 changes: 26 additions & 1 deletion InquirerPy/base/simple.py
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down
3 changes: 3 additions & 0 deletions InquirerPy/prompts/confirm.py
Expand Up @@ -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()
3 changes: 3 additions & 0 deletions InquirerPy/prompts/input.py
Expand Up @@ -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)
13 changes: 13 additions & 0 deletions 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())

0 comments on commit 75070c3

Please sign in to comment.