eive-tmtc/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
"""
2023-02-01 11:17:04 +01:00
import logging
2022-02-14 16:52:32 +01:00
2023-02-01 11:17:04 +01:00
_LOGGER = logging.getLogger(__name__)
2022-02-14 16:52:32 +01:00
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:
2023-02-01 11:17:04 +01:00
_LOGGER.info("Invalid key specified, try again.")
2022-02-14 16:52:32 +01:00
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)
2023-02-01 11:17:04 +01:00
_LOGGER.info(f"{key_string} | {description_string}")
_LOGGER.info(separator_string)
2022-02-14 16:52:32 +01:00
for key in self.menu:
key_string = key.ljust(key_column_width)
description_string = self.menu[key][0].ljust(description_column_width)
2023-02-01 11:17:04 +01:00
_LOGGER.info(f"{key_string} | {description_string}")
2022-02-14 16:52:32 +01:00
key = input("Specify key: ")
return key