refactor(kiauh): rework menu formatting logic

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2023-11-15 00:25:19 +01:00
parent 279d000bb0
commit 6128e35d45
7 changed files with 82 additions and 62 deletions

View File

@@ -0,0 +1,14 @@
#!/usr/bin/env python3
# ======================================================================= #
# Copyright (C) 2020 - 2023 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
QUIT_FOOTER = "quit"
BACK_FOOTER = "back"
BACK_HELP_FOOTER = "back_help"

View File

@@ -11,19 +11,23 @@
import textwrap import textwrap
from kiauh.core.menus import BACK_FOOTER
from kiauh.core.menus.base_menu import BaseMenu from kiauh.core.menus.base_menu import BaseMenu
from kiauh.utils.constants import COLOR_YELLOW, RESET_FORMAT from kiauh.utils.constants import COLOR_YELLOW, RESET_FORMAT
class AdvancedMenu(BaseMenu): class AdvancedMenu(BaseMenu):
def __init__(self): def __init__(self):
super().__init__(header=True, options={}, footer_type="back") super().__init__(header=True, options={}, footer_type=BACK_FOOTER)
def print_menu(self): def print_menu(self):
header = " [ Advanced Menu ] "
color = COLOR_YELLOW
count = 62 - len(color) - len(RESET_FORMAT)
menu = textwrap.dedent( menu = textwrap.dedent(
f""" f"""
/=======================================================\\ /=======================================================\\
| {COLOR_YELLOW}~~~~~~~~~~~~~ [ Advanced Menu ] ~~~~~~~~~~~~~{RESET_FORMAT} | | {color}{header:~^{count}}{RESET_FORMAT} |
|-------------------------------------------------------| |-------------------------------------------------------|
| Klipper & API: | Mainsail: | | Klipper & API: | Mainsail: |
| 0) [Rollback] | 5) [Theme installer] | | 0) [Rollback] | 5) [Theme installer] |

View File

