48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
@file input_helper.py
|
|
@brief This class can be used to get user input. A dictionary must be provided which describes the
|
|
input options.
|
|
@author J. Meier
|
|
@date 13.02.2021
|
|
"""
|
|
import logging
|
|
from typing import Tuple, Dict
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
class InputHelper:
|
|
def __init__(self, menu: Dict[str, Tuple[str, ...]]):
|
|
"""
|
|
@brief Constructor
|
|
@param menu The menu describing the input options
|
|
"""
|
|
self.menu = menu
|
|
|
|
def get_key(self) -> str:
|
|
"""
|
|
@brief Asks the user for input and returns the chosen key
|
|
"""
|
|
key = self.menu_handler()
|
|
while key not in self.menu:
|
|
_LOGGER.info("Invalid key specified, try again.")
|
|
key = self.menu_handler()
|
|
return key
|
|
|
|
def menu_handler(self) -> str:
|
|
key_column_width = 10
|
|
description_column_width = 50
|
|
separator_width = key_column_width + description_column_width + 3
|
|
separator_string = separator_width * "-"
|
|
key_string = "Key".ljust(key_column_width)
|
|
description_string = "Description".ljust(description_column_width)
|
|
_LOGGER.info(f"{key_string} | {description_string}")
|
|
_LOGGER.info(separator_string)
|
|
for key in self.menu:
|
|
key_string = key.ljust(key_column_width)
|
|
description_string = self.menu[key][0].ljust(description_column_width)
|
|
_LOGGER.info(f"{key_string} | {description_string}")
|
|
key = input("Specify key: ")
|
|
return key
|