Fix up tab-completion to use the commands 'complete' method

This commit is contained in:
Andrew Resch 2009-04-28 01:15:12 +00:00
parent d4cfebadbc
commit 925dcd43b8
8 changed files with 58 additions and 49 deletions

View File

@ -134,14 +134,5 @@ class Command(BaseCommand):
self.console.write("{!success!}Configuration value successfully updated.")
client.core.set_config({key: val}).addCallback(on_set_config)
"""
def complete(self, text, *args):
keys = []
def _on_get_config(config):
keys.extend(config.keys())
client.get_config(_on_get_config)
client.force_call()
return [ k for k in keys if k.startswith(text) ]
def split(self, text):
return str.split(text)"""
def complete(self, text):
return [ k for k in component.get("CoreConfig").keys() if k.startswith(text) ]

View File

@ -26,6 +26,7 @@ from deluge.ui.console.main import BaseCommand
from deluge.ui.client import client
import deluge.ui.console.colors as colors
import deluge.log
import deluge.component as component
class Command(BaseCommand):
"""Enable and disable debugging"""
@ -36,7 +37,7 @@ class Command(BaseCommand):
elif state == 'off':
deluge.log.setLoggerLevel("error")
else:
console.write("{!error!}%s" % usage)
component.get("ConsoleUI").write("{!error!}%s" % usage)
# def complete(self, text, *args):
# return [ x for x in ['on', 'off'] if x.startswith(text) ]
def complete(self, text):
return [x for x in ['on', 'off'] if x.startswith(text)]

View File

@ -54,3 +54,6 @@ class Command(BaseCommand):
self.console.write("{!info!}" + cmd + "{!input!} - " + self._commands[cmd].__doc__ or '')
self.console.write(" ")
self.console.write('For help on a specific command, use "<command> --help"')
def complete(self, line):
return [x for x in component.get("ConsoleUI")._commands if x.startswith(line)]

View File

@ -92,7 +92,6 @@ class Command(BaseCommand):
usage = "Usage: info [<torrent-id> [<torrent-id> ...]]\n"\
" You can give the first few characters of a torrent-id to identify the torrent."
def handle(self, *args, **options):
self.console = component.get("ConsoleUI")
# Compile a list of torrent_ids to request the status of
@ -197,3 +196,7 @@ class Command(BaseCommand):
s += "\n"
self.console.write(s[:-1])
def complete(self, line):
# We use the ConsoleUI torrent tab complete method
return component.get("ConsoleUI").tab_complete_torrent(line)

View File

@ -46,3 +46,7 @@ class Command(BaseCommand):
if torrent_ids:
client.core.pause_torrent(torrent_ids)
def complete(self, line):
# We use the ConsoleUI torrent tab complete method
return component.get("ConsoleUI").tab_complete_torrent(line)

View File

@ -46,3 +46,7 @@ class Command(BaseCommand):
if torrent_ids:
client.core.resume_torrent(torrent_ids)
def complete(self, line):
# We use the ConsoleUI torrent tab complete method
return component.get("ConsoleUI").tab_complete_torrent(line)

View File

@ -50,3 +50,7 @@ class Command(BaseCommand):
torrent_ids.extend(self.console.match_torrent(arg))
client.core.remove_torrent(torrent_ids, options['remove_data'])
def complete(self, line):
# We use the ConsoleUI torrent tab complete method
return component.get("ConsoleUI").tab_complete_torrent(line)

View File

@ -225,6 +225,8 @@ class ConsoleUI(component.Component):
def tab_completer(self, line, cursor, second_hit):
"""
Called when the user hits 'tab' and will autocomplete or show options.
If a command is already supplied in the line, this function will call the
complete method of the command.
:param line: str, the current input string
:param cursor: int, the cursor position in the line
@ -237,48 +239,24 @@ class ConsoleUI(component.Component):
# First check to see if there is no space, this will mean that it's a
# command that needs to be completed.
if " " not in line:
if len(line) == 0:
# We only print these out if it's a second_hit
if second_hit:
# There is nothing in line so just print out all possible commands
# and return.
self.write(" ")
for cmd in self._commands:
self.write(cmd)
return ("", 0)
possible_matches = []
# Iterate through the commands looking for ones that startwith the
# line.
possible_matches = []
for cmd in self._commands:
if cmd.startswith(line):
possible_matches.append(cmd)
line_prefix = ""
else:
# This isn't a command so treat it as a torrent_id or torrent name
name = line.split(" ")[-1]
if len(name) == 0:
# There is nothing in the string, so just display all possible options
if second_hit:
self.write(" ")
# Display all torrent_ids and torrent names
for torrent_id, name in self.torrents:
self.write(torrent_id)
self.write(name)
cmd = line.split(" ")[0]
if cmd in self._commands:
# Call the command's complete method to get 'er done
possible_matches = self._commands[cmd].complete(line.split(" ")[-1])
line_prefix = " ".join(line.split(" ")[:-1]) + " "
else:
# This is a bogus command
return (line, cursor)
# Find all possible matches
possible_matches = []
for torrent_id, torrent_name in self.torrents:
if torrent_id.startswith(name):
possible_matches.append(torrent_id)
elif torrent_name.startswith(name):
possible_matches.append(torrent_name)
# Set the line prefix that should be prepended to any input line match
line_prefix = " ".join(line.split(" ")[:-1]) + " "
# No matches, so just return what we got passed
if len(possible_matches) == 0:
return (line, cursor)
@ -292,10 +270,31 @@ class ConsoleUI(component.Component):
if second_hit:
# Only print these out if it's a second_hit
self.write(" ")
for cmd in possible_matches:
self.write(cmd)
for match in possible_matches:
self.write(match)
return (line, cursor)
def tab_complete_torrent(self, line):
"""
Completes torrent_ids or names.
:param line: str, the string to complete
:returns: list of matches
"""
possible_matches = []
# Find all possible matches
for torrent_id, torrent_name in self.torrents:
if torrent_id.startswith(line):
possible_matches.append(torrent_id)
if torrent_name.startswith(line):
possible_matches.append(torrent_name)
return possible_matches
def get_torrent_name(self, torrent_id):
"""
Gets a torrent name from the torrents list.