@@ -13,8 +13,9 @@ import subprocess
import sys import sys
import textwrap import textwrap
from abc import abstractmethod, ABC from abc import abstractmethod, ABC
from typing import Dict, Any from typing import Dict, Any, Literal
from kiauh.core.menus import QUIT_FOOTER, BACK_FOOTER, BACK_HELP_FOOTER
from kiauh.utils.constants import ( from kiauh.utils.constants import (
COLOR_GREEN, COLOR_GREEN,
COLOR_YELLOW, COLOR_YELLOW,
@@ -29,12 +30,17 @@ def clear():
def print_header(): def print_header():
line1 = " [ KIAUH ] "
line2 = "Klipper Installation And Update Helper"
line3 = ""
color = COLOR_CYAN
count = 62 - len(color) - len(RESET_FORMAT)
header = textwrap.dedent( header = textwrap.dedent(
f""" f"""
/=======================================================\\ /=======================================================\\
| {COLOR_CYAN}~~~~~~~~~~~~~~~~~ [ KIAUH ] ~~~~~~~~~~~~~~~~~{RESET_FORMAT} | | {color}{line1:~^{count}}{RESET_FORMAT} |
| {COLOR_CYAN} Klipper Installation And Update Helper {RESET_FORMAT} | | {color}{line2:^{count}}{RESET_FORMAT} |
| {COLOR_CYAN}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{RESET_FORMAT} | | {color}{line3:~^{count}}{RESET_FORMAT} |
\=======================================================/ \=======================================================/
""" """
)[1:] )[1:]
@@ -42,10 +48,13 @@ def print_header():
def print_quit_footer(): def print_quit_footer():
text = "Q) Quit"
color = COLOR_RED
count = 62 - len(color) - len(RESET_FORMAT)
footer = textwrap.dedent( footer = textwrap.dedent(
f""" f"""
|-------------------------------------------------------| |-------------------------------------------------------|
| {COLOR_RED}Q) Quit{RESET_FORMAT} | | {color}{text:^{count}}{RESET_FORMAT} |
\=======================================================/ \=======================================================/
""" """
)[1:] )[1:]
@@ -53,10 +62,13 @@ def print_quit_footer():
def print_back_footer(): def print_back_footer():
text = "B) « Back"
color = COLOR_GREEN
count = 62 - len(color) - len(RESET_FORMAT)
footer = textwrap.dedent( footer = textwrap.dedent(
f""" f"""
|-------------------------------------------------------| |-------------------------------------------------------|
| {COLOR_GREEN}B) « Back{RESET_FORMAT} | | {color}{text:^{count}}{RESET_FORMAT} |
\=======================================================/ \=======================================================/
""" """
)[1:] )[1:]
@@ -64,32 +76,15 @@ def print_back_footer():
def print_back_help_footer(): def print_back_help_footer():
text1 = "B) « Back"
text2 = "H) Help [?]"
color1 = COLOR_GREEN
color2 = COLOR_YELLOW
count = 34 - len(color1) - len(RESET_FORMAT)
footer = textwrap.dedent( footer = textwrap.dedent(
f""" f"""
|-------------------------------------------------------| |-------------------------------------------------------|
| {COLOR_GREEN}B) « Back{RESET_FORMAT} | {COLOR_RED}Q) Quit{RESET_FORMAT} | | {color1}{text1:^{count}}{RESET_FORMAT} | {color2}{text2:^{count}}{RESET_FORMAT} |
\=======================================================/
"""
)[1:]
print(footer, end="")
def print_back_quit_footer():
footer = textwrap.dedent(
f"""
|-------------------------------------------------------|
| {COLOR_GREEN}B) « Back{RESET_FORMAT} | {COLOR_YELLOW}H) Help [?]{RESET_FORMAT} |
\=======================================================/
"""
)[1:]
print(footer, end="")
def print_back_quit_help_footer():
footer = textwrap.dedent(
f"""
|-------------------------------------------------------|
| {COLOR_GREEN}B) « Back{RESET_FORMAT} | {COLOR_RED}Q) Quit{RESET_FORMAT} | {COLOR_YELLOW}H) Help [?]{RESET_FORMAT} |
\=======================================================/ \=======================================================/
""" """
)[1:] )[1:]
@@ -97,14 +92,14 @@ def print_back_quit_help_footer():
class BaseMenu(ABC): class BaseMenu(ABC):
QUIT_FOOTER = "quit"
BACK_FOOTER = "back"
BACK_HELP_FOOTER = "back_help"
BACK_QUIT_FOOTER = "back_quit"
BACK_QUIT_HELP_FOOTER = "back_quit_help"
def __init__( def __init__(
self, options: Dict[int, Any], options_offset=0, header=True, footer_type="quit" self,
options: Dict[int, Any],
options_offset: int = 0,
header: bool = True,
footer_type: Literal[
"QUIT_FOOTER", "BACK_FOOTER", "BACK_HELP_FOOTER"
] = QUIT_FOOTER,
): ):
self.options = options self.options = options
self.options_offset = options_offset self.options_offset = options_offset
@@ -117,11 +112,9 @@ class BaseMenu(ABC):
def print_footer(self): def print_footer(self):
footer_type_map = { footer_type_map = {
self.QUIT_FOOTER: print_quit_footer, QUIT_FOOTER: print_quit_footer,
self.BACK_FOOTER: print_back_footer, BACK_FOOTER: print_back_footer,
self.BACK_HELP_FOOTER: print_back_help_footer, BACK_HELP_FOOTER: print_back_help_footer,
self.BACK_QUIT_FOOTER: print_back_quit_footer,
self.BACK_QUIT_HELP_FOOTER: print_back_quit_help_footer,
} }
footer_function = footer_type_map.get(self.footer_type, print_quit_footer) footer_function = footer_type_map.get(self.footer_type, print_quit_footer)
footer_function() footer_function()
@@ -150,8 +143,6 @@ class BaseMenu(ABC):
"quit": ["q"], "quit": ["q"],
"back": ["b"], "back": ["b"],
"back_help": ["b", "h"], "back_help": ["b", "h"],
"back_quit": ["b", "q"],
"back_quit_help": ["b", "q", "h"],
} }
if ( if (
self.footer_type in allowed_input self.footer_type in allowed_input

View File

@@ -11,6 +11,7 @@
import textwrap import textwrap
from kiauh.core.menus import BACK_FOOTER
from kiauh.core.menus.base_menu import BaseMenu from kiauh.core.menus.base_menu import BaseMenu
from kiauh.modules.klipper import klipper_setup from kiauh.modules.klipper import klipper_setup
from kiauh.utils.constants import COLOR_GREEN, RESET_FORMAT from kiauh.utils.constants import COLOR_GREEN, RESET_FORMAT
@@ -34,18 +35,17 @@ class InstallMenu(BaseMenu):
10: self.install_mobileraker, 10: self.install_mobileraker,
11: self.install_crowsnest, 11: self.install_crowsnest,
}, },
footer_type="back", footer_type=BACK_FOOTER,
) )
def print_menu(self): def print_menu(self):
header = " [ Installation Menu ] "
color = COLOR_GREEN
count = 62 - len(color) - len(RESET_FORMAT)
menu = textwrap.dedent( menu = textwrap.dedent(
f""" f"""
/=======================================================\\ /=======================================================\\
| {COLOR_GREEN}~~~~~~~~~~~ [ Installation Menu ] ~~~~~~~~~~~{RESET_FORMAT} | | {color}{header:~^{count}}{RESET_FORMAT} |
|-------------------------------------------------------|
| You need this menu usually only for installing |
| all necessary dependencies for the various |
| functions on a completely fresh system. |
|-------------------------------------------------------| |-------------------------------------------------------|
| Firmware & API: | Other: | | Firmware & API: | Other: |
| 1) [Klipper] | 6) [PrettyGCode] | | 1) [Klipper] | 6) [PrettyGCode] |

