home · contact · privacy
In input prompt show only what fits into window width, inform about cutoff. master
authorChristian Heller <c.heller@plomlompom.de>
Sat, 7 Jun 2025 16:40:31 +0000 (18:40 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sat, 7 Jun 2025 16:40:31 +0000 (18:40 +0200)
ircplom.py

index b754b826f3db5a6ea97da9b7b14f408b4cb0b330..1ca94fd2959681ede992374bac7ac67e9581fb7f 100755 (executable)
@@ -18,7 +18,6 @@ PORT = 6667
 TIMEOUT_CONNECT = 5
 TIMEOUT_LOOP = 0.1
 CONN_RECV_BUFSIZE = 1024
-INPUT_PROMPT = '> '
 
 KEYBINDINGS = {
     'KEY_BACKSPACE': ('window.prompt.backspace',),
@@ -444,7 +443,8 @@ class ScrollableWidget(Widget):
 
 class PromptWidget(ScrollableWidget):
     'Keyboard-controlled command input field.'
-    _start_y: int
+    _y: int
+    _width: int
 
     def __init__(self, *args, **kwargs) -> None:
         super().__init__(*args, **kwargs)
@@ -452,7 +452,7 @@ class PromptWidget(ScrollableWidget):
         self._history_idx = 0
 
     def set_geometry(self, measurements: YX) -> None:
-        self._start_y = measurements[0]
+        self._y, self._width = measurements
 
     def append(self, to_append: str) -> None:
         self._input_buffer += to_append
@@ -460,8 +460,18 @@ class PromptWidget(ScrollableWidget):
         self.draw()
 
     def draw(self) -> None:
-        self._write_yx(YX(self._start_y, 0),
-                       f'{INPUT_PROMPT} {self._input_buffer}_')
+        cursor = '_'
+        prompt_template = '> '
+        prompt = f'{prompt_template}'
+        offset = 0
+        while True:
+            to_write = f'{prompt}{self._input_buffer[offset:]}{cursor}'
+            len_too_much = len(to_write) - self._width
+            if len_too_much <= 0:
+                break
+            offset += len_too_much
+            prompt = f'<{offset}|{prompt_template}…'
+        self._write_yx(YX(self._y, 0), to_write)
 
     def _scroll(self, up: bool = True) -> None:
         if up and -(self._history_idx) < len(self._history):