From: Christian Heller Date: Fri, 13 Jun 2025 16:52:00 +0000 (+0200) Subject: Refactor _ScrollableWidget.append usage, explicitly limit to ._history appendage. X-Git-Url: https://plomlompom.com/repos/%22https:/validator.w3.org/%7B%7Bdb.prefix%7D%7D/test.html?a=commitdiff_plain;h=d536ea45f525cb8c4a2044140996c564b6c56f28;p=ircplom Refactor _ScrollableWidget.append usage, explicitly limit to ._history appendage. --- diff --git a/ircplom/tui.py b/ircplom/tui.py index 4d3f01f..080e941 100644 --- a/ircplom/tui.py +++ b/ircplom/tui.py @@ -68,9 +68,9 @@ class _ScrollableWidget(_Widget, ABC): self._write = write self._history: list[str] = [] - @abstractmethod def append(self, to_append: str) -> None: - 'Append to widget content.' + 'Append to scrollable history.' + self._history += [to_append] @abstractmethod def _scroll(self, up=True) -> None: @@ -115,7 +115,7 @@ class _LogWidget(_ScrollableWidget): self._wrapped_idx = idx_their_last - len(self._wrapped) def append(self, to_append: str) -> None: - self._history += [to_append] + super().append(to_append) n_wrapped_lines = self._add_wrapped(len(self._history) - 1, to_append) if self._wrapped_idx < -1: self._history_idx -= 1 @@ -162,14 +162,6 @@ class _PromptWidget(_ScrollableWidget): def set_geometry(self, measurements: _YX) -> None: self._y, self._width = measurements - def append(self, to_append: str) -> None: - self._cursor_x += len(to_append) - self._input_buffer = (self._input_buffer[:self._cursor_x - 1] - + to_append - + self._input_buffer[self._cursor_x - 1:]) - self._history_idx = 0 - self.draw() - def draw(self) -> None: prefix = self._prompt[:] content = self._input_buffer[:] @@ -192,11 +184,14 @@ class _PromptWidget(_ScrollableWidget): padding=False) self._write(to_write[cursor_x_to_write + 1:]) + def _archive_prompt(self) -> None: + self.append(self._input_buffer[:]) + self._reset_buffer('') + def _scroll(self, up: bool = True) -> None: if up and -(self._history_idx) < len(self._history): if self._history_idx == 0 and self._input_buffer: - self._history += [self._input_buffer[:]] - self._reset_buffer('') + self._archive_prompt() self._history_idx -= 1 self._history_idx -= 1 self._reset_buffer(self._history[self._history_idx]) @@ -208,12 +203,16 @@ class _PromptWidget(_ScrollableWidget): else: self._reset_buffer(self._history[self._history_idx]) elif self._input_buffer: - self._history += [self._input_buffer[:]] - self._reset_buffer('') + self._archive_prompt() def cmd__append(self, to_append: str) -> None: 'Append to prompt input buffer.' - self.append(to_append) + self._cursor_x += len(to_append) + self._input_buffer = (self._input_buffer[:self._cursor_x - 1] + + to_append + + self._input_buffer[self._cursor_x - 1:]) + self._history_idx = 0 + self.draw() def cmd__backspace(self) -> None: 'Truncate current content by one character, if possible.' @@ -243,8 +242,7 @@ class _PromptWidget(_ScrollableWidget): 'Return current content while also clearing and then redrawing.' to_return = self._input_buffer[:] if to_return: - self._history += [to_return] - self._reset_buffer('') + self._archive_prompt() self.draw() return to_return