home · contact · privacy
Some code re-organization to make reasoning about it easier.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 7 Oct 2025 00:07:35 +0000 (02:07 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 7 Oct 2025 00:07:35 +0000 (02:07 +0200)
src/ircplom/tui_base.py

index b3b016de9d175a1bcdbef1688867189a27865d49..fe74f99cb6efe524e6ec1f515a5e00cb87e36f47 100644 (file)
@@ -114,19 +114,20 @@ class _ScrollableWidget(_Widget):
 
 
 class _HistoryWidget(_ScrollableWidget):
-    _last_read_idx_pos: int = 0
-    _bookmark_idx_pos: int = 0
+    _history_idx_neg = -1
+    _newest_read_history_idx: int = 0
+    _bookmark_history_idx_pos: int = 0
+    _wrapped_idx_neg: int
     _y_pgscroll: int
 
     def __init__(self, wrap: Callable[[str], list[str]], **kwargs) -> None:
         super().__init__(**kwargs)
         self._wrap = wrap
-        self._wrapped_idx_neg = self._history_idx_neg = -1
         self._wrapped: list[tuple[Optional[int], str]] = []
 
-    def _add_wrapped(self, idx_pos_original, line) -> int:
+    def _add_wrapped(self, history_idx_pos, line) -> int:
         wrapped_lines = self._wrap(line)
-        self._wrapped += [(idx_pos_original, line) for line in wrapped_lines]
+        self._wrapped += [(history_idx_pos, line) for line in wrapped_lines]
         return len(wrapped_lines)
 
     def set_geometry(self, sizes: _YX) -> None:
@@ -134,15 +135,16 @@ class _HistoryWidget(_ScrollableWidget):
         if self._drawable:
             self._y_pgscroll = self._sizes.y // 2
             self._wrapped.clear()
+            self._wrapped_idx_neg = -1
             self._wrapped += [(None, '')] * self._sizes.y
-            if self._history:
-                for idx_pos_history, line in enumerate(self._history):
-                    self._add_wrapped(idx_pos_history, line)
-                wrapped_lines_for_history_idx = [
-                        t for t in self._wrapped
-                        if t[0] == len(self._history) + self._history_idx_neg]
-                # ensure that of the full line identified by ._history_idx_neg,
-                # ._wrapped_idx_neg point to the lowest of its wrap parts
+            for history_idx_pos, line in enumerate(self._history):
+                self._add_wrapped(history_idx_pos, line)
+            # ensure that of the full line identified by ._history_idx_neg,
+            # ._wrapped_idx_neg point to the lowest of its wrap parts
+            wrapped_lines_for_history_idx = [
+                    t for t in self._wrapped
+                    if t[0] == len(self._history) + self._history_idx_neg]
+            if wrapped_lines_for_history_idx:
                 idx_pos_their_last = self._wrapped.index(
                         wrapped_lines_for_history_idx[-1])
                 self._wrapped_idx_neg = idx_pos_their_last - len(self._wrapped)
@@ -162,11 +164,12 @@ class _HistoryWidget(_ScrollableWidget):
         start_idx_neg = self._wrapped_idx_neg - self._sizes.y + 1
         add_scroll_info = self._wrapped_idx_neg < -1
         end_idx_neg = self._wrapped_idx_neg if add_scroll_info else None
-        bookmark_idx_pos = ([idx_pos for idx_pos, t in enumerate(self._wrapped)
-                             if t[0] == self._bookmark_idx_pos] + [0])[0]
-        wrapped = (self._wrapped[:bookmark_idx_pos]
+        bookmark_wrapped_idx_pos\
+            = ([idx_pos for idx_pos, t in enumerate(self._wrapped)
+                if t[0] == self._bookmark_history_idx_pos] + [0])[0]
+        wrapped = (self._wrapped[:bookmark_wrapped_idx_pos]
                    + [(0, '-' * self._sizes.x)]  # bookmark line
-                   + self._wrapped[bookmark_idx_pos:])
+                   + self._wrapped[bookmark_wrapped_idx_pos:])
 
         to_write_w_attrs: list[tuple[Optional[str], str]] = []
         prev_idx_unwrapped: Optional[int] = -1
@@ -187,13 +190,13 @@ class _HistoryWidget(_ScrollableWidget):
         for idx, line_t in enumerate(to_write_w_attrs):
             self._write(start_y=idx, attributes=line_t[0], msg=line_t[1])
 
-        hist_idx_pos = self._wrapped[(end_idx_neg or 0) - 1][0]
-        self._last_read_idx_pos = max(self._last_read_idx_pos,
-                                      hist_idx_pos or 0)
+        hist_idx_pos = self._wrapped[(end_idx_neg or 0) - 1][0] or 0
+        self._newest_read_history_idx = max(self._newest_read_history_idx,
+                                            hist_idx_pos)
 
     def bookmark(self) -> None:
         'Store to what most recent line we have (been) scrolled.'
-        self._bookmark_idx_pos = self._last_read_idx_pos + 1
+        self._bookmark_history_idx_pos = self._newest_read_history_idx + 1
 
     @property
     def has_unread_highlight(self) -> bool:
@@ -204,7 +207,7 @@ class _HistoryWidget(_ScrollableWidget):
     @property
     def n_lines_unread(self) -> int:
         'How many new lines have been logged since last focus.'
-        return len(self._history) - self._last_read_idx_pos - 1
+        return len(self._history) - self._newest_read_history_idx - 1
 
     def _scroll(self, up: bool = True) -> None:
         super()._scroll(up)
@@ -224,7 +227,7 @@ class _HistoryWidget(_ScrollableWidget):
 
 class PromptWidget(_ScrollableWidget):
     'Manages/displays keyboard input field.'
-    _history_idx_neg: int = 0
+    _history_idx_neg = 0
     _input_buffer_unsafe: str
     _cursor_x: int