eive-tmtc/utility/input_helper.py

47 lines
1.5 KiB
Python
Raw Normal View History

2022-02-14 16:52:32 +01:00
# -*- 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
2022-02-27 16:17:24 +01:00
@param menu The menu describing the input options
2022-02-14 16:52:32 +01:00
"""
self.menu = menu
def get_key(self) -> str:
"""
2022-02-27 16:17:24 +01:00
@brief Asks the user for input and returns the chosen key
2022-02-14 16:52:32 +01:00
"""
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