From cfc45a9746b19dbf0080c77635d6db12f348e520 Mon Sep 17 00:00:00 2001 From: dw-0 Date: Sat, 22 Jun 2024 15:50:31 +0200 Subject: [PATCH] refactor: rework some klipper dialogs Signed-off-by: Dominik Willner --- kiauh/components/klipper/klipper_dialogs.py | 106 ++++++-------------- kiauh/components/klipper/klipper_setup.py | 14 ++- kiauh/components/klipper/klipper_utils.py | 28 ++++-- 3 files changed, 59 insertions(+), 89 deletions(-) diff --git a/kiauh/components/klipper/klipper_dialogs.py b/kiauh/components/klipper/klipper_dialogs.py index 7218c43..fc43c7a 100644 --- a/kiauh/components/klipper/klipper_dialogs.py +++ b/kiauh/components/klipper/klipper_dialogs.py @@ -13,7 +13,12 @@ from typing import List from core.instance_manager.base_instance import BaseInstance from core.menus.base_menu import print_back_footer -from utils.constants import COLOR_CYAN, COLOR_GREEN, COLOR_YELLOW, RESET_FORMAT +from utils.constants import ( + COLOR_CYAN, + COLOR_GREEN, + COLOR_YELLOW, + RESET_FORMAT, +) @unique @@ -29,7 +34,7 @@ def print_instance_overview( show_index=False, show_select_all=False, ): - dialog = "/=======================================================\\\n" + dialog = "╔═══════════════════════════════════════════════════════╗\n" if show_headline: d_type = ( "Klipper instances" @@ -37,13 +42,13 @@ def print_instance_overview( else "printer directories" ) headline = f"{COLOR_GREEN}The following {d_type} were found:{RESET_FORMAT}" - dialog += f"|{headline:^64}|\n" - dialog += "|-------------------------------------------------------|\n" + dialog += f"║{headline:^64}║\n" + dialog += "╟───────────────────────────────────────────────────────╢\n" if show_select_all: select_all = f"{COLOR_YELLOW}a) Select all{RESET_FORMAT}" - dialog += f"| {select_all:<63}|\n" - dialog += "| |\n" + dialog += f"║ {select_all:<63}║\n" + dialog += "║ ║\n" for i, s in enumerate(instances): if display_type is DisplayType.SERVICE_NAME: @@ -51,7 +56,8 @@ def print_instance_overview( else: name = s.data_dir line = f"{COLOR_CYAN}{f'{i})' if show_index else '●'} {name}{RESET_FORMAT}" - dialog += f"| {line:<63}|\n" + dialog += f"║ {line:<63}║\n" + dialog += "╟───────────────────────────────────────────────────────╢\n" print(dialog, end="") print_back_footer() @@ -62,13 +68,14 @@ def print_select_instance_count_dialog(): line2 = f"{COLOR_YELLOW}Setting up too many instances may crash your system.{RESET_FORMAT}" dialog = textwrap.dedent( f""" - /=======================================================\\ - | Please select the number of Klipper instances to set | - | up. The number of Klipper instances will determine | - | the amount of printers you can run from this host. | - | | - | {line1:<63}| - | {line2:<63}| + ╔═══════════════════════════════════════════════════════╗ + ║ Please select the number of Klipper instances to set ║ + ║ up. The number of Klipper instances will determine ║ + ║ the amount of printers you can run from this host. ║ + ║ ║ + ║ {line1:<63}║ + ║ {line2:<63}║ + ╟───────────────────────────────────────────────────────╢ """ )[1:] @@ -81,71 +88,16 @@ def print_select_custom_name_dialog(): line2 = f"{COLOR_YELLOW}Only alphanumeric characters are allowed!{RESET_FORMAT}" dialog = textwrap.dedent( f""" - /=======================================================\\ - | You can now assign a custom name to each instance. | - | If skipped, each instance will get an index assigned | - | in ascending order, starting at index '1'. | - | | - | {line1:<63}| - | {line2:<63}| + ╔═══════════════════════════════════════════════════════╗ + ║ You can now assign a custom name to each instance. ║ + ║ If skipped, each instance will get an index assigned ║ + ║ in ascending order, starting at index '1'. ║ + ║ ║ + ║ {line1:<63}║ + ║ {line2:<63}║ + ╟───────────────────────────────────────────────────────╢ """ )[1:] print(dialog, end="") print_back_footer() - - -def print_missing_usergroup_dialog(missing_groups) -> None: - line1 = f"{COLOR_YELLOW}WARNING: Your current user is not in group:{RESET_FORMAT}" - line2 = f"{COLOR_CYAN}● tty{RESET_FORMAT}" - line3 = f"{COLOR_CYAN}● dialout{RESET_FORMAT}" - line4 = f"{COLOR_YELLOW}INFO:{RESET_FORMAT}" - line5 = f"{COLOR_YELLOW}Relog required for group assignments to take effect!{RESET_FORMAT}" - - dialog = textwrap.dedent( - f""" - /=======================================================\\ - | {line1:<63}| - """ - )[1:] - - if "tty" in missing_groups: - dialog += f"| {line2:<63}|\n" - if "dialout" in missing_groups: - dialog += f"| {line3:<63}|\n" - - dialog += textwrap.dedent( - f""" - | | - | It is possible that you won't be able to successfully | - | connect and/or flash the controller board without | - | your user being a member of that group. | - | If you want to add the current user to the group(s) | - | listed above, answer with 'Y'. Else skip with 'n'. | - | | - | {line4:<63}| - | {line5:<63}| - \\=======================================================/ - """ - )[1:] - - print(dialog, end="") - - -def print_update_warn_dialog() -> None: - line1 = f"{COLOR_YELLOW}WARNING:{RESET_FORMAT}" - line2 = f"{COLOR_YELLOW}Do NOT continue if there are ongoing prints running!{RESET_FORMAT}" - line3 = f"{COLOR_YELLOW}All Klipper instances will be restarted during the {RESET_FORMAT}" - line4 = f"{COLOR_YELLOW}update process and ongoing prints WILL FAIL.{RESET_FORMAT}" - dialog = textwrap.dedent( - f""" - /=======================================================\\ - | {line1:<63}| - | {line2:<63}| - | {line3:<63}| - | {line4:<63}| - \\=======================================================/ - """ - )[1:] - - print(dialog, end="") diff --git a/kiauh/components/klipper/klipper_setup.py b/kiauh/components/klipper/klipper_setup.py index 55ad42b..4196f1f 100644 --- a/kiauh/components/klipper/klipper_setup.py +++ b/kiauh/components/klipper/klipper_setup.py @@ -16,7 +16,6 @@ from components.klipper import ( KLIPPER_REQUIREMENTS_TXT, ) from components.klipper.klipper import Klipper -from components.klipper.klipper_dialogs import print_update_warn_dialog from components.klipper.klipper_utils import ( add_to_existing, backup_klipper_dir, @@ -39,7 +38,7 @@ from core.settings.kiauh_settings import KiauhSettings from utils.common import check_install_dependencies from utils.git_utils import git_clone_wrapper, git_pull_wrapper from utils.input_utils import get_confirm -from utils.logger import Logger +from utils.logger import DialogType, Logger from utils.sys_utils import ( cmd_sysctl_manage, create_python_venv, @@ -139,7 +138,16 @@ def install_klipper_packages(klipper_dir: Path) -> None: def update_klipper() -> None: - print_update_warn_dialog() + Logger.print_dialog( + DialogType.WARNING, + [ + "Do NOT continue if there are ongoing prints running!", + "All Klipper instances will be restarted during the update process and " + "ongoing prints WILL FAIL.", + ], + end="", + ) + if not get_confirm("Update Klipper now?"): return diff --git a/kiauh/components/klipper/klipper_utils.py b/kiauh/components/klipper/klipper_utils.py index 3f97d09..19633c4 100644 --- a/kiauh/components/klipper/klipper_utils.py +++ b/kiauh/components/klipper/klipper_utils.py @@ -23,7 +23,6 @@ from components.klipper import ( from components.klipper.klipper import Klipper from components.klipper.klipper_dialogs import ( print_instance_overview, - print_missing_usergroup_dialog, print_select_custom_name_dialog, print_select_instance_count_dialog, ) @@ -201,18 +200,29 @@ def klipper_to_multi_conversion(new_name: str) -> None: def check_user_groups(): - current_groups = [grp.getgrgid(gid).gr_name for gid in os.getgroups()] - - missing_groups = [] - if "tty" not in current_groups: - missing_groups.append("tty") - if "dialout" not in current_groups: - missing_groups.append("dialout") + user_groups = [grp.getgrgid(gid).gr_name for gid in os.getgroups()] + missing_groups = [g for g in user_groups if g == "tty" or g == "dialout"] if not missing_groups: return - print_missing_usergroup_dialog(missing_groups) + Logger.print_dialog( + DialogType.ATTENTION, + [ + "Your current user is not in group:", + *[f"● {g}" for g in missing_groups], + "\n\n", + "It is possible that you won't be able to successfully connect and/or " + "flash the controller board without your user being a member of that " + "group. If you want to add the current user to the group(s) listed above, " + "answer with 'Y'. Else skip with 'n'.", + "\n\n", + "INFO:", + "Relog required for group assignments to take effect!", + ], + end="", + ) + if not get_confirm(f"Add user '{CURRENT_USER}' to group(s) now?"): log = "Skipped adding user to required groups. You might encounter issues." Logger.warn(log)