View File

@@ -11,6 +11,7 @@
import textwrap import textwrap
from kiauh.core.menus import QUIT_FOOTER
from kiauh.core.menus.advanced_menu import AdvancedMenu from kiauh.core.menus.advanced_menu import AdvancedMenu
from kiauh.core.menus.base_menu import BaseMenu from kiauh.core.menus.base_menu import BaseMenu
from kiauh.core.menus.install_menu import InstallMenu from kiauh.core.menus.install_menu import InstallMenu
@@ -25,7 +26,7 @@ class MainMenu(BaseMenu):
super().__init__( super().__init__(
header=True, header=True,
options={ options={
0: self.test, 0: None,
1: InstallMenu, 1: InstallMenu,
2: UpdateMenu, 2: UpdateMenu,
3: RemoveMenu, 3: RemoveMenu,
@@ -33,14 +34,19 @@ class MainMenu(BaseMenu):
5: None, 5: None,
6: SettingsMenu, 6: SettingsMenu,
}, },
footer_type="quit", footer_type=QUIT_FOOTER,
) )
def print_menu(self): def print_menu(self):
header = " [ Main Menu ] "
footer1 = "KIAUH v6.0.0"
footer2 = f"Changelog: {COLOR_MAGENTA}https://git.io/JnmlX{RESET_FORMAT}"
color = COLOR_CYAN
count = 62 - len(color) - len(RESET_FORMAT)
menu = textwrap.dedent( menu = textwrap.dedent(
f""" f"""
/=======================================================\\ /=======================================================\\
| {COLOR_CYAN}~~~~~~~~~~~~~~~ [ Main Menu ] ~~~~~~~~~~~~~~~{RESET_FORMAT} | | {color}{header:~^{count}}{RESET_FORMAT} |
|-------------------------------------------------------| |-------------------------------------------------------|
| 0) [Log-Upload] | Klipper: <TODO> | | 0) [Log-Upload] | Klipper: <TODO> |
| | Repo: <TODO> | | | Repo: <TODO> |
@@ -58,10 +64,7 @@ class MainMenu(BaseMenu):
| | Obico: <TODO> | | | Obico: <TODO> |
| | OctoEverywhere: <TODO> | | | OctoEverywhere: <TODO> |
|-------------------------------------------------------| |-------------------------------------------------------|
| {COLOR_CYAN}KIAUH v6.0.0{RESET_FORMAT} | Changelog: {COLOR_MAGENTA}https://git.io/JnmlX{RESET_FORMAT} | | {COLOR_CYAN}{footer1:^16}{RESET_FORMAT} | {footer2:^43} |
""" """
)[1:] )[1:]
print(menu, end="") print(menu, end="")
def test(self):
print("blub")

