home · contact · privacy
Refactor testing log columns output.
authorChristian Heller <c.heller@plomlompom.de>
Fri, 7 Nov 2025 18:31:07 +0000 (19:31 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Fri, 7 Nov 2025 18:31:07 +0000 (19:31 +0100)
src/ircplom/testing.py

index 1835be99df488f7274cfb630c3bd2fa939edb897..c7c06c30b2929ade4a8fbead70efa49eb365e457 100644 (file)
@@ -189,6 +189,7 @@ class _Playbook:
     assert_screen_line: Optional[Callable] = None
     redraw_affected: Optional[Callable] = None
     _lines_t: list[tuple[str, str]]
+    _colwidths: dict[str, int]
 
     def __init__(self, path: Path, get_client: Callable, verbose: bool
                  ) -> None:
@@ -311,23 +312,6 @@ class _Playbook:
                              and not line.startswith(_MARK_COMMENT)]
         expand_inserts()
         expand_parsed(_MARK_LOGSRVRMSG, split_server_put_and_log)
-        title_idx = 'line number(s)'
-        title_cmdname = 'command'
-        self._colwidths: dict[str, int] = {}
-        for idx_str, line in self._lines_t:
-            self._colwidths['idx'] = max(
-                    len(title_idx),
-                    len(idx_str),
-                    self._colwidths.get('idx', 0))
-            cmd_name, args = self._cmdname_and_args_from(line)
-            self._colwidths['cmdname'] = max(
-                    len(title_cmdname),
-                    len(cmd_name),
-                    self._colwidths.get('cmdname', 0))
-            self._colwidths['midargs'] = max(
-                    len(self._args_verbose(args[:-1])),
-                    self._colwidths.get('midargs', 0))
-        self._log_verbose_columns((title_idx, title_cmdname, 'arguments'))
 
     @staticmethod
     def _args_verbose(args: tuple[str, ...]) -> str:
@@ -349,9 +333,27 @@ class _Playbook:
         return self._lines_t[self._idx]
 
     def _log_verbose_columns(self, items: tuple[str, ...]) -> None:
+        col_titles = (
+            'line number(s)',  # = idx_str
+            'command',         # = cmd_name
+            '',                # = last two both "arguments", but empty key to
+            'arguments')       # … harvest width on non-last (middle) args
+
+        if not hasattr(self, '_colwidths'):
+            self._colwidths: dict[str, int] = {}
+            for idx_str, line in self._lines_t:
+                cmd_name, args = self._cmdname_and_args_from(line)
+                for idx, value in enumerate((idx_str,
+                                             cmd_name,
+                                             self._args_verbose(args[:-1]))):
+                    title = col_titles[idx]
+                    self._colwidths[title] = max(len(title),
+                                                 len(value),
+                                                 self._colwidths.get(title, 0))
+            self._log_verbose_columns(tuple(t for t in col_titles if t))
+
         to_print = []
-        for idx, colname in enumerate(('idx', 'cmdname', 'midargs'
-                                       )[:len(items) - 1]):
+        for idx, colname in enumerate(col_titles[:-1][:len(items) - 1]):
             msg = items[idx]
             to_print += [msg + ' ' * (self._colwidths[colname] - len(msg))]
         to_print += [items[-1]]