mirror of
https://github.com/onyx-and-iris/q3rcon-tui.git
synced 2026-02-26 03:09:09 +00:00
add Renderable type annotation
add {Writable}.error() for displaying error messages in red.
This commit is contained in:
parent
ab4898dac3
commit
6e50e0861f
@ -1,9 +1,12 @@
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
from rich.table import Table
|
from rich.table import Table
|
||||||
|
from rich.text import Text
|
||||||
|
|
||||||
from .settings import settings
|
from .settings import settings
|
||||||
|
|
||||||
|
Renderable = Text | Table | str
|
||||||
|
|
||||||
|
|
||||||
class Writable:
|
class Writable:
|
||||||
RE_COLOR_CODES = re.compile(r'\^[0-9]')
|
RE_COLOR_CODES = re.compile(r'\^[0-9]')
|
||||||
@ -34,20 +37,22 @@ class Writable:
|
|||||||
def remove_color_codes(s: str) -> str:
|
def remove_color_codes(s: str) -> str:
|
||||||
return Writable.RE_COLOR_CODES.sub('', s)
|
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')
|
response = response.removeprefix('print\n')
|
||||||
if settings.raw:
|
if settings.raw:
|
||||||
return response
|
return Text(response, style=style)
|
||||||
|
|
||||||
match cmd:
|
match cmd:
|
||||||
case 'status':
|
case 'status':
|
||||||
return self.status_table(response)
|
return self.status_table(response)
|
||||||
case _:
|
case _:
|
||||||
match self.RE_CVAR.match(response):
|
if m := self.RE_CVAR.match(response):
|
||||||
case None:
|
|
||||||
return self.remove_color_codes(response)
|
|
||||||
case m:
|
|
||||||
return self.cvar_table(m)
|
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:
|
def status_table(self, status_response: str) -> Table | str:
|
||||||
table = Table(show_header=True, header_style='bold #88c0d0')
|
table = Table(show_header=True, header_style='bold #88c0d0')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user