home · contact · privacy
Move representational logic for Client.conn_setup, .caps into client_tui module.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 10 Aug 2025 11:29:10 +0000 (13:29 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 10 Aug 2025 11:29:10 +0000 (13:29 +0200)
ircplom/client.py
ircplom/client_tui.py

index 3aba1ce68a9930772a36e41d2d5d7f8f2743a7da..929a691ecdca9a19868111c0e2069aaebd7dd9cd 100644 (file)
@@ -69,14 +69,6 @@ class _ServerCapability:
     enabled: bool
     data: str
 
-    def str_for_log(self, name: str) -> str:
-        'Optimized for Client._log per-line listing.'
-        listing = '+' if self.enabled else '-'
-        listing += f' {name}'
-        if self.data:
-            listing += f' ({self.data})'
-        return listing
-
 
 class _CapsManager:
 
@@ -186,7 +178,7 @@ class Client(ABC, ClientQueueMixin):
         self._log = Logger(self._do_log).add
         self.update_login(nick_confirmed=False,
                           nickname=self.conn_setup.nickname)
-        self._log(f'connecting {self.conn_setup}')
+        self._log('connecting …', conn_setup=self.conn_setup)
         self._start_connecting()
 
     def _start_connecting(self) -> None:
@@ -221,7 +213,7 @@ class Client(ABC, ClientQueueMixin):
         self.send(IrcMessage(verb='NICK', params=(self.conn_setup.nickname,)))
 
     @abstractmethod
-    def _do_log(self, msg: str, stream: str = STREAM_SERVER) -> None:
+    def _do_log(self, msg: str, stream: str = STREAM_SERVER, **kwargs) -> None:
         'Write msg into log of stream, whatever shape that may have.'
 
     def send(self,
@@ -259,7 +251,7 @@ class Client(ABC, ClientQueueMixin):
 
     def close(self) -> None:
         'Close both recv Loop and socket.'
-        self._log(msg='disconnecting from server', stream=STREAM_ALL)
+        self._log(msg='disconnecting from server …', stream=STREAM_ALL)
         self._caps.clear()
         if self.conn:
             self.conn.close()
@@ -290,10 +282,7 @@ class Client(ABC, ClientQueueMixin):
                     if isinstance(result, str):
                         self._log(result)
                     else:
-                        for line in (['server capabilities (enabled: "+"):']
-                                     + [cap.str_for_log(c_name) for c_name, cap
-                                        in self._caps.dict.items()]):
-                            self._log(line)
+                        self._log('', caps=self._caps.dict)
                 if self._caps.sasl_wait:
                     if self.conn_setup.password:
                         self._log('trying to authenticate via SASL/plain')
index 45aa9df4ccb8ccff00a03cd2b032be60f3842ed5..46154b226a398059782a33a50705172c09935363 100644 (file)
@@ -1,6 +1,7 @@
 'TUI adaptions to Client.'
 # built-ins
 from getpass import getuser
+from dataclasses import asdict as dc_asdict
 from inspect import signature
 from typing import Callable, Optional
 # ourselves
@@ -196,12 +197,28 @@ class _ClientKnowingTui(Client):
             return
         self._start_connecting()
 
-    def _do_log(self, msg: str, stream: str = STREAM_SERVER) -> None:
-        if stream == _STREAM_SAME:
-            self._put(TuiEvent.affector('_log').kw(msg=msg))
-            self._put(TuiEvent.affector('redraw_affected'))
-        else:
-            self._client_tui_trigger('log', stream=stream, msg=msg)
+    def _do_log(self, msg: str, stream: str = STREAM_SERVER, **kwargs) -> None:
+        to_log = []
+        if msg:
+            to_log += [msg]
+        if 'caps' in kwargs:
+            to_log += ['server capabilities (enabled: "+"):']
+            for cap_name, cap in kwargs['caps'].items():
+                listing = '+' if cap.enabled else '-'
+                listing += f' {cap_name}'
+                if cap.data:
+                    listing += f' ({cap.data})'
+                to_log += [listing]
+        if 'conn_setup' in kwargs:
+            to_log += ['connection setup:']
+            for k, v in dc_asdict(kwargs['conn_setup']).items():
+                to_log += [f'  {k}: [{v}]']
+        for item in to_log:
+            if stream == _STREAM_SAME:
+                self._put(TuiEvent.affector('_log').kw(msg=item))
+                self._put(TuiEvent.affector('redraw_affected'))
+            else:
+                self._client_tui_trigger('log', stream=stream, msg=item)
 
     def update_login(self, nick_confirmed: bool, nickname: str = '') -> None:
         super().update_login(nick_confirmed, nickname)