50 lines
1.5 KiB
Python
50 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.utility.logger import get_console_logger
|
||
|
|
||
|
LOGGER = get_console_logger()
|
||
|
|
||
|
|
||
|
class InputHelper:
|
||
|
|
||
|
def __init__(self, menu: dict):
|
||
|
"""
|
||
|
@brief Constructor
|
||
|
@param menu The describing the input options
|
||
|
"""
|
||
|
self.menu = menu
|
||
|
|
||
|
def get_key(self) -> str:
|
||
|
"""
|
||
|
@brief Ask 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
|
||
|
|
||
|
|