home · contact · privacy
Refactor _ScrollableWidget.append usage, explicitly limit to ._history appendage.
authorChristian Heller <c.heller@plomlompom.de>
Fri, 13 Jun 2025 16:52:00 +0000 (18:52 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Fri, 13 Jun 2025 16:52:00 +0000 (18:52 +0200)
ircplom/tui.py

index 4d3f01f1198f7df5e3f258d604c414678b60d52e..080e941fc807161f4c73bc1dc479a82766034b6f 100644 (file)
@@ -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