home · contact · privacy
Improve command prompt error messaging. master
authorChristian Heller <c.heller@plomlompom.de>
Sun, 1 Jun 2025 11:02:48 +0000 (13:02 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 1 Jun 2025 11:02:48 +0000 (13:02 +0200)
ircplom.py

index ce447d7d0f37475b122e58ee3550bbb1773685a8..e49a68492de736b45841620cc1e23fd7774da893 100755 (executable)
@@ -490,10 +490,11 @@ class TuiLoop(Loop):
 
     def _cmd__prompt_enter(self) -> None:
         if self._prompt:
-            success = False
+            alert: Optional[str] = None
             if len(self._prompt) > 1 and self._prompt[0] == '/':
                 toks = self._prompt[1:].split(maxsplit=1)
                 method_name = f'_cmd__{toks[0]}'
+                alert = f'{toks[0]} unknown'
                 if hasattr(self, method_name)\
                         and method_name != stack()[0].function:
                     method = getattr(self, method_name)
@@ -501,8 +502,9 @@ class TuiLoop(Loop):
                     n_args_max = len(params)
                     n_args_min = len([p for p in params.values()
                                       if p.default == inspect_empty])
+                    alert = f'{toks[0]} needs {n_args_min} - {n_args_max} args'
                     if len(toks) == 1 and not n_args_min:
-                        success = method()
+                        alert = method()
                     elif len(toks) > 1 and params\
                             and n_args_min <= len(toks[1].split()):
                         args = []
@@ -510,10 +512,11 @@ class TuiLoop(Loop):
                             toks = toks[1].split(maxsplit=1)
                             args += [toks[0]]
                             n_args_max -= 1
-                        success = method(*args)
-            if not success:
-                self.broadcast('ALERT',
-                               f'invalid prompt command: {self._prompt}')
+                        alert = method(*args)
+            else:
+                alert = 'not prefixed by /'
+            if alert:
+                self.broadcast('ALERT', f'invalid prompt command: {alert}')
         self._prompt = ''
         self._draw_prompt()
 
@@ -525,24 +528,21 @@ class TuiLoop(Loop):
         self._log.scroll_down()
         self._draw_log()
 
-    def _cmd__disconnect(self, quit_msg: str = 'ircplom says bye') -> bool:
+    def _cmd__disconnect(self, quit_msg: str = 'ircplom says bye') -> None:
         self.broadcast('SEND', IrcMessage('QUIT', [quit_msg]))
-        return True
 
-    def _cmd__quit(self) -> bool:
+    def _cmd__quit(self) -> None:
         self.broadcast('QUIT')
-        return True
 
-    def _cmd__buffer(self, buffer_idx_str: str) -> bool:
+    def _cmd__buffer(self, buffer_idx_str: str) -> Optional[str]:
         if not buffer_idx_str.isdigit():
-            return False
+            return f'not-integer buffer_idx: {buffer_idx_str}'
         buffer_idx = int(buffer_idx_str)
-        if 0 <= buffer_idx < len(self._logs):
-            self._log_selected = buffer_idx
-            self._draw_log()
-        else:
-            self.broadcast('ALERT', 'invalid buffer idx')
-        return True
+        if buffer_idx < 0 or buffer_idx > len(self._logs):
+            return f'unavailable buffer idx: {buffer_idx}'
+        self._log_selected = buffer_idx
+        self._draw_log()
+        return None
 
     def _calc_and_draw_all(self) -> None:
         self._term.clear()