Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timeout option for every input methods #163

Open
ajay15283 opened this issue Apr 17, 2022 · 1 comment
Open

timeout option for every input methods #163

ajay15283 opened this issue Apr 17, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@ajay15283
Copy link

Hi,

inquirer doesn't seems to have timeout option for text, list and checkbox. If a timeout option is added it can help to exit the program if a user didn't provide the input on mentioned time

Thanks
Ajay Ramachandar

@Cube707 Cube707 added the enhancement New feature or request label Dec 18, 2022
@technotaff-nbs
Copy link

technotaff-nbs commented Apr 11, 2023

Hi Ajay,

You could always create your own wrapper around inquirer something like this:

import signal
import inquirer

def prompt_for(prompt: str, choice_options: list, default: str = None, timeout: int = None) -> str:
    """
    Prompt for choices from a user
    """

    if timeout:
        prompt = f'{prompt} ({timeout} seconds to decide)'
    if default:
        questions = [
            inquirer.List(
                "answer",
                message=prompt,
                choices=choice_options,
                default=default
            ),
        ]
    else:
        questions = [
            inquirer.List(
                "answer",
                message=prompt,
                choices=choice_options,
            ),
        ]

    def alarm_handler(signum, frame):
        raise TimeoutError()

    if timeout:
        signal.signal(signal.SIGALRM, alarm_handler)
        signal.alarm(timeout)
    try:
        return inquirer.prompt(questions)
    except TimeoutError:
            return default
    finally:
        signal.alarm(0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants