Exemplo n.º 1
0
def ask_user(question: str, default: bool, new_line: bool = False) -> bool:
    """
    Asks the user a yes/no question.
    :param question:    The question to ask
    :param default:     The default answer, if user presses enter.
                        True for yes, False for no
    :param new_line:    If new_line before printing the question
    :return:            yes: True, no: False
    """

    yes = ["y"]
    no = ["n"]
    if default:
        yes.append("")
        choices = "Y/n"
    else:
        no.append("")
        choices = "N/y"

    while True:
        user_choice = str(
            input(
                aurman_question("{} {}: ".format(question, choices),
                                new_line=new_line,
                                to_print=False))).strip().lower()
        if user_choice in yes or user_choice in no:
            return user_choice in yes
        aurman_error("That was not a valid choice!")
Exemplo n.º 2
0
def ask_user(question: str, default: bool, new_line: bool = False) -> bool:
    """
    Asks the user a yes/no question.
    :param question:    The question to ask
    :param default:     The default answer, if user presses enter.
                        True for yes, False for no
    :param new_line:    If new_line before printing the question
    :return:            yes: True, no: False
    """

    yes = ["y"]
    no = ["n"]
    if default:
        yes.append("")
        choices = "Y/n"
    else:
        no.append("")
        choices = "N/y"

    while True:
        print(aurman_question("{} {}: ".format(question, choices),
                              new_line=new_line,
                              to_print=False),
              end='',
              flush=True)

        # see https://stackoverflow.com/a/36974338
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)

        try:
            tty.setcbreak(fd)
            answer = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)

        print(flush=True)
        user_choice = answer.strip().lower()
        if user_choice in yes or user_choice in no:
            return user_choice in yes
        aurman_error("That was not a valid choice!")