View File

@@ -11,6 +11,7 @@
import textwrap import textwrap
from kiauh.core.menus import BACK_FOOTER
from kiauh.core.menus.base_menu import BaseMenu from kiauh.core.menus.base_menu import BaseMenu
from kiauh.modules.klipper import klipper_setup from kiauh.modules.klipper import klipper_setup
from kiauh.utils.constants import COLOR_RED, RESET_FORMAT from kiauh.utils.constants import COLOR_RED, RESET_FORMAT
@@ -38,14 +39,17 @@ class RemoveMenu(BaseMenu):
14: self.remove_mobileraker, 14: self.remove_mobileraker,
15: self.remove_nginx, 15: self.remove_nginx,
}, },
footer_type="back", footer_type=BACK_FOOTER,
) )
def print_menu(self): def print_menu(self):
header = " [ Remove Menu ] "
color = COLOR_RED
count = 62 - len(color) - len(RESET_FORMAT)
menu = textwrap.dedent( menu = textwrap.dedent(
f""" f"""
/=======================================================\\ /=======================================================\\
| {COLOR_RED}~~~~~~~~~~~~~~ [ Remove Menu ] ~~~~~~~~~~~~~~{RESET_FORMAT} | | {color}{header:~^{count}}{RESET_FORMAT} |
|-------------------------------------------------------| |-------------------------------------------------------|
| INFO: Configurations and/or any backups will be kept! | | INFO: Configurations and/or any backups will be kept! |
|-------------------------------------------------------| |-------------------------------------------------------|

View File

@@ -11,6 +11,7 @@
import textwrap import textwrap
from kiauh.core.menus import BACK_FOOTER
from kiauh.core.menus.base_menu import BaseMenu from kiauh.core.menus.base_menu import BaseMenu
from kiauh.modules.klipper.klipper_setup import update_klipper from kiauh.modules.klipper.klipper_setup import update_klipper
from kiauh.utils.constants import COLOR_GREEN, RESET_FORMAT from kiauh.utils.constants import COLOR_GREEN, RESET_FORMAT
@@ -36,14 +37,17 @@ class UpdateMenu(BaseMenu):
11: self.update_crowsnest, 11: self.update_crowsnest,
12: self.upgrade_system_packages, 12: self.upgrade_system_packages,
}, },
footer_type="back", footer_type=BACK_FOOTER,
) )
def print_menu(self): def print_menu(self):
header = " [ Update Menu ] "
color = COLOR_GREEN
count = 62 - len(color) - len(RESET_FORMAT)
menu = textwrap.dedent( menu = textwrap.dedent(
f""" f"""
/=======================================================\\ /=======================================================\\
| {COLOR_GREEN}~~~~~~~~~~~~~~ [ Update Menu ] ~~~~~~~~~~~~~~{RESET_FORMAT} | | {color}{header:~^{count}}{RESET_FORMAT} |
|-------------------------------------------------------| |-------------------------------------------------------|
| 0) [Update all] | | | | 0) [Update all] | | |
| | Current: | Latest: | | | Current: | Latest: |