home · contact · privacy
Simplify window scoping.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 23 Sep 2025 19:17:13 +0000 (21:17 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 23 Sep 2025 19:17:13 +0000 (21:17 +0200)
ircplom/client_tui.py

index 39ec96db1ffdb7fa782732e3f62dd6fc10964382..41ea1e1a5983b454c6ecfbf6a2a16ce86110f38f 100644 (file)
@@ -325,29 +325,37 @@ class _ClientWindowsManager:
         self.windows += [win]
         return win
 
-    def window(self, scope: _LogScope, chatname: str = '') -> _ClientWindow:
-        'Return client window of scope.'
-        for win in [w for w in self.windows if w.scope == scope]:
-            if scope == _LogScope.CHAT:
-                if isinstance(win, _ChatWindow) and win.chatname == chatname:
-                    return win
-                continue
-            return win
-        return self._new_win(scope=scope, chatname=chatname)
-
-    def windows_for_userid(self, user_id: str, w_channels=True
-                           ) -> list[_ClientWindow]:
-        'Return windows interacting with userid (all if "me").'
-        chan_names = [c for c, v in self.db.channels.items()
-                      if user_id in v.user_ids]
-        return [w for w in self.windows
-                if ((w_channels
-                     and isinstance(w, _ChannelWindow)
-                     and w.chatname in chan_names)
-                    or (isinstance(w, _QueryWindow)
-                        and (user_id == 'me' or w.chatname in {
-                            self.db.users[user_id].nick,
-                            self.db.users[user_id].prev_nick})))]
+    def windows_for(self, scope: _LogScope, id_='') -> list[_ClientWindow]:
+        'Return client windows of scope, and additional potential identifier.'
+        ret = []
+        if scope == _LogScope.ALL:
+            ret = [w for w in self.windows
+                   if w not in self.windows_for(_LogScope.DEBUG)]
+        elif scope == _LogScope.RAW:
+            ret = [w for w in self.windows if w.scope == _LogScope.RAW]
+        elif scope == _LogScope.DEBUG:
+            ret = self.windows_for(_LogScope.RAW)\
+                    + [w for w in self.windows if w.scope == _LogScope.DEBUG]
+        elif scope == _LogScope.CHAT:
+            ret = [w for w in self.windows
+                   if isinstance(w, _ChatWindow) and w.chatname == id_]
+            if (not ret):
+                ret += [self._new_win(_LogScope.CHAT, chatname=id_)]
+        elif scope == _LogScope.USER:
+            chan_names = [c for c, v in self.db.channels.items()
+                          if id_ in v.user_ids]
+            ret = [w for w in self.windows
+                   if (isinstance(w, _ChannelWindow)
+                       and w.chatname in chan_names)
+                   or (isinstance(w, _QueryWindow)
+                       and (id_ == 'me' or w.chatname in {
+                           self.db.users[id_].nick,
+                           self.db.users[id_].prev_nick}))]
+        elif scope == _LogScope.USER_NO_CHANNELS:
+            ret = [w for w in self.windows_for(_LogScope.USER, id_)
+                   if isinstance(w, _QueryWindow)]
+        ret.sort(key=lambda w: w.idx)
+        return ret
 
     def log(self, msg: str, scope: _LogScope, **kwargs) -> None:
         'From parsing scope, kwargs, build prefix before sending to logger.'
@@ -422,21 +430,8 @@ class ClientTui(BaseTui):
 
     def _log_target_wins(self, **kwargs) -> Sequence[Window]:
         if (scope := kwargs.get('scope', None)):
-            m = self._client_mngrs[kwargs['client_id']]
-            if scope == _LogScope.ALL:
-                return [w for w in m.windows
-                        if w.scope not in {_LogScope.DEBUG, _LogScope.RAW}]
-            if scope == _LogScope.DEBUG:
-                return [m.window(_LogScope.DEBUG), m.window(_LogScope.RAW)]
-            if scope == _LogScope.CHAT:
-                return [m.window(_LogScope.CHAT,
-                                 chatname=kwargs['log_target'])]
-            if scope == _LogScope.USER:
-                return m.windows_for_userid(kwargs['log_target'])
-            if scope == _LogScope.USER_NO_CHANNELS:
-                return m.windows_for_userid(kwargs['log_target'],
-                                            w_channels=False)
-            return [m.window(scope)]
+            return self._client_mngrs[kwargs['client_id']].windows_for(
+                    scope, kwargs.get('log_target', ''))
         return super()._log_target_wins(**kwargs)
 
     def log(self, msg: str, **kwargs) -> tuple[tuple[int, ...], str]: