home · contact · privacy
Limit depth of log history buffer. master
authorChristian Heller <c.heller@plomlompom.de>
Tue, 7 Oct 2025 08:20:11 +0000 (10:20 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 7 Oct 2025 08:20:11 +0000 (10:20 +0200)
src/ircplom/tui_base.py

index 8b5028ee8a87b2393e5f10b20ba9f24d0645cc23..c82eddf34d132065f397680248029a1862a2ff55 100644 (file)
@@ -24,6 +24,7 @@ LOG_FMT_ATTRS: dict[str, tuple[str, ...]] = {
     LOG_FMT_ALERT: ('bold', 'bright_red'),
     _LOG_PREFIX_DEFAULT: ('bright_cyan',),
 }
     LOG_FMT_ALERT: ('bold', 'bright_red'),
     _LOG_PREFIX_DEFAULT: ('bright_cyan',),
 }
+_LOG_MAX_LEN = 1000
 
 _MIN_HEIGHT = 4
 _MIN_WIDTH = 32
 
 _MIN_HEIGHT = 4
 _MIN_WIDTH = 32
@@ -115,12 +116,13 @@ class _ScrollableWidget(_Widget):
 
 class _HistoryWidget(_ScrollableWidget):
     _history_idx_neg = -1
 
 class _HistoryWidget(_ScrollableWidget):
     _history_idx_neg = -1
+    _wrapped_idx_neg: int = -1
+    _history_offset: int = 0
     _UNSET_HISTORY_IDX_POS: int = -1
     _COMPARAND_HISTORY_IDX_POS: int = -2
     _BOOKMARK_HISTORY_IDX_POS: int = -3
     _PADDING_HISTORY_IDX_POS: int = -4
     _newest_read_history_idx_pos: int
     _UNSET_HISTORY_IDX_POS: int = -1
     _COMPARAND_HISTORY_IDX_POS: int = -2
     _BOOKMARK_HISTORY_IDX_POS: int = -3
     _PADDING_HISTORY_IDX_POS: int = -4
     _newest_read_history_idx_pos: int
-    _wrapped_idx_neg: int
     _y_pgscroll: int
 
     def __init__(self, wrap: Callable[[str], list[str]], **kwargs) -> None:
     _y_pgscroll: int
 
     def __init__(self, wrap: Callable[[str], list[str]], **kwargs) -> None:
@@ -131,9 +133,14 @@ class _HistoryWidget(_ScrollableWidget):
 
     def _add_wrapped(self, history_idx_pos: int, line: str) -> int:
         wrapped_lines = self._wrap(line)
 
     def _add_wrapped(self, history_idx_pos: int, line: str) -> int:
         wrapped_lines = self._wrap(line)
-        self._wrapped += [(history_idx_pos, line) for line in wrapped_lines]
+        self._wrapped += [(self._history_offset + history_idx_pos, line)
+                          for line in wrapped_lines]
         return len(wrapped_lines)
 
         return len(wrapped_lines)
 
+    @property
+    def _len_full_history(self) -> int:
+        return self._history_offset + len(self._history)
+
     def set_geometry(self, sizes: _YX) -> None:
         super().set_geometry(sizes)
         if self._drawable:
     def set_geometry(self, sizes: _YX) -> None:
         super().set_geometry(sizes)
         if self._drawable:
@@ -147,7 +154,7 @@ class _HistoryWidget(_ScrollableWidget):
                 -1 if (not self._wrapped)
                 else (-len(self._wrapped)
                       + self._last_wrapped_idx_pos_for_hist_idx_pos(
                 -1 if (not self._wrapped)
                 else (-len(self._wrapped)
                       + self._last_wrapped_idx_pos_for_hist_idx_pos(
-                          self._history_idx_neg + len(self._history))))
+                          self._len_full_history + self._history_idx_neg)))
             self.bookmark()
 
     def append(self, to_append: str) -> None:
             self.bookmark()
 
     def append(self, to_append: str) -> None:
@@ -159,6 +166,17 @@ class _HistoryWidget(_ScrollableWidget):
             n_wrapped = self._add_wrapped(len(self._history) - 1, to_append)
             if self._wrapped_idx_neg < -1:
                 self._wrapped_idx_neg -= n_wrapped
             n_wrapped = self._add_wrapped(len(self._history) - 1, to_append)
             if self._wrapped_idx_neg < -1:
                 self._wrapped_idx_neg -= n_wrapped
+        if len(self._history) > _LOG_MAX_LEN:
+            self._history = self._history[1:]
+            self._history_offset += 1
+            wrap_offset = 0
+            for wrap_idx_pos, t in enumerate(self._wrapped):
+                if t[0] == self._history_offset:
+                    wrap_offset = wrap_idx_pos
+                    break
+            self._wrapped = self._wrapped[wrap_offset:]
+            self._wrapped_idx_neg = max(self._wrapped_idx_neg,
+                                        -len(self._wrapped))
 
     def _draw(self) -> None:
         add_scroll_info = self._wrapped_idx_neg < -1
 
     def _draw(self) -> None:
         add_scroll_info = self._wrapped_idx_neg < -1
@@ -223,7 +241,8 @@ class _HistoryWidget(_ScrollableWidget):
     @property
     def n_lines_unread(self) -> int:
         'How many new lines have been logged since last focus.'
     @property
     def n_lines_unread(self) -> int:
         'How many new lines have been logged since last focus.'
-        return len(self._history) - (self._newest_read_history_idx_pos + 1)
+        return (self._len_full_history
+                - (self._newest_read_history_idx_pos + 1))
 
     def _scroll(self, up: bool = True) -> None:
         super()._scroll(up)
 
     def _scroll(self, up: bool = True) -> None:
         super()._scroll(up)
@@ -236,7 +255,7 @@ class _HistoryWidget(_ScrollableWidget):
                 self._wrapped_idx_neg = min(
                         -1, self._wrapped_idx_neg + self._y_pgscroll)
             self._history_idx_neg\
                 self._wrapped_idx_neg = min(
                         -1, self._wrapped_idx_neg + self._y_pgscroll)
             self._history_idx_neg\
-                = (-len(self._history)
+                = (-self._len_full_history
                    + max(0, self._wrapped[self._wrapped_idx_neg][0]))
 
 
                    + max(0, self._wrapped[self._wrapped_idx_neg][0]))