From 30601a3c6a09e826bb0df41711cebfb0a551464e Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Tue, 28 Oct 2025 20:01:01 +0100 Subject: [PATCH] Clean up remnants of previous prefix-focused log line formatting logic. --- src/ircplom/client_tui.py | 37 ++++++++++++++++++++++++------------- src/ircplom/testing.py | 19 ++++++++++++++++--- src/ircplom/tui_base.py | 33 ++++++++++++++++++--------------- 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/src/ircplom/client_tui.py b/src/ircplom/client_tui.py index 92806d5..eca0d32 100644 --- a/src/ircplom/client_tui.py +++ b/src/ircplom/client_tui.py @@ -7,7 +7,7 @@ from typing import Any, Callable, Optional, Sequence # ourselves from ircplom.tui_base import ( BaseTui, FormattingString, PromptWidget, TuiEvent, Window, - CMD_SHORTCUTS, LOG_FMT_ATTRS) + CMD_SHORTCUTS, LOG_FMT_ATTRS, LOG_FMT_TAG_ALERT) from ircplom.client import ( AutoAttrMixin, Channel, ChatMessage, Client, ClientQueueMixin, Dict, DictItem, ImplementationFail, IrcConnSetup, NewClientEvent, NickUserHost, @@ -26,6 +26,8 @@ _LOG_PREFIX_SERVER = '$' _LOG_PREFIX_OUT = '>' LOG_PREFIX_IN = '<' +_LOG_FMT_TAG_HIGHLIGHT = 'highlight' +LOG_FMT_ATTRS[_LOG_FMT_TAG_HIGHLIGHT] = ('reverse',) LOG_FMT_ATTRS[LOG_PREFIX_IN] = ('bright_white',) LOG_FMT_ATTRS[_LOG_PREFIX_OUT] = ('bright_green',) LOG_FMT_ATTRS[_LOG_PREFIX_SERVER] = ('bright_yellow',) @@ -439,15 +441,19 @@ class _ClientWindowsManager: escape=True ) -> None: 'From parsing scope, kwargs, build prefix before sending to logger.' - prefix = '$' - if out is not None: - prefix = _LOG_PREFIX_OUT if out else LOG_PREFIX_IN - kwargs = {'alert': True} if alert else {} - kwargs |= {'target': target} if target else {} + formatting_tags = [LOG_FMT_TAG_ALERT] if alert else [] for word in [word for word in self._to_highlight if word in str(msg)]: - kwargs['highlight'] = True + formatting_tags += [_LOG_FMT_TAG_HIGHLIGHT] break - self._tui_log(msg, scope=scope, prefix=prefix, escape=escape, **kwargs) + kwargs = {'target': target} if target else {} + self._tui_log( + msg, + formatting_tags=tuple(formatting_tags), + scope=scope, + prefix_char=(_LOG_PREFIX_SERVER if out is None + else (_LOG_PREFIX_OUT if out else LOG_PREFIX_IN)), + escape=escape, + **kwargs) def update_db(self, update: _Update) -> bool: 'Apply update to .db, and if changing anything, log and trigger.' @@ -485,8 +491,9 @@ class ClientKnowingTui(Client): self._put(TuiEvent.affector(method_name).kw(**kwargs)) def _tui_alert_trigger(self, msg: str) -> None: - self._tui_trigger('log', msg=msg, prefix=_LOG_PREFIX_SERVER, - alert=True) + self._tui_trigger('log', msg=msg, + prefix_char=_LOG_PREFIX_SERVER, + formatting_tags=(LOG_FMT_TAG_ALERT,)) self._tui_trigger('redraw_affected') def _client_tui_trigger(self, todo: str, **kwargs) -> None: @@ -541,9 +548,13 @@ class ClientKnowingTui(Client): def _log(self, msg: str, alert=False, target='', out: Optional[bool] = None ) -> None: - scope = _LogScope.CHAT if target else _LogScope.DEBUG - self._client_tui_trigger('log', scope=scope, msg=msg, alert=alert, - target=target, out=out) + self._client_tui_trigger( + 'log', + scope=_LogScope.CHAT if target else _LogScope.DEBUG, + msg=msg, + alert=alert, + target=target, + out=out) def _on_update(self, *path) -> None: for path, value in self.db.into_endnode_updates(path): diff --git a/src/ircplom/testing.py b/src/ircplom/testing.py index cafc95f..6ddb1c2 100644 --- a/src/ircplom/testing.py +++ b/src/ircplom/testing.py @@ -435,7 +435,13 @@ class TestingClientTui(ClientTui): self._clients += [client] return client - def log(self, msg: str, escape=True, **kwargs) -> None: + def log(self, + msg: str, + formatting_tags=tuple(), + prefix_char: Optional[str] = None, + escape=True, + **kwargs + ) -> None: def test_after(cmd_name: str, args: tuple[str, ...], ret) -> None: assert cmd_name == _MARK_LOG, f'WANTED {_MARK_LOG}, GOT {cmd_name}' win_ids, logged = ret @@ -453,8 +459,15 @@ class TestingClientTui(ClientTui): assert args[1] == msg_sans_time, info assert expected_win_ids == win_ids, info - self._playbook.test_wrap(None, test_after, super().log, - msg, escape=escape, **kwargs) + self._playbook.test_wrap( + test_before=None, + test_after=test_after, + f=super().log, + msg=msg, + formatting_tags=formatting_tags, + prefix_char=prefix_char, + escape=escape, + **kwargs) def cmd__prompt_enter(self) -> None: def test_before(cmd_name: str, args: tuple[str, ...]) -> None: diff --git a/src/ircplom/tui_base.py b/src/ircplom/tui_base.py index fb1fc02..208220f 100644 --- a/src/ircplom/tui_base.py +++ b/src/ircplom/tui_base.py @@ -15,13 +15,9 @@ from ircplom.events import AffectiveEvent, Loop, QueueMixin, QuitEvent _LOG_PREFIX_DEFAULT = '#' LOG_FMT_SEP = ' ' -LOG_FMT_NONE = '.' -LOG_FMT_ALERT = '!' -LOG_FMT_HIGHLIGHT = '*' +LOG_FMT_TAG_ALERT = 'alert' LOG_FMT_ATTRS: dict[str, tuple[str, ...]] = { - LOG_FMT_NONE: tuple(), - LOG_FMT_HIGHLIGHT: ('reverse',), - LOG_FMT_ALERT: ('bold', 'bright_red'), + LOG_FMT_TAG_ALERT: ('bold', 'bright_red'), _LOG_PREFIX_DEFAULT: ('bright_cyan',), } _WRAP_INDENT = 2 @@ -669,19 +665,24 @@ class BaseTui(QueueMixin): # separated to serve as hook for subclass window selection return [self.window] - def log(self, msg: str, escape=True, **kwargs + def log(self, + msg: str, + formatting_tags=tuple(), + prefix_char: Optional[str] = None, + escape=True, + **kwargs ) -> Optional[tuple[tuple[int, ...], str]]: 'Write with timestamp, prefix to what window ._log_target_wins offers.' if escape: msg = FormattingString(msg).escape() - prefix = kwargs.get('prefix', _LOG_PREFIX_DEFAULT) + if prefix_char is None: + prefix_char = _LOG_PREFIX_DEFAULT now = str(datetime.now()) today, time = now[:10], now[11:19] - msg = f'{prefix}{LOG_FMT_SEP}{time} {msg}' - msg_attrs: list[str] = list(LOG_FMT_ATTRS.get(prefix[0], tuple())) - for t in (('alert', LOG_FMT_ALERT), ('highlight', LOG_FMT_HIGHLIGHT)): - if kwargs.get(t[0], False): - msg_attrs += list(LOG_FMT_ATTRS.get(t[1], tuple())) + msg = f'{prefix_char}{LOG_FMT_SEP}{time} {msg}' + msg_attrs: list[str] = list(LOG_FMT_ATTRS[prefix_char]) + for tag in formatting_tags: + msg_attrs += list(LOG_FMT_ATTRS.get(tag, tuple())) msg = FormattingString(msg).attrd(*msg_attrs) affected_win_indices = [] for win in self._log_target_wins(**kwargs): @@ -774,7 +775,8 @@ class BaseTui(QueueMixin): elif len(typed_in) == 1: self.window.prompt.insert(typed_in) else: - self.log(f'unknown keyboard input: {typed_in}', alert=True) + self.log(f'unknown keyboard input: {typed_in}', + (LOG_FMT_TAG_ALERT,)) self.redraw_affected() def cmd__prompt_enter(self) -> None: @@ -809,7 +811,8 @@ class BaseTui(QueueMixin): else: alert = 'not prefixed by /' if alert: - self.log(f'invalid prompt command: {alert}', alert=True) + self.log(f'invalid prompt command: {alert}', + (LOG_FMT_TAG_ALERT,)) def cmd__help(self) -> None: 'Print available commands.' -- 2.30.2