From 6e50e0861f34b9a819368a672417a3cab446d184 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 20 Feb 2026 18:16:47 +0000 Subject: [PATCH] add Renderable type annotation add {Writable}.error() for displaying error messages in red. --- src/q3rcon_tui/writable.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/q3rcon_tui/writable.py b/src/q3rcon_tui/writable.py index 4c81fee..58e32c9 100644 --- a/src/q3rcon_tui/writable.py +++ b/src/q3rcon_tui/writable.py @@ -1,9 +1,12 @@ import re from rich.table import Table +from rich.text import Text from .settings import settings +Renderable = Text | Table | str + class Writable: RE_COLOR_CODES = re.compile(r'\^[0-9]') @@ -34,20 +37,22 @@ class Writable: def remove_color_codes(s: str) -> str: return Writable.RE_COLOR_CODES.sub('', s) - def parse(self, cmd, response: str) -> str: + def parse(self, cmd, response: str, style=None) -> Renderable: response = response.removeprefix('print\n') if settings.raw: - return response + return Text(response, style=style) match cmd: case 'status': return self.status_table(response) case _: - match self.RE_CVAR.match(response): - case None: - return self.remove_color_codes(response) - case m: - return self.cvar_table(m) + if m := self.RE_CVAR.match(response): + return self.cvar_table(m) + else: + return Text(self.remove_color_codes(response), style=style) + + def error(self, message: str) -> Text: + return Text(message, style='#c73d4b') def status_table(self, status_response: str) -> Table | str: table = Table(show_header=True, header_style='bold #88c0d0')