mirror of https://github.com/tuhiproject/tuhi.git
kete/prompt: add command completion
allows to be much more efficient with the prompt. I wonder if we can not reuse the argparse facilities somehowpull/41/head
parent
08bdaffd89
commit
e7a38c3bdc
|
@ -67,6 +67,12 @@ ORG_FREEDESKTOP_TUHI1_MANAGER = 'org.freedesktop.tuhi1.Manager'
|
||||||
ORG_FREEDESKTOP_TUHI1_DEVICE = 'org.freedesktop.tuhi1.Device'
|
ORG_FREEDESKTOP_TUHI1_DEVICE = 'org.freedesktop.tuhi1.Device'
|
||||||
ROOT_PATH = '/org/freedesktop/tuhi1'
|
ROOT_PATH = '/org/freedesktop/tuhi1'
|
||||||
|
|
||||||
|
# remove ':' from the completer delimiters of readline so we can match on
|
||||||
|
# device addresses
|
||||||
|
completer_delims = readline.get_completer_delims()
|
||||||
|
completer_delims = completer_delims.replace(':', '')
|
||||||
|
readline.set_completer_delims(completer_delims)
|
||||||
|
|
||||||
|
|
||||||
class DBusError(Exception):
|
class DBusError(Exception):
|
||||||
def __init__(self, message):
|
def __init__(self, message):
|
||||||
|
@ -624,6 +630,23 @@ class TuhiKeteShell(cmd.Cmd):
|
||||||
def help_listen(self):
|
def help_listen(self):
|
||||||
self.do_listen('-h')
|
self.do_listen('-h')
|
||||||
|
|
||||||
|
def complete_listen(self, text, line, begidx, endidx):
|
||||||
|
# mark the end of the line so we can match on the number of fields
|
||||||
|
if line.endswith(' '):
|
||||||
|
line += 'm'
|
||||||
|
fields = line.split()
|
||||||
|
|
||||||
|
completion = []
|
||||||
|
if len(fields) == 2:
|
||||||
|
for device in self._manager.devices:
|
||||||
|
if device.address.startswith(text):
|
||||||
|
completion.append(device.address)
|
||||||
|
elif len(fields) == 3:
|
||||||
|
for v in ('on', 'off'):
|
||||||
|
if v.startswith(text):
|
||||||
|
completion.append(v)
|
||||||
|
return completion
|
||||||
|
|
||||||
def do_listen(self, args):
|
def do_listen(self, args):
|
||||||
desc = '''Enable or disable listening on the given device. When
|
desc = '''Enable or disable listening on the given device. When
|
||||||
listening, all drawings are downloaded from the device as they
|
listening, all drawings are downloaded from the device as they
|
||||||
|
@ -675,6 +698,39 @@ class TuhiKeteShell(cmd.Cmd):
|
||||||
def help_fetch(self):
|
def help_fetch(self):
|
||||||
self.do_fetch('-h')
|
self.do_fetch('-h')
|
||||||
|
|
||||||
|
def complete_fetch(self, text, line, begidx, endidx):
|
||||||
|
# mark the end of the line so we can match on the number of fields
|
||||||
|
if line.endswith(' '):
|
||||||
|
line += 'm'
|
||||||
|
fields = line.split()
|
||||||
|
|
||||||
|
completion = []
|
||||||
|
if len(fields) == 2:
|
||||||
|
for device in self._manager.devices:
|
||||||
|
if device.address.startswith(text):
|
||||||
|
completion.append(device.address)
|
||||||
|
|
||||||
|
elif len(fields) == 3:
|
||||||
|
device = None
|
||||||
|
for d in self._manager.devices:
|
||||||
|
if d.address == fields[1]:
|
||||||
|
device = d
|
||||||
|
break
|
||||||
|
|
||||||
|
if device is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
timestamps = [str(t) for t in d.drawings_available]
|
||||||
|
timestamps.append('all')
|
||||||
|
|
||||||
|
logger.error(f'{device} / {timestamps}')
|
||||||
|
|
||||||
|
for t in timestamps:
|
||||||
|
if t.startswith(text):
|
||||||
|
completion.append(t)
|
||||||
|
|
||||||
|
return completion
|
||||||
|
|
||||||
def do_fetch(self, args):
|
def do_fetch(self, args):
|
||||||
def is_index_or_all(string):
|
def is_index_or_all(string):
|
||||||
try:
|
try:
|
||||||
|
@ -714,6 +770,20 @@ class TuhiKeteShell(cmd.Cmd):
|
||||||
def help_search(self):
|
def help_search(self):
|
||||||
self.do_search('-h')
|
self.do_search('-h')
|
||||||
|
|
||||||
|
def complete_search(self, text, line, begidx, endidx):
|
||||||
|
# mark the end of the line so we can match on the number of fields
|
||||||
|
if line.endswith(' '):
|
||||||
|
line += 'm'
|
||||||
|
fields = line.split()
|
||||||
|
|
||||||
|
completion = []
|
||||||
|
if len(fields) == 2:
|
||||||
|
for v in ('on', 'off'):
|
||||||
|
if v.startswith(text):
|
||||||
|
completion.append(v)
|
||||||
|
|
||||||
|
return completion
|
||||||
|
|
||||||
def do_search(self, args):
|
def do_search(self, args):
|
||||||
desc = '''
|
desc = '''
|
||||||
Start/Stop listening for devices that can be paired with the daemon.
|
Start/Stop listening for devices that can be paired with the daemon.
|
||||||
|
@ -742,6 +812,20 @@ class TuhiKeteShell(cmd.Cmd):
|
||||||
def help_pair(self):
|
def help_pair(self):
|
||||||
self.do_pair('-h')
|
self.do_pair('-h')
|
||||||
|
|
||||||
|
def complete_pair(self, text, line, begidx, endidx):
|
||||||
|
# mark the end of the line so we can match on the number of fields
|
||||||
|
if line.endswith(' '):
|
||||||
|
line += 'm'
|
||||||
|
fields = line.split()
|
||||||
|
|
||||||
|
completion = []
|
||||||
|
if len(fields) == 2:
|
||||||
|
for device in self._manager.pairable_devices + self._manager.devices:
|
||||||
|
if device.address.startswith(text):
|
||||||
|
completion.append(device.address)
|
||||||
|
|
||||||
|
return completion
|
||||||
|
|
||||||
def do_pair(self, args):
|
def do_pair(self, args):
|
||||||
if not self._manager.searching and '-h' not in args.split():
|
if not self._manager.searching and '-h' not in args.split():
|
||||||
print("please call search first")
|
print("please call search first")
|
||||||
|
@ -782,6 +866,20 @@ class TuhiKeteShell(cmd.Cmd):
|
||||||
def help_info(self):
|
def help_info(self):
|
||||||
self.do_info('-h')
|
self.do_info('-h')
|
||||||
|
|
||||||
|
def complete_info(self, text, line, begidx, endidx):
|
||||||
|
# mark the end of the line so we can match on the number of fields
|
||||||
|
if line.endswith(' '):
|
||||||
|
line += 'm'
|
||||||
|
fields = line.split()
|
||||||
|
|
||||||
|
completion = []
|
||||||
|
if len(fields) == 2:
|
||||||
|
for device in self._manager.devices:
|
||||||
|
if device.address.startswith(text):
|
||||||
|
completion.append(device.address)
|
||||||
|
|
||||||
|
return completion
|
||||||
|
|
||||||
def do_info(self, args):
|
def do_info(self, args):
|
||||||
desc = '''
|
desc = '''
|
||||||
Show information about the given device. If no device is given, show
|
Show information about the given device. If no device is given, show
|
||||||
|
|
Loading…
Reference in New Issue