home · contact · privacy
Minor refactoring.
authorChristian Heller <c.heller@plomlompom.de>
Fri, 26 Sep 2025 14:55:34 +0000 (16:55 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Fri, 26 Sep 2025 14:55:34 +0000 (16:55 +0200)
src/ircplom/testing.py

index b97e09f6b42d4d707cfab7932eb80c1e38403e37..22977a75491be8aa7c65d47f347dd5a1c2d9a20f 100644 (file)
@@ -101,7 +101,7 @@ class TestingClientTui(ClientTui):
         assert isinstance(self._term, TestTerminal)
         self._q_keypresses = self._term._q_keypresses
         with self._path_test.open('r', encoding='utf8') as f:
-            self._playbook = tuple(line[:-1] for line in f.readlines())
+            self._playbook = tuple(line.rstrip() for line in f.readlines())
         self._playbook_anchors: dict[str, int] = {}
         while True:
             self._playbook_anchors.clear()
@@ -110,12 +110,9 @@ class TestingClientTui(ClientTui):
                     self._playbook_anchors[line[1:]] = idx
             found_repeat = False
             for idx, line in enumerate(self._playbook):
-                if line[:1] in '#|' or not line.strip():
-                    continue
-                context, msg = line.split(' ', maxsplit=1)
-                if context == 'repeat':
+                if (t := self.split_active_line(line)) and t[0] == 'repeat':
                     found_repeat = True
-                    start_key, end_key = msg.split(' ')
+                    start_key, end_key = t[1].split(' ', maxsplit=1)
                     start = self._playbook_anchors[start_key] + 1
                     end = self._playbook_anchors[end_key]
                     self._playbook = (self._playbook[:idx]
@@ -128,6 +125,13 @@ class TestingClientTui(ClientTui):
         self._playbook_idx = -1
         self._play_till_next_log()
 
+    @staticmethod
+    def split_active_line(line: str) -> Optional[tuple[str, ...]]:
+        'Return two-items tuple of split line, or None if inactive one.'
+        if line[:1] in '#|' or ' ' not in line:
+            return None
+        return tuple(line.split(' ', maxsplit=1))
+
     @classmethod
     def on_file(cls, path_test: Path):
         'Return cls with ._path_test set.'
@@ -163,19 +167,18 @@ class TestingClientTui(ClientTui):
         while True:
             self._playbook_idx += 1
             line = self._playbook[self._playbook_idx]
-            if line[:1] in '#|' or not line.strip():
-                continue
-            context, msg = line.split(' ', maxsplit=1)
-            if context == '>':
-                for c in msg:
-                    self._q_keypresses.put(c)
-                self._q_keypresses.put('KEY_ENTER')
-                continue
-            if ':' in context and msg.startswith('< '):
-                client_id, win_ids = context.split(':')
-                client = self._clients[int(client_id)]
-                assert isinstance(client.conn, _FakeIrcConnection)
-                client.conn.put_server_msg(msg[2:])
-                if not win_ids:
+            if (result := self.split_active_line(line)):
+                context, msg = result
+                if context == '>':
+                    for c in msg:
+                        self._q_keypresses.put(c)
+                    self._q_keypresses.put('KEY_ENTER')
                     continue
-            break
+                if ':' in context and msg.startswith('< '):
+                    client_id, win_ids = context.split(':')
+                    client = self._clients[int(client_id)]
+                    assert isinstance(client.conn, _FakeIrcConnection)
+                    client.conn.put_server_msg(msg[2:])
+                    if not win_ids:
+                        continue
+                break