mirror of
https://github.com/dw-0/kiauh.git
synced 2025-12-24 16:23:36 +05:00
feat: implement method for printing formatted dialogs
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
# #
|
||||
# This file may be distributed under the terms of the GNU GPLv3 license #
|
||||
# ======================================================================= #
|
||||
import textwrap
|
||||
from enum import Enum
|
||||
|
||||
from utils.constants import (
|
||||
COLOR_WHITE,
|
||||
@@ -14,9 +16,31 @@ from utils.constants import (
|
||||
COLOR_RED,
|
||||
COLOR_MAGENTA,
|
||||
RESET_FORMAT,
|
||||
COLOR_CYAN,
|
||||
)
|
||||
|
||||
|
||||
class DialogType(Enum):
|
||||
INFO = ("INFO", COLOR_WHITE)
|
||||
SUCCESS = ("SUCCESS", COLOR_GREEN)
|
||||
ATTENTION = ("ATTENTION", COLOR_YELLOW)
|
||||
WARNING = ("WARNING", COLOR_YELLOW)
|
||||
ERROR = ("ERROR", COLOR_RED)
|
||||
CUSTOM = (None, None)
|
||||
|
||||
|
||||
class DialogCustomColor(Enum):
|
||||
WHITE = COLOR_WHITE
|
||||
GREEN = COLOR_GREEN
|
||||
YELLOW = COLOR_YELLOW
|
||||
RED = COLOR_RED
|
||||
CYAN = COLOR_CYAN
|
||||
MAGENTA = COLOR_MAGENTA
|
||||
|
||||
|
||||
LINE_WIDTH = 53
|
||||
|
||||
|
||||
class Logger:
|
||||
@staticmethod
|
||||
def info(msg):
|
||||
@@ -57,3 +81,69 @@ class Logger:
|
||||
def print_status(msg, prefix=True, start="", end="\n") -> None:
|
||||
message = f"\n###### {msg}" if prefix else msg
|
||||
print(f"{COLOR_MAGENTA}{start}{message}{RESET_FORMAT}", end=end)
|
||||
|
||||
@staticmethod
|
||||
def print_dialog(
|
||||
title: DialogType,
|
||||
content: str,
|
||||
custom_title: str = None,
|
||||
custom_color: DialogCustomColor = None,
|
||||
) -> None:
|
||||
dialog_color = Logger._get_dialog_color(title, custom_color)
|
||||
dialog_title = Logger._get_dialog_title(title, custom_title)
|
||||
dialog_title_formatted = Logger._format_dialog_title(dialog_title)
|
||||
dialog_content = Logger._format_dialog_content(content, LINE_WIDTH)
|
||||
top = Logger._format_top_border(dialog_color)
|
||||
bottom = Logger._format_bottom_border()
|
||||
|
||||
print(
|
||||
f"{top}{dialog_title_formatted}{dialog_content}{bottom}",
|
||||
end="",
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _get_dialog_title(title: DialogType, custom_title: str = None) -> str:
|
||||
if title == DialogType.CUSTOM and custom_title:
|
||||
return f"[ {custom_title} ]"
|
||||
return f"[ {title.value[0]} ]" if title.value[0] else None
|
||||
|
||||
@staticmethod
|
||||
def _get_dialog_color(
|
||||
title: DialogType, custom_color: DialogCustomColor = None
|
||||
) -> str:
|
||||
if title == DialogType.CUSTOM and custom_color:
|
||||
return str(custom_color.value)
|
||||
return title.value[1] if title.value[1] else DialogCustomColor.WHITE.value
|
||||
|
||||
@staticmethod
|
||||
def _format_top_border(color: str) -> str:
|
||||
return textwrap.dedent(f"""
|
||||
{color}┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
|
||||
""")[:-1]
|
||||
|
||||
@staticmethod
|
||||
def _format_bottom_border() -> str:
|
||||
return textwrap.dedent(f"""
|
||||
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
|
||||
{RESET_FORMAT}""")
|
||||
|
||||
@staticmethod
|
||||
def _format_dialog_title(title: str) -> str:
|
||||
if title is not None:
|
||||
return textwrap.dedent(f"""
|
||||
┃ {title:^{LINE_WIDTH}} ┃
|
||||
┠───────────────────────────────────────────────────────┨
|
||||
""")
|
||||
else:
|
||||
return "\n"
|
||||
|
||||
@staticmethod
|
||||
def _format_dialog_content(content: str, line_width: int) -> str:
|
||||
border_left = "┃"
|
||||
border_right = "┃"
|
||||
wrapper = textwrap.TextWrapper(line_width)
|
||||
lines = wrapper.wrap(content)
|
||||
formatted_lines = [
|
||||
f"{border_left} {line:<{line_width}} {border_right}" for line in lines
|
||||
]
|
||||
return "\n".join(formatted_lines)
|
||||
|
||||
Reference in New Issue
Block a user