2022-11-30 00:09:23 -08:00
|
|
|
from prompt_toolkit import prompt
|
|
|
|
from prompt_toolkit.completion import Completer, Completion
|
2022-12-16 23:24:05 -08:00
|
|
|
from prompt_toolkit import print_formatted_text, HTML
|
|
|
|
from groove.console import Console
|
|
|
|
|
2022-11-30 00:09:23 -08:00
|
|
|
|
|
|
|
|
|
|
|
class BasePrompt(Completer):
|
|
|
|
|
2022-12-16 23:24:05 -08:00
|
|
|
def __init__(self, manager=None, console=None, parent=None):
|
2022-11-30 00:09:23 -08:00
|
|
|
super(BasePrompt, self).__init__()
|
|
|
|
|
2022-12-07 23:41:49 -08:00
|
|
|
if (not manager and not parent): # pragma: no cover
|
2022-11-30 00:09:23 -08:00
|
|
|
raise RuntimeError("Must define either a database manager or a parent object.")
|
|
|
|
|
|
|
|
self._prompt = ''
|
|
|
|
self._values = []
|
|
|
|
self._parent = parent
|
|
|
|
self._manager = manager
|
2022-12-16 23:24:05 -08:00
|
|
|
self._console = None
|
|
|
|
self._theme = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def console(self):
|
|
|
|
if not self._console:
|
|
|
|
self._console = Console(color_system='truecolor')
|
|
|
|
return self._console
|
2022-11-30 00:09:23 -08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def usage(self):
|
|
|
|
return self.__class__.__name__
|
|
|
|
|
|
|
|
@property
|
|
|
|
def help_text(self):
|
|
|
|
return self.__doc__
|
|
|
|
|
|
|
|
@property
|
|
|
|
def manager(self):
|
|
|
|
if self._manager:
|
|
|
|
return self._manager
|
|
|
|
elif self._parent:
|
|
|
|
return self._parent.manager
|
|
|
|
|
|
|
|
@property
|
|
|
|
def parent(self):
|
|
|
|
return self._parent
|
|
|
|
|
|
|
|
@property
|
|
|
|
def prompt(self):
|
|
|
|
return self._prompt
|
|
|
|
|
|
|
|
@property
|
|
|
|
def values(self):
|
2022-12-10 10:20:30 -08:00
|
|
|
return self._values
|
2022-11-30 00:09:23 -08:00
|
|
|
|
|
|
|
def get_completions(self, document, complete_event):
|
|
|
|
word = document.get_word_before_cursor()
|
|
|
|
found = False
|
|
|
|
for value in self.values:
|
|
|
|
if word in value:
|
|
|
|
found = True
|
|
|
|
yield Completion(value, start_position=-len(word))
|
|
|
|
if not found:
|
|
|
|
try:
|
|
|
|
for result in self.default_completer(document, complete_event):
|
|
|
|
yield result
|
|
|
|
except NotImplementedError:
|
|
|
|
pass
|
|
|
|
|
2022-12-16 23:24:05 -08:00
|
|
|
def help(self, parts):
|
|
|
|
self.console.print(
|
|
|
|
getattr(self, parts[0]).__doc__ if parts else self.help_text
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
2022-11-30 00:09:23 -08:00
|
|
|
def start(self, cmd=''):
|
|
|
|
while True:
|
|
|
|
if not cmd:
|
2022-12-16 23:24:05 -08:00
|
|
|
cmd = self.console.prompt(self.prompt, completer=self)
|
2022-11-30 00:09:23 -08:00
|
|
|
if not cmd:
|
|
|
|
return
|
|
|
|
cmd, *parts = cmd.split()
|
|
|
|
if not self.process(cmd, *parts):
|
|
|
|
return
|
|
|
|
cmd = ''
|
|
|
|
|
|
|
|
def default_completer(self, document, complete_event):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def process(self, cmd, *parts):
|
|
|
|
raise NotImplementedError()
|