Exemplo n.º 1
0
def ask_confirmation_code(source: str  # The system the confirmation code is displayed by
                          ) -> str:    # The confirmation code entered by the user
    """\
    Ask the user to input confirmation code from Source Computer to
    verify local key has been installed.
    """
    title       = f"Enter confirmation code (from {source}): "
    input_space = len(' ff ')

    upper_line = '┌' + (len(title) + input_space) * '─' + '┐'
    title_line = '│' +      title  + input_space  * ' ' + '│'
    lower_line = '└' + (len(title) + 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)

    terminal_width_check(len(upper_line))

    print(upper_line)
    print(title_line)
    print(lower_line)
    print(3 * CURSOR_UP_ONE_LINE)

    indent = title_line.find('│')
    return input(indent * ' ' + f'│ {title}')
Exemplo n.º 2
0
def pwd_prompt(message: str,          # Prompt message
               repeat:  bool = False  # When True, prints corner chars for the second box
               ) -> str:              # Password from user
    """Prompt the user to enter a password.

    The getpass library ensures the password is not echoed on screen
    when it is typed.
    """
    l, r = ('├', '┤') if repeat else ('┌', '┐')

    terminal_w  = get_terminal_width()
    input_space = len(' c ')  # `c` is where the caret sits

    upper_line = ( l  + (len(message) + input_space) * '─' +  r ).center(terminal_w)
    title_line = ('│' +      message  + input_space  * ' ' + '│').center(terminal_w)
    lower_line = ('└' + (len(message) + input_space) * '─' + '┘').center(terminal_w)

    terminal_width_check(len(upper_line))

    print(upper_line)
    print(title_line)
    print(lower_line)
    print(3 * CURSOR_UP_ONE_LINE)

    indent     = title_line.find('│')
    user_input = getpass.getpass(indent * ' ' + f'│ {message}')

    return user_input
Exemplo n.º 3
0
Arquivo: input.py Projeto: 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
Exemplo n.º 4
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
Exemplo n.º 5
0
 def test_width_check(self, *_):
     self.assertIsNone(terminal_width_check(80))
Exemplo n.º 6
0
 def test_width_check(self, *_: Any) -> None:
     self.assertIsNone(terminal_width_check(80))