Example #1
0
File: input.py Project: xprog12/tfc
def yes(
    prompt: str,  # Question to be asked
    abort: Optional[bool] = None,  # Determines the return value of ^C and ^D
    head: int = 0,  # Number of new lines to print before prompt
    tail: int = 0  # Number of new lines to print after prompt
) -> bool:  # True/False depending on input
    """Prompt the user a question that is answered with yes/no."""
    print_spacing(head)

    prompt = f"{prompt} (y/n): "
    input_space = len(' yes ')

    upper_line = '┌' + (len(prompt) + input_space) * '─' + '┐'
    title_line = '│' + prompt + input_space * ' ' + '│'
    lower_line = '└' + (len(prompt) + input_space) * '─' + '┘'

    terminal_w = get_terminal_width()
    upper_line = upper_line.center(terminal_w)
    title_line = title_line.center(terminal_w)
    lower_line = lower_line.center(terminal_w)

    indent = title_line.find('│')

    terminal_width_check(len(upper_line))

    print(upper_line)
    while True:
        print(title_line)
        print(lower_line)
        print(3 * CURSOR_UP_ONE_LINE)

        try:
            user_input = input(indent * ' ' + f'│ {prompt}')
        except (EOFError, KeyboardInterrupt):
            if abort is None:
                raise
            print('')
            user_input = 'y' if abort else 'n'

        print_on_previous_line()

        if user_input == '':
            continue

        if user_input.lower() in ['y', 'yes']:
            print(indent * ' ' + f'│ {prompt}Yes │\n')
            print_spacing(tail)
            return True

        if user_input.lower() in ['n', 'no']:
            print(indent * ' ' + f'│ {prompt}No  │\n')
            print_spacing(tail)
            return False
Example #2
0
def box_input(message:        str,                          # Input prompt message
              default:        str                 = '',     # Default return value
              head:           int                 = 0,      # Number of new lines to print before the input
              tail:           int                 = 1,      # Number of new lines to print after input
              expected_len:   int                 = 0,      # Expected length of the input
              key_type:       str                 = '',     # When specified, sets input width
              guide:          bool                = False,  # When True, prints the guide for key
              validator:      Optional[Validator] = None,   # Input validator function
              validator_args: Optional[Any]       = None    # Arguments required by the validator
              ) -> str:                                     # Input from user
    """Display boxed input prompt with a message."""
    print_spacing(head)

    terminal_width = get_terminal_width()

    if key_type:
        key_guide = {B58_LOCAL_KEY:  B58_LOCAL_KEY_GUIDE,
                     B58_PUBLIC_KEY: B58_PUBLIC_KEY_GUIDE}.get(key_type, '')
        if guide:
            inner_spc = len(key_guide) + 2
        else:
            inner_spc = ENCODED_B58_PUB_KEY_LENGTH if key_type == B58_PUBLIC_KEY else ENCODED_B58_KDK_LENGTH
            inner_spc += 2  # Spacing around input space
    else:
        key_guide = ''
        inner_spc = terminal_width - 2 if expected_len == 0 else expected_len + 2

    upper_line = '┌'  + inner_spc * '─'  +  '┐'
    guide_line = '│ ' + key_guide        + ' │'
    input_line = '│'  + inner_spc * ' '  +  '│'
    lower_line = '└'  + inner_spc * '─'  +  '┘'
    box_indent = (terminal_width - len(upper_line)) // 2 * ' '

    terminal_width_check(len(upper_line))

    print(box_indent + upper_line)
    if guide:
        print(box_indent + guide_line)
    print(box_indent + input_line)
    print(box_indent + lower_line)
    print((5 if guide else 4) * CURSOR_UP_ONE_LINE)
    print(box_indent + '┌─┤' + message + '├')
    if guide:
        print('')

    user_input = input(box_indent + '│ ')

    if user_input == '':
        print(2 * CURSOR_UP_ONE_LINE)
        print(box_indent + '│ ' + default)
        user_input = default

    if validator is not None:
        error_msg = validator(user_input, validator_args)
        if error_msg:
            m_print(error_msg, head=1)
            print_on_previous_line(reps=4, delay=1)
            return box_input(message, default, head, tail, expected_len, key_type, guide, validator, validator_args)

    print_spacing(tail)

    return user_input