home · contact · privacy
Add log bookmarking.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 29 Sep 2025 02:10:40 +0000 (04:10 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 29 Sep 2025 02:10:40 +0000 (04:10 +0200)
src/ircplom/tui_base.py

index ad54537218b06bbd820275ff7c710649bf8efdd1..2ef81c777992716744ebdd1aa586d4f8840a561f 100644 (file)
@@ -106,6 +106,7 @@ class _ScrollableWidget(_Widget):
 
 class _HistoryWidget(_ScrollableWidget):
     _last_read: int = 0
+    _bookmark: int = 0
     _y_pgscroll: int
 
     def __init__(self, wrap: Callable[[str], list[str]], **kwargs) -> None:
@@ -149,21 +150,31 @@ class _HistoryWidget(_ScrollableWidget):
     def _draw(self) -> None:
         start_idx = self._wrapped_idx - self._sizes.y + 1
         end_idx = self._wrapped_idx
-        to_write = [t[1] for t in self._wrapped[start_idx:end_idx]]
+        idx_bookmark = ([idx for idx, t in enumerate(self._wrapped)
+                         if t[0] == self._bookmark]+[0])[0]
+        wrapped = (self._wrapped[:idx_bookmark+1]
+                   + [(0, '-'*self._sizes.x)]
+                   + self._wrapped[idx_bookmark+1:])
+        to_write = [t[1] for t in wrapped[start_idx:end_idx]]
         if self._wrapped_idx < -1:
             scroll_info = f'vvv [{(-1) * self._wrapped_idx}] '
             scroll_info += 'v' * (self._sizes.x - len(scroll_info))
             to_write += [scroll_info]
         else:
-            to_write += [self._wrapped[self._wrapped_idx][1]]
+            to_write += [wrapped[self._wrapped_idx][1]]
         for i, line in enumerate(to_write):
             self._write(line, i)
-        self._last_read = len(self._history)
+        hist_idx = self._wrapped[end_idx][0]
+        self._last_read = max(self._last_read, hist_idx or 0)
+
+    def bookmark(self) -> None:
+        'Store to what most recent line we have (been) scrolled.'
+        self._bookmark = self._last_read
 
     @property
     def n_lines_unread(self) -> int:
         'How many new lines have been logged since last focus.'
-        return len(self._history) - self._last_read
+        return len(self._history) - self._last_read - 1
 
     def _scroll(self, up: bool = True) -> None:
         super()._scroll(up)
@@ -498,6 +509,7 @@ class BaseTui(QueueMixin):
 
     def _switch_window(self, idx: int) -> None:
         self.window.taint()
+        self.window.history.bookmark()
         self._status_line.idx_focus = self._window_idx = idx
         self._status_line.taint()