reformatting
This commit is contained in:
@@ -1,18 +1,17 @@
|
||||
import functools
|
||||
from collections import namedtuple, defaultdict
|
||||
from collections import defaultdict, namedtuple
|
||||
from textwrap import dedent
|
||||
|
||||
from prompt_toolkit.completion import NestedCompleter
|
||||
|
||||
from site_tools.console import Console
|
||||
from textwrap import dedent
|
||||
|
||||
COMMANDS = defaultdict(dict)
|
||||
|
||||
Command = namedtuple('Commmand', 'prompt,handler,usage,completer')
|
||||
|
||||
Command = namedtuple("Commmand", "prompt,handler,usage,completer")
|
||||
|
||||
def register_command(handler, usage, completer=None):
|
||||
prompt = handler.__qualname__.split('.', -1)[0]
|
||||
prompt = handler.__qualname__.split(".", -1)[0]
|
||||
cmd = handler.__name__
|
||||
if cmd not in COMMANDS[prompt]:
|
||||
COMMANDS[prompt][cmd] = Command(
|
||||
@@ -22,7 +21,6 @@ def register_command(handler, usage, completer=None):
|
||||
completer=completer,
|
||||
)
|
||||
|
||||
|
||||
def command(usage, completer=None, binding=None):
|
||||
def decorator(func):
|
||||
register_command(func, usage, completer)
|
||||
@@ -33,49 +31,38 @@ def command(usage, completer=None, binding=None):
|
||||
return wrapper
|
||||
return decorator
|
||||
|
||||
|
||||
class BasePrompt(NestedCompleter):
|
||||
|
||||
def __init__(self, cache={}):
|
||||
super(BasePrompt, self).__init__(self._nested_completer_map())
|
||||
self._prompt = ''
|
||||
self._prompt = ""
|
||||
self._console = None
|
||||
self._theme = None
|
||||
self._toolbar = None
|
||||
self._key_bindings = None
|
||||
self._subshells = {}
|
||||
self._cache = cache
|
||||
self._name = 'Interactive Shell'
|
||||
|
||||
self._name = "Interactive Shell"
|
||||
def _register_subshells(self):
|
||||
for subclass in BasePrompt.__subclasses__():
|
||||
if subclass.__name__ == self.__class__.__name__:
|
||||
continue
|
||||
self._subshells[subclass.__name__] = subclass(parent=self)
|
||||
|
||||
def _nested_completer_map(self):
|
||||
return dict(
|
||||
(cmd_name, cmd.completer) for (cmd_name, cmd) in COMMANDS[self.__class__.__name__].items()
|
||||
)
|
||||
|
||||
return dict((cmd_name, cmd.completer) for (cmd_name, cmd) in COMMANDS[self.__class__.__name__].items())
|
||||
def _get_help(self, cmd=None):
|
||||
try:
|
||||
return dedent(COMMANDS[self.__class__.__name__][cmd].usage)
|
||||
except KeyError:
|
||||
return self.usage
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def cache(self):
|
||||
return self._cache
|
||||
|
||||
@property
|
||||
def key_bindings(self):
|
||||
return self._key_bindings
|
||||
|
||||
@property
|
||||
def usage(self):
|
||||
text = dedent(f"""
|
||||
@@ -86,56 +73,45 @@ class BasePrompt(NestedCompleter):
|
||||
[title]COMMANDS[/title]
|
||||
|
||||
""")
|
||||
for (name, cmd) in sorted(self.commands.items()):
|
||||
for name, cmd in sorted(self.commands.items()):
|
||||
text += f" [b]{name:10s}[/b] {cmd.handler.__doc__.strip()}\n"
|
||||
return text
|
||||
|
||||
@property
|
||||
def commands(self):
|
||||
return COMMANDS[self.__class__.__name__]
|
||||
|
||||
@property
|
||||
def console(self):
|
||||
if not self._console:
|
||||
self._console = Console(color_system='truecolor')
|
||||
self._console = Console(color_system="truecolor")
|
||||
return self._console
|
||||
|
||||
@property
|
||||
def prompt(self):
|
||||
return self._prompt
|
||||
|
||||
@property
|
||||
def autocomplete_values(self):
|
||||
return list(self.commands.keys())
|
||||
|
||||
@property
|
||||
def toolbar(self):
|
||||
return self._toolbar
|
||||
|
||||
@property
|
||||
def key_bindings(self):
|
||||
return self._key_bindings
|
||||
|
||||
def help(self, parts):
|
||||
attr = None
|
||||
if parts:
|
||||
attr = parts[0]
|
||||
self.console.print(self._get_help(attr))
|
||||
return True
|
||||
|
||||
def process(self, cmd, *parts):
|
||||
if cmd in self.commands:
|
||||
return self.commands[cmd].handler(self, parts)
|
||||
self.console.error(f"Command {cmd} not understood; try 'help' for help.")
|
||||
|
||||
def start(self, cmd=None):
|
||||
while True:
|
||||
if not cmd:
|
||||
cmd = self.console.prompt(
|
||||
self.prompt,
|
||||
completer=self,
|
||||
bottom_toolbar=self.toolbar,
|
||||
key_bindings=self.key_bindings)
|
||||
self.prompt, completer=self, bottom_toolbar=self.toolbar, key_bindings=self.key_bindings
|
||||
)
|
||||
if cmd:
|
||||
cmd, *parts = cmd.split()
|
||||
self.process(cmd, *parts)
|
||||
|
||||
@@ -1,47 +1,43 @@
|
||||
from site_tools.shell.base import BasePrompt, command
|
||||
from rolltable.tables import RollTable
|
||||
from rich.table import Table
|
||||
from pathlib import Path
|
||||
|
||||
from prompt_toolkit.application import get_app
|
||||
from prompt_toolkit.completion import WordCompleter
|
||||
from prompt_toolkit.key_binding import KeyBindings
|
||||
from prompt_toolkit.application import get_app
|
||||
from rich.table import Table
|
||||
from rolltable.tables import RollTable
|
||||
|
||||
from site_tools.shell.base import BasePrompt, command
|
||||
|
||||
BINDINGS = KeyBindings()
|
||||
|
||||
|
||||
class DMShell(BasePrompt):
|
||||
|
||||
def __init__(self, cache={}):
|
||||
super().__init__(cache)
|
||||
self._name = "DM Shell"
|
||||
self._prompt = ['dm']
|
||||
self._toolbar = [('class:bold', ' DMSH ')]
|
||||
self._prompt = ["dm"]
|
||||
self._toolbar = [("class:bold", " DMSH ")]
|
||||
self._key_bindings = BINDINGS
|
||||
self._register_subshells()
|
||||
self._register_keybindings()
|
||||
|
||||
def _register_keybindings(self):
|
||||
self._toolbar.extend(
|
||||
[
|
||||
("", " [H]elp "),
|
||||
("", " [W]ild Magic Table "),
|
||||
("", " [Q]uit "),
|
||||
]
|
||||
)
|
||||
|
||||
self._toolbar.extend([
|
||||
('', " [H]elp "),
|
||||
('', " [W]ild Magic Table "),
|
||||
('', " [Q]uit "),
|
||||
])
|
||||
|
||||
@self.key_bindings.add('c-q')
|
||||
@self.key_bindings.add('c-d')
|
||||
@self.key_bindings.add("c-q")
|
||||
@self.key_bindings.add("c-d")
|
||||
def quit(event):
|
||||
self.quit()
|
||||
|
||||
@self.key_bindings.add('c-h')
|
||||
@self.key_bindings.add("c-h")
|
||||
def help(event):
|
||||
self.help()
|
||||
|
||||
@self.key_bindings.add('c-w')
|
||||
@self.key_bindings.add("c-w")
|
||||
def wmt(event):
|
||||
self.wmt()
|
||||
|
||||
@command(usage="""
|
||||
[title]QUIT[/title]
|
||||
|
||||
@@ -59,7 +55,6 @@ class DMShell(BasePrompt):
|
||||
get_app().exit()
|
||||
finally:
|
||||
raise SystemExit("")
|
||||
|
||||
@command(usage="""
|
||||
[title]HELP FOR THE HELP LORD[/title]
|
||||
|
||||
@@ -76,7 +71,6 @@ class DMShell(BasePrompt):
|
||||
"""
|
||||
super().help(parts)
|
||||
return True
|
||||
|
||||
@command(usage="""
|
||||
[title]INCREMENT DATE[/title]
|
||||
|
||||
@@ -91,8 +85,8 @@ class DMShell(BasePrompt):
|
||||
Increment the date by one day.
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
@command(usage="""
|
||||
@command(
|
||||
usage="""
|
||||
[title]LOCATION[/title]
|
||||
|
||||
[b]loc[/b] sets the party's location to the specified region of the Sahwat Desert.
|
||||
@@ -101,20 +95,15 @@ class DMShell(BasePrompt):
|
||||
|
||||
[link]loc LOCATION[/link]
|
||||
""",
|
||||
completer=WordCompleter([
|
||||
"The Blooming Wastes",
|
||||
"Dust River Canyon",
|
||||
"Gopher Gulch",
|
||||
"Calamity Ridge"
|
||||
]))
|
||||
completer=WordCompleter(["The Blooming Wastes", "Dust River Canyon", "Gopher Gulch", "Calamity Ridge"]),
|
||||
)
|
||||
def loc(self, parts=[]):
|
||||
"""
|
||||
Move the party to a new region of the Sahwat Desert.
|
||||
"""
|
||||
if parts:
|
||||
self.cache['location'] = (' '.join(parts))
|
||||
self.cache["location"] = " ".join(parts)
|
||||
self.console.print(f"The party is in {self.cache['location']}.")
|
||||
|
||||
@command(usage="""
|
||||
[title]OVERLAND TRAVEL[/title]
|
||||
|
||||
@@ -129,7 +118,6 @@ class DMShell(BasePrompt):
|
||||
Increment the date by one day and record
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
@command(usage="""
|
||||
[title]WILD MAGIC TABLE[/title]
|
||||
|
||||
@@ -145,18 +133,18 @@ class DMShell(BasePrompt):
|
||||
sources/sahwat_magic_table.yaml \\
|
||||
--frequency default --die 20[/link]
|
||||
""")
|
||||
def wmt(self, *parts, source='sahwat_magic_table.yaml'):
|
||||
def wmt(self, *parts, source="sahwat_magic_table.yaml"):
|
||||
"""
|
||||
Generate a Wild Magic Table for resolving spell effects.
|
||||
"""
|
||||
if 'wmt' not in self.cache:
|
||||
if "wmt" not in self.cache:
|
||||
rt = RollTable(
|
||||
[Path(f"{self.cache['table_sources_path']}/{source}").read_text()],
|
||||
frequency='default',
|
||||
frequency="default",
|
||||
die=20,
|
||||
)
|
||||
table = Table(*rt.expanded_rows[0])
|
||||
for row in rt.expanded_rows[1:]:
|
||||
table.add_row(*row)
|
||||
self.cache['wmt'] = table
|
||||
self.console.print(self.cache['wmt'])
|
||||
self.cache["wmt"] = table
|
||||
self.console.print(self.cache["wmt"])
|
||||
|
||||
Reference in New Issue
Block a user