home · contact · privacy
Remove LogScope.SAME with its handling's unnecessary round-trips.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 23 Sep 2025 14:29:17 +0000 (16:29 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 23 Sep 2025 14:29:17 +0000 (16:29 +0200)
ircplom/client.py
ircplom/client_tui.py
ircplom/testing.py
ircplom/tui_base.py

index ed7ffe134a7257bd2af728ab9d13955e14f60543..d48e37efdb78c373c192284abc44f9ad156389d9 100644 (file)
@@ -349,7 +349,6 @@ class LogScope(Enum):
     CHAT = auto()
     USER = auto()
     USER_NO_CHANNELS = auto()
-    SAME = auto()
 
 
 @dataclass
index 25a6470008a0a45e790b334d304837a481bf185d..27e171e79556a5eec860ff7087b629c5dc181729 100644 (file)
@@ -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()
 
index beffff5c22480c60c0130dc8a413f0b8c8a5f8fc..1edc1eaaf9b13a5a8ef26f4978434ee643c96e2a 100644 (file)
@@ -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:]:
index db50eed14864b26a55e3da51a3cc6a7d51d7bb2a..7fa0e78a203c104ee6f2ec2705305219cdb887a7 100644 (file)
@@ -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.'