Skip to content

Commit

Permalink
Fixed gui text/table formatting
Browse files Browse the repository at this point in the history
- Added new text modifier: 'subscript'
- Added new font modifier: 'face'
- Added new font modifier: 'bgcolor'
  • Loading branch information
DuckBoss committed Apr 23, 2023
1 parent 1b719ac commit 75f8ec3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 18 deletions.
45 changes: 34 additions & 11 deletions src/lib/frameworks/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Optional, Union, List
from .constants import TextTypes
from .utility import TagModifiers
from .utility import FontModifiers, AlignmentModifiers
from ....utils import mumble_utils


Expand All @@ -28,12 +28,13 @@ def __init__(self, settings: Optional["Settings"] = None) -> None:
self._box_close = ""
self._box_rows = []

def open(self, settings: Optional["Settings"] = None) -> bool:
def open(self, settings: Optional["Settings"] = None, **kwargs) -> bool:
if self.is_open:
return False
if settings is None:
settings = self.settings
self._box_open = f'<table bgcolor="{self.settings.background_color}" align="{self.settings.background_color}">'
settings.update(**kwargs)
self._box_open = f'<table bgcolor="{settings.table_bg_color}" align="{settings.table_align}" cellspacing="1" cellpadding="5">'
self.is_open = True
return True

Expand All @@ -44,14 +45,19 @@ def close(self) -> bool:
self.is_open = False
return True

def add_row(self, text: str, settings: Optional["Settings"] = None) -> bool:
def add_row(self, text: str, settings: Optional["Settings"] = None, **kwargs) -> bool:
if not self.is_open:
return False
if settings is None:
settings = self.settings
_row_type = f"{'tr' if self.settings.text_type == TextTypes.BODY else 'th'}"
settings.update(**kwargs)
_row_type = f"{'td' if settings.text_type == TextTypes.BODY else 'th'}"
self._box_rows.append(
f'<{_row_type} {TagModifiers.align(self.settings.text_align)} {TagModifiers.color(settings.text_color)}">{text}</{_row_type}>'
f"<tr {FontModifiers.bgcolor(settings.row_bg_color)}>"
f"<{_row_type} {AlignmentModifiers.align(settings.row_align)}>"
f"<font {FontModifiers.color(settings.text_color)}>{text}</font>"
f"</{_row_type}>"
f"</tr>"
)
return True

Expand All @@ -68,10 +74,27 @@ class Settings:
text_type: TextTypes = TextTypes.HEADER
text_color: str = "yellow"
text_align: str = "center"
background_color: str = "black"

def __init__(self) -> None:
logger.debug("Initialized GUI Framework.")
table_align: str = "center"
table_bg_color: str = "black"

row_align: str = "left"
row_bg_color: str = "black"

def __init__(self, **kwargs) -> None:
self.update(**kwargs)

def update(self, **kwargs) -> "GUIFramework.ContentBox.Settings":
self.text_type = kwargs.get("text_type", self.text_type)
self.text_color = kwargs.get("text_color", self.text_color)
self.text_align = kwargs.get("text_align", self.text_align)

self.table_align = kwargs.get("table_align", self.table_align)
self.table_bg_color = kwargs.get("table_bg_color", self.table_bg_color)

self.row_align = kwargs.get("row_align", self.row_align)
self.row_bg_color = kwargs.get("bg_color", self.row_bg_color)
return self

@staticmethod
def gui(
Expand All @@ -84,9 +107,9 @@ def gui(
raw_text = [text]

_content = GUIFramework.ContentBox(settings=settings)
_content.open()
_content.open(**kwargs)
for idx, row in enumerate(raw_text):
_content.add_row(row)
_content.add_row(row, **kwargs)
_content.close()

_compiled_text: str = _content.compile()
Expand Down
20 changes: 17 additions & 3 deletions src/lib/frameworks/gui/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,26 @@ def italicize(text):
def underline(text):
return f"<u>{text}</u>"

@staticmethod
def subscript(text):
return f"<sub>{text}</sub>"


class TagModifiers:
class FontModifiers:
@staticmethod
def color(color: str) -> str:
return f"color={color}"
return f'color="{color}"'

@staticmethod
def face(face: str) -> str:
return f'face="{face}"'

@staticmethod
def bgcolor(bgcolor: str) -> str:
return f'bgcolor="{bgcolor}"'


class AlignmentModifiers:
@staticmethod
def align(align: str) -> str:
return f"align={align}"
return f'align="{align}"'
16 changes: 12 additions & 4 deletions src/services/cmd_processing_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,22 @@ async def _process_cmd(self) -> None:
logger.warning(f"The command: [{_cmd_name}] is not a registered command.")
_all_command_names = [command for command in _callbacks.keys()]
_command_suggestions = process.extract(_cmd_name, _all_command_names, limit=3)
_command_suggestions = [x[0] for x in _command_suggestions if x[1] >= 75]
logger.debug(f"Found command suggestions: {_command_suggestions}")
_command_suggestions = [x[0] for x in _command_suggestions if x[1] >= 70]
# Only display suggestions if there is a closely matched ratio.
if _command_suggestions:
_msg = f"The command: [{_cmd_name}] could not be found. Did you mean any of the following: "
_msg += f"[{', '.join(_command_suggestions)}]?"
_suggestion_msgs = []
for idx, cmd in enumerate(_command_suggestions):
_suggestion_msgs.append(f"- {cmd} ")
_msgs = [
f"The command: [{_cmd_name}] could not be found. ",
"Did you mean any of the following: ",
*_suggestion_msgs,
]
GUIFramework.gui(
_msg,
_msgs,
target_users=mumble_utils.get_user_by_id(command.actor),
table_align="left",
)
return

Expand Down
1 change: 1 addition & 0 deletions src/utils/mumble_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def echo(
target_users: Optional[Union[List["User"], "User"]] = None,
log_severity: int = logging.DEBUG,
raw_text: Optional[str] = None,
**kwargs,
) -> None:
if text is None:
logger.warning("Unable to echo message: 'text' value is missing.")
Expand Down

0 comments on commit 75f8ec3

Please sign in to comment.