eive-tmtc/utility/input_helper.py
2022-04-05 00:51:52 +02:00

47 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
"""
from tmtccmd.logging import get_console_logger
LOGGER = get_console_logger()
class InputHelper:
def __init__(self, menu: dict):
"""
@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