fix whitespace

This commit is contained in:
evilchili 2023-07-04 11:17:28 -07:00
parent 6ad0ab68da
commit b580cd2532

View File

@ -10,6 +10,7 @@ COMMANDS = defaultdict(dict)
Command = namedtuple("Commmand", "prompt,handler,usage,completer") Command = namedtuple("Commmand", "prompt,handler,usage,completer")
def register_command(handler, usage, completer=None): def register_command(handler, usage, completer=None):
prompt = handler.__qualname__.split(".", -1)[0] prompt = handler.__qualname__.split(".", -1)[0]
cmd = handler.__name__ cmd = handler.__name__
@ -21,6 +22,7 @@ def register_command(handler, usage, completer=None):
completer=completer, completer=completer,
) )
def command(usage, completer=None, binding=None): def command(usage, completer=None, binding=None):
def decorator(func): def decorator(func):
register_command(func, usage, completer) register_command(func, usage, completer)
@ -31,6 +33,7 @@ def command(usage, completer=None, binding=None):
return wrapper return wrapper
return decorator return decorator
class BasePrompt(NestedCompleter): class BasePrompt(NestedCompleter):
def __init__(self, cache={}): def __init__(self, cache={}):
super(BasePrompt, self).__init__(self._nested_completer_map()) super(BasePrompt, self).__init__(self._nested_completer_map())
@ -42,27 +45,34 @@ class BasePrompt(NestedCompleter):
self._subshells = {} self._subshells = {}
self._cache = cache self._cache = cache
self._name = "Interactive Shell" self._name = "Interactive Shell"
def _register_subshells(self): def _register_subshells(self):
for subclass in BasePrompt.__subclasses__(): for subclass in BasePrompt.__subclasses__():
if subclass.__name__ == self.__class__.__name__: if subclass.__name__ == self.__class__.__name__:
continue continue
self._subshells[subclass.__name__] = subclass(parent=self) self._subshells[subclass.__name__] = subclass(parent=self)
def _nested_completer_map(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): def _get_help(self, cmd=None):
try: try:
return dedent(COMMANDS[self.__class__.__name__][cmd].usage) return dedent(COMMANDS[self.__class__.__name__][cmd].usage)
except KeyError: except KeyError:
return self.usage return self.usage
@property @property
def name(self): def name(self):
return self._name return self._name
@property @property
def cache(self): def cache(self):
return self._cache return self._cache
@property @property
def key_bindings(self): def key_bindings(self):
return self._key_bindings return self._key_bindings
@property @property
def usage(self): def usage(self):
text = dedent(f""" text = dedent(f"""
@ -76,36 +86,41 @@ class BasePrompt(NestedCompleter):
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" text += f" [b]{name:10s}[/b] {cmd.handler.__doc__.strip()}\n"
return text return text
@property @property
def commands(self): def commands(self):
return COMMANDS[self.__class__.__name__] return COMMANDS[self.__class__.__name__]
@property @property
def console(self): def console(self):
if not self._console: if not self._console:
self._console = Console(color_system="truecolor") self._console = Console(color_system="truecolor")
return self._console return self._console
@property @property
def prompt(self): def prompt(self):
return self._prompt return self._prompt
@property @property
def autocomplete_values(self): def autocomplete_values(self):
return list(self.commands.keys()) return list(self.commands.keys())
@property @property
def toolbar(self): def toolbar(self):
return self._toolbar return self._toolbar
@property
def key_bindings(self):
return self._key_bindings
def help(self, parts): def help(self, parts):
attr = None attr = None
if parts: if parts:
attr = parts[0] attr = parts[0]
self.console.print(self._get_help(attr)) self.console.print(self._get_help(attr))
return True return True
def process(self, cmd, *parts): def process(self, cmd, *parts):
if cmd in self.commands: if cmd in self.commands:
return self.commands[cmd].handler(self, parts) return self.commands[cmd].handler(self, parts)
self.console.error(f"Command {cmd} not understood; try 'help' for help.") self.console.error(f"Command {cmd} not understood; try 'help' for help.")
def start(self, cmd=None): def start(self, cmd=None):
while True: while True:
if not cmd: if not cmd: