dnd/deadsands/site_tools/shell.py

33 lines
1.0 KiB
Python
Raw Normal View History

2023-07-03 14:14:03 -07:00
from enum import EnumMeta
from inspect import signature
2023-07-04 10:59:51 -07:00
from prompt_toolkit import PromptSession
from prompt_toolkit.completion import FuzzyWordCompleter, NestedCompleter
2023-07-03 14:14:03 -07:00
from rich import print
2023-07-04 10:59:51 -07:00
from site_tools.cli import app
2023-07-03 14:14:03 -07:00
2023-07-04 10:59:51 -07:00
def dmsh():
2023-07-03 14:14:03 -07:00
session = PromptSession()
def cmd2dict(cmd):
sig = signature(cmd)
if not sig.parameters:
return None
cmds = {}
2023-07-04 10:59:51 -07:00
for k, v in list(sig.parameters.items()):
2023-07-03 14:14:03 -07:00
print(v, dir(v))
if v.annotation.__class__ == EnumMeta:
cmds[k] = FuzzyWordCompleter([e.value for e in v.annotation])
else:
cmds[k] = None
return cmds
2023-07-04 10:59:51 -07:00
commands = dict(site=dict((c.callback.__name__, cmd2dict(c.callback)) for c in app.registered_commands))
2023-07-03 14:14:03 -07:00
print(commands)
completer = NestedCompleter.from_nested_dict(commands)
text = session.prompt("DM> ", completer=completer, complete_while_typing=True, enable_history_search=False)
words = text.split()
print(f"You said {words}")