From 4b2f0e053286b000fbc30e0cb26960dc4b9d23a7 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Tue, 23 Sep 2025 16:29:17 +0200 Subject: [PATCH] Remove LogScope.SAME with its handling's unnecessary round-trips. --- ircplom/client.py | 1 - ircplom/client_tui.py | 26 ++++++++++++++++++-------- ircplom/testing.py | 4 ++-- ircplom/tui_base.py | 15 ++++++++------- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/ircplom/client.py b/ircplom/client.py index ed7ffe1..d48e37e 100644 --- a/ircplom/client.py +++ b/ircplom/client.py @@ -349,7 +349,6 @@ class LogScope(Enum): CHAT = auto() USER = auto() USER_NO_CHANNELS = auto() - SAME = auto() @dataclass diff --git a/ircplom/client_tui.py b/ircplom/client_tui.py index 25a6470..27e171e 100644 --- a/ircplom/client_tui.py +++ b/ircplom/client_tui.py @@ -361,6 +361,7 @@ class _ClientWindowsManager: self.db.recursive_set_and_report_change(update) if not update.results: return False + for scope, result in update.results: log_kwargs: dict[str, Any] = {'scope': scope} if scope in {LogScope.CHAT, LogScope.USER, @@ -372,6 +373,7 @@ class _ClientWindowsManager: log_kwargs['is_notice'] = update.value.is_notice else: log_kwargs['log_target'] = update.full_path[1] + if isinstance(result, list): msg = '' for item in result: @@ -381,6 +383,7 @@ class _ClientWindowsManager: content = str(nuh) if transform == 'NUH' else nuh.nick msg += content self.log(msg, **log_kwargs) + elif not update.full_path == ('message',): log_path = ':'.join(update.full_path) if result is None: @@ -393,6 +396,7 @@ class _ClientWindowsManager: self.log(f' {item}', **log_kwargs) else: self.log(f'{announcement} [{result}]', **log_kwargs) + for win in [w for w in self.windows if isinstance(w, _ChatWindow)]: win.set_prompt_prefix() return bool([w for w in self.windows if w.tainted]) @@ -406,8 +410,7 @@ class ClientTui(BaseTui): self._client_mngrs: dict[str, _ClientWindowsManager] = {} def _log_target_wins(self, **kwargs) -> Sequence[Window]: - scope = kwargs.get('scope', LogScope.SAME) - if scope != LogScope.SAME: + if (scope := kwargs.get('scope', None)): m = self._client_mngrs[kwargs['client_id']] if scope == LogScope.ALL: return [w for w in m.windows @@ -428,7 +431,7 @@ class ClientTui(BaseTui): 'Forward todo to appropriate _ClientWindowsManager.' if client_id not in self._client_mngrs: self._client_mngrs[client_id] = _ClientWindowsManager( - tui_log=lambda msg, **kw: self._log( + tui_log=lambda msg, **kw: self.log( msg, client_id=client_id, **kw), tui_new_window=lambda win_cls, **kw: self._new_window( win_cls, _q_out=self._q_out, client_id=client_id, **kw)) @@ -474,9 +477,16 @@ class ClientTui(BaseTui): class ClientKnowingTui(Client): 'Adapted to communicate with ClientTui.' + def _tui_trigger(self, method_name: str, **kwargs) -> None: + 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) + def _client_tui_trigger(self, todo: str, **kwargs) -> None: - self._put(TuiEvent.affector('for_client_do').kw( - client_id=self.client_id, todo=todo, **kwargs)) + self._tui_trigger('for_client_do', client_id=self.client_id, + todo=todo, **kwargs) def send_w_params_tuple(self, verb: str, params: tuple[str, ...]) -> None: 'Helper for ClientWindow to trigger .send, for it can only do kwargs.' @@ -490,15 +500,15 @@ class ClientKnowingTui(Client): raise SendFail('not sending, since not in channel') self.send('PRIVMSG', chat_target, msg) except SendFail as e: - self._log(f'{e}', scope=LogScope.SAME, alert=True) + self._tui_alert_trigger(f'{e}') else: self.db.messaging('').to(chat_target).privmsg = msg # type: ignore def reconnect(self) -> None: 'Catch /reconnect, only initiate if not connected, else complain back.' if self.conn: - self._log('not re-connecting since already connected', - scope=LogScope.SAME, alert=True) + self._tui_alert_trigger( + 'not re-connecting since already connected') return self.connect() diff --git a/ircplom/testing.py b/ircplom/testing.py index beffff5..1edc1ea 100644 --- a/ircplom/testing.py +++ b/ircplom/testing.py @@ -108,8 +108,8 @@ class TestingClientTui(ClientTui): conn_setup=conn_setup)] return self._clients[-1] - def _log(self, msg: str, **kwargs) -> tuple[tuple[int, ...], str]: - win_ids, logged_msg = super()._log(msg, **kwargs) + def log(self, msg: str, **kwargs) -> tuple[tuple[int, ...], str]: + win_ids, logged_msg = super().log(msg, **kwargs) time_str, msg_sans_time = logged_msg.split(' ', maxsplit=1) assert len(time_str) == 8 for c in time_str[:2] + time_str[3:5] + time_str[6:]: diff --git a/ircplom/tui_base.py b/ircplom/tui_base.py index db50eed..7fa0e78 100644 --- a/ircplom/tui_base.py +++ b/ircplom/tui_base.py @@ -440,7 +440,8 @@ class BaseTui(QueueMixin): # separated to serve as hook for subclass window selection return [self.window] - def _log(self, msg: str, **kwargs) -> tuple[tuple[int, ...], str]: + def log(self, msg: str, **kwargs) -> tuple[tuple[int, ...], str]: + 'Write with timestamp, prefix to what window ._log_target_wins offers.' prefix = kwargs.get('prefix', _LOG_PREFIX_DEFAULT) if kwargs.get('alert', False): prefix = _LOG_PREFIX_ALERT + prefix @@ -534,7 +535,7 @@ 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}', alert=True) self.redraw_affected() def cmd__prompt_enter(self) -> None: @@ -568,11 +569,11 @@ 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}', alert=True) def cmd__help(self) -> None: 'Print available commands.' - self._log('commands available in this window:') + self.log('commands available in this window:') to_log = [] for cmd_name, cmd_data in self._commands.items(): to_print = [cmd_name] @@ -583,13 +584,13 @@ class BaseTui(QueueMixin): to_print += [arg] to_log += [' '.join(to_print)] for item in sorted(to_log): - self._log(f' /{item}') + self.log(f' /{item}') def cmd__list(self) -> None: 'List available windows.' - self._log('windows available via /window:') + self.log('windows available via /window:') for win in self._windows: - self._log(f' {win.idx}) {win.title}') + self.log(f' {win.idx}) {win.title}') def cmd__quit(self) -> None: 'Trigger program exit.' -- 2.30.2