From: Christian Heller Date: Tue, 7 Oct 2025 08:20:11 +0000 (+0200) Subject: Limit depth of log history buffer. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/day?a=commitdiff_plain;h=HEAD;p=ircplom Limit depth of log history buffer. --- diff --git a/src/ircplom/tui_base.py b/src/ircplom/tui_base.py index 8b5028e..c82eddf 100644 --- a/src/ircplom/tui_base.py +++ b/src/ircplom/tui_base.py @@ -24,6 +24,7 @@ LOG_FMT_ATTRS: dict[str, tuple[str, ...]] = { LOG_FMT_ALERT: ('bold', 'bright_red'), _LOG_PREFIX_DEFAULT: ('bright_cyan',), } +_LOG_MAX_LEN = 1000 _MIN_HEIGHT = 4 _MIN_WIDTH = 32 @@ -115,12 +116,13 @@ class _ScrollableWidget(_Widget): 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 - _wrapped_idx_neg: int _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) - 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) + @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: @@ -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( - self._history_idx_neg + len(self._history)))) + self._len_full_history + self._history_idx_neg))) 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 + 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 @@ -223,7 +241,8 @@ 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._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) @@ -236,7 +255,7 @@ class _HistoryWidget(_ScrollableWidget): 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]))