home · contact · privacy
Add /help command to print available commands. master
authorChristian Heller <c.heller@plomlompom.de>
Wed, 6 Aug 2025 12:18:24 +0000 (14:18 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 6 Aug 2025 12:18:24 +0000 (14:18 +0200)
ircplom/tui_base.py

index f78991406de7c8f2ec0d39e21d7483606f0cb8f3..25152ca4d938c05c6f134e6a9af7e8dbb16f7b01 100644 (file)
@@ -471,6 +471,35 @@ class BaseTui(QueueMixin):
         if alert:
             self._log.alert(f'invalid prompt command: {alert}')
 
+    def cmd__help(self) -> None:
+        'Print available commands.'
+        self._log.add('commands available in this window:')
+        cmds = {}
+        cmd_prefix = 'cmd__'
+        base = 'self'
+        for path in (base, f'{base}.window', f'{base}.window.prompt'):
+            for cmd_method_name in [attr_name for attr_name in dir(eval(path))
+                                    if attr_name.startswith(cmd_prefix)]:
+                path_prefix = f'{path}.'
+                cmd_name = (path_prefix[len(base)+1:]
+                            + cmd_method_name[len(cmd_prefix):])
+                method = eval(f'{path_prefix}{cmd_method_name}')
+                args = []
+                for key, param in signature(method).parameters.items():
+                    arg = key.upper()
+                    if param.default != inspect_empty:
+                        arg = f'[{arg}]'
+                    args += [arg]
+                cmds[cmd_name] = args
+        to_log = []
+        for cmd_name, args in cmds.items():
+            for key, target in CMD_SHORTCUTS.items():
+                if target == cmd_name:
+                    cmd_name = key
+            to_log += [f'{cmd_name} {" ".join(args)}']
+        for item in sorted(to_log):
+            self._log.add(f'  /{item}')
+
     def cmd__quit(self) -> None:
         'Trigger program exit.'
         self._put(QuitEvent())