Move level-to-style mapping into MsgLevelType.style() method

Replace the module-level _LEVEL_STYLE dict in components.py with a
style() method on the MsgLevelType enum, following the project
convention of encapsulating type-bound logic on the type itself.
This commit is contained in:
Softer 2026-06-06 13:00:13 +03:00
parent 9c40f8e9ae
commit 139bd105e6
2 changed files with 13 additions and 10 deletions

View File

@ -27,14 +27,6 @@ from archinstall.tui.result import Result, ResultType
ValueT = TypeVar('ValueT')
_LEVEL_STYLE: dict[MsgLevelType, str] = {
MsgLevelType.MsgNone: '',
MsgLevelType.MsgError: 'red',
MsgLevelType.MsgWarning: 'bright_yellow',
MsgLevelType.MsgInfo: 'green',
}
def _update_preview(widget: Label, result: str | PreviewResult | list[PreviewResult] | None) -> None:
if result is None:
widget.update('')
@ -43,14 +35,14 @@ def _update_preview(widget: Label, result: str | PreviewResult | list[PreviewRes
if isinstance(result, str):
widget.update(result)
elif isinstance(result, PreviewResult):
text = Text(result.message, style=_LEVEL_STYLE[result.msg_level])
text = Text(result.message, style=result.msg_level.style())
widget.update(text)
else:
text = Text()
for i, section in enumerate(result):
if i > 0:
text.append('\n\n')
text.append(section.message, style=_LEVEL_STYLE[section.msg_level])
text.append(section.message, style=section.msg_level.style())
widget.update(text)

View File

@ -15,6 +15,17 @@ class MsgLevelType(Enum):
MsgWarning = auto()
MsgError = auto()
def style(self) -> str:
match self:
case MsgLevelType.MsgNone:
return 'white'
case MsgLevelType.MsgInfo:
return 'green'
case MsgLevelType.MsgWarning:
return 'bright_yellow'
case MsgLevelType.MsgError:
return 'red'
@dataclass
class PreviewResult: