From 2f34253badcfde34330d9e441fcc58975fb92bee Mon Sep 17 00:00:00 2001 From: dw-0 Date: Thu, 8 Feb 2024 22:41:52 +0100 Subject: [PATCH] refactor(kiauh): handle menus based on if they need instantiation or not Signed-off-by: Dominik Willner --- kiauh/core/menus/base_menu.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/kiauh/core/menus/base_menu.py b/kiauh/core/menus/base_menu.py index 38a9873..2cdf379 100644 --- a/kiauh/core/menus/base_menu.py +++ b/kiauh/core/menus/base_menu.py @@ -97,13 +97,14 @@ class BaseMenu(ABC): def __init__( self, - options: Dict[str, Union[Callable, Type["BaseMenu"]]], + options: Dict[str, Union[Callable, Any]], options_offset: int = 0, header: bool = True, footer_type: Literal[ "QUIT_FOOTER", "BACK_FOOTER", "BACK_HELP_FOOTER" ] = QUIT_FOOTER, ): + self.previous_menu = None self.options = options self.options_offset = options_offset self.header = header @@ -131,11 +132,11 @@ class BaseMenu(ABC): def handle_user_input(self) -> str: while True: - choice = input(f"{COLOR_CYAN}###### Perform action: {RESET_FORMAT}") + choice = input(f"{COLOR_CYAN}###### Perform action: {RESET_FORMAT}").lower() option = self.options.get(choice, None) has_navi_option = self.footer_type in self.NAVI_OPTIONS - user_navigated = choice.lower() in self.NAVI_OPTIONS[self.footer_type] + user_navigated = choice in self.NAVI_OPTIONS[self.footer_type] if has_navi_option and user_navigated: return choice @@ -163,7 +164,9 @@ class BaseMenu(ABC): option = self.options.get(choice, None) if isinstance(option, type) and issubclass(option, BaseMenu): - self.navigate_to_submenu(option) + self.navigate_to_menu(option, True) + elif isinstance(option, BaseMenu): + self.navigate_to_menu(option, False) elif callable(option): option(opt_index=choice) elif option is None: @@ -173,7 +176,14 @@ class BaseMenu(ABC): f"Type {type(option)} of option {choice} not of type BaseMenu or Method" ) - def navigate_to_submenu(self, submenu_class) -> None: - submenu = submenu_class() - submenu.previous_menu = self - submenu.start() + def navigate_to_menu(self, menu, instantiate: bool) -> None: + """ + Method for handling the actual menu switch. Can either take in a menu type or an already + instantiated menu class. Use instantiated menu classes only if the menu requires specific input parameters + :param menu: A menu type or menu instance + :param instantiate: Specify if the menu requires instantiation + :return: None + """ + menu = menu() if instantiate else menu + menu.previous_menu = self + menu.start()