From 341ecb325cbd4bb4d852d2a187dd33aecfbb9266 Mon Sep 17 00:00:00 2001 From: dw-0 Date: Sun, 24 Mar 2024 00:01:36 +0100 Subject: [PATCH] refactor(klipper): instance overview dialog can now show printer folder and not only services Signed-off-by: Dominik Willner --- kiauh/components/klipper/klipper_dialogs.py | 39 +++++++++++++------ kiauh/components/klipper/klipper_remove.py | 2 +- .../components/moonraker/moonraker_remove.py | 2 +- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/kiauh/components/klipper/klipper_dialogs.py b/kiauh/components/klipper/klipper_dialogs.py index 2cfc672..ada75b6 100644 --- a/kiauh/components/klipper/klipper_dialogs.py +++ b/kiauh/components/klipper/klipper_dialogs.py @@ -8,24 +8,37 @@ # ======================================================================= # import textwrap -from typing import List +from enum import Enum, unique +from typing import List, Union, Literal from core.instance_manager.base_instance import BaseInstance from core.menus.base_menu import print_back_footer from utils.constants import COLOR_GREEN, RESET_FORMAT, COLOR_YELLOW, COLOR_CYAN +@unique +class DisplayType(Enum): + SERVICE_NAME = "SERVICE_NAME" + PRINTER_NAME = "PRINTER_NAME" + + def print_instance_overview( - instances: List[BaseInstance], show_index=False, show_select_all=False + instances: List[BaseInstance], + display_type: DisplayType = DisplayType.SERVICE_NAME, + show_headline=True, + show_index=False, + show_select_all=False, ): - headline = f"{COLOR_GREEN}The following Klipper instances were found:{RESET_FORMAT}" - dialog = textwrap.dedent( - f""" - /=======================================================\\ - |{headline:^64}| - |-------------------------------------------------------| - """ - )[1:] + dialog = "/=======================================================\\\n" + if show_headline: + d_type = ( + "Klipper instances" + if display_type is DisplayType.SERVICE_NAME + else "printer directories" + ) + headline = f"{COLOR_GREEN}The following {d_type} were found:{RESET_FORMAT}" + dialog += f"|{headline:^64}|\n" + dialog += "|-------------------------------------------------------|\n" if show_select_all: select_all = f"{COLOR_YELLOW}a) Select all{RESET_FORMAT}" @@ -33,7 +46,11 @@ def print_instance_overview( dialog += "| |\n" for i, s in enumerate(instances): - line = f"{COLOR_CYAN}{f'{i})' if show_index else '●'} {s.get_service_file_name()}{RESET_FORMAT}" + if display_type is DisplayType.SERVICE_NAME: + name = s.get_service_file_name() + else: + name = s.data_dir + line = f"{COLOR_CYAN}{f'{i})' if show_index else '●'} {name}{RESET_FORMAT}" dialog += f"| {line:<63}|\n" print(dialog, end="") diff --git a/kiauh/components/klipper/klipper_remove.py b/kiauh/components/klipper/klipper_remove.py index 7245201..2c30e30 100644 --- a/kiauh/components/klipper/klipper_remove.py +++ b/kiauh/components/klipper/klipper_remove.py @@ -62,7 +62,7 @@ def run_klipper_removal( def select_instances_to_remove( instances: List[Klipper], ) -> Union[List[Klipper], None]: - print_instance_overview(instances, True, True) + print_instance_overview(instances, show_index=True, show_select_all=True) options = [str(i) for i in range(len(instances))] options.extend(["a", "A", "b", "B"]) diff --git a/kiauh/components/moonraker/moonraker_remove.py b/kiauh/components/moonraker/moonraker_remove.py index 04a9872..7d469fb 100644 --- a/kiauh/components/moonraker/moonraker_remove.py +++ b/kiauh/components/moonraker/moonraker_remove.py @@ -68,7 +68,7 @@ def run_moonraker_removal( def select_instances_to_remove( instances: List[Moonraker], ) -> Union[List[Moonraker], None]: - print_instance_overview(instances, True, True) + print_instance_overview(instances, show_index=True, show_select_all=True) options = [str(i) for i in range(len(instances))] options.extend(["a", "A", "b", "B"])