From 925dcd43b8a827ed0017c11227e321315064eabe Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Tue, 28 Apr 2009 01:15:12 +0000 Subject: [PATCH] Fix up tab-completion to use the commands 'complete' method --- deluge/ui/console/commands/config.py | 13 +----- deluge/ui/console/commands/debug.py | 7 +-- deluge/ui/console/commands/help.py | 3 ++ deluge/ui/console/commands/info.py | 5 ++- deluge/ui/console/commands/pause.py | 4 ++ deluge/ui/console/commands/resume.py | 4 ++ deluge/ui/console/commands/rm.py | 4 ++ deluge/ui/console/main.py | 67 ++++++++++++++-------------- 8 files changed, 58 insertions(+), 49 deletions(-) diff --git a/deluge/ui/console/commands/config.py b/deluge/ui/console/commands/config.py index 6fc3da1e9..962bf9c14 100644 --- a/deluge/ui/console/commands/config.py +++ b/deluge/ui/console/commands/config.py @@ -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) ] diff --git a/deluge/ui/console/commands/debug.py b/deluge/ui/console/commands/debug.py index a7b840277..6719bef38 100644 --- a/deluge/ui/console/commands/debug.py +++ b/deluge/ui/console/commands/debug.py @@ -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)] diff --git a/deluge/ui/console/commands/help.py b/deluge/ui/console/commands/help.py index 134e08451..96b3cde7c 100644 --- a/deluge/ui/console/commands/help.py +++ b/deluge/ui/console/commands/help.py @@ -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 " --help"') + + def complete(self, line): + return [x for x in component.get("ConsoleUI")._commands if x.startswith(line)] diff --git a/deluge/ui/console/commands/info.py b/deluge/ui/console/commands/info.py index 38f9cdee7..3d1d9810d 100644 --- a/deluge/ui/console/commands/info.py +++ b/deluge/ui/console/commands/info.py @@ -92,7 +92,6 @@ class Command(BaseCommand): usage = "Usage: info [ [ ...]]\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) diff --git a/deluge/ui/console/commands/pause.py b/deluge/ui/console/commands/pause.py index 0e9dd7308..8a06f6392 100644 --- a/deluge/ui/console/commands/pause.py +++ b/deluge/ui/console/commands/pause.py @@ -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) diff --git a/deluge/ui/console/commands/resume.py b/deluge/ui/console/commands/resume.py index 119930220..c6863fa8d 100644 --- a/deluge/ui/console/commands/resume.py +++ b/deluge/ui/console/commands/resume.py @@ -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) diff --git a/deluge/ui/console/commands/rm.py b/deluge/ui/console/commands/rm.py index d5a0a2df9..c796309d2 100644 --- a/deluge/ui/console/commands/rm.py +++ b/deluge/ui/console/commands/rm.py @@ -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) diff --git a/deluge/ui/console/main.py b/deluge/ui/console/main.py index 0cc7b2a8a..bee5fe775 100644 --- a/deluge/ui/console/main.py +++ b/deluge/ui/console/main.py @@ -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.