LOG_FMT_ALERT: ('bold', 'bright_red'),
_LOG_PREFIX_DEFAULT: ('bright_cyan',),
}
+_LOG_MAX_LEN = 1000
_MIN_HEIGHT = 4
_MIN_WIDTH = 32
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:
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:
-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:
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
@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)
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]))