From e856e65db53c91adb77f873419fc3998b6bf1b47 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Wed, 29 Apr 2009 02:41:10 +0000 Subject: [PATCH] Add path tab-complete to the add command --- deluge/ui/console/commands/add.py | 39 +++++++++++++++++++++++++++++++ deluge/ui/console/main.py | 14 +++++++---- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/deluge/ui/console/commands/add.py b/deluge/ui/console/commands/add.py index bc63fe030..0155c2827 100644 --- a/deluge/ui/console/commands/add.py +++ b/deluge/ui/console/commands/add.py @@ -48,6 +48,9 @@ class Command(BaseCommand): t_options["download_location"] = options["path"] for arg in args: + if not os.path.isfile(arg): + self.console.write("{!error!}This is a directory!") + continue self.console.write("{!info!}Attempting to add torrent: %s" % arg) filename = os.path.split(arg)[-1] filedump = base64.encodestring(open(arg).read()) @@ -58,3 +61,39 @@ class Command(BaseCommand): self.console.write("{!error!}Torrent was not added! %s" % result) client.core.add_torrent_file(filename, filedump, t_options).addCallback(on_success).addErrback(on_fail) + + def complete(self, line): + line = os.path.abspath(os.path.expanduser(line)) + ret = [] + if os.path.exists(line): + # This is a correct path, check to see if it's a directory + if os.path.isdir(line): + # Directory, so we need to show contents of directory + #ret.extend(os.listdir(line)) + for f in os.listdir(line): + # Skip hidden + if f.startswith("."): + continue + f = os.path.join(line, f) + if os.path.isdir(f): + f += "/" + ret.append(f) + else: + # This is a file, but we could be looking for another file that + # shares a common prefix. + for f in os.listdir(os.path.dirname(line)): + if f.startswith(os.path.split(line)[1]): + ret.append(os.path.join( os.path.dirname(line), f)) + else: + # This path does not exist, so lets do a listdir on it's parent + # and find any matches. + ret = [] + for f in os.listdir(os.path.dirname(line)): + if f.startswith(os.path.split(line)[1]): + p = os.path.join(os.path.dirname(line), f) + + if os.path.isdir(p): + p += "/" + ret.append(p) + + return ret diff --git a/deluge/ui/console/main.py b/deluge/ui/console/main.py index bee5fe775..6df200800 100644 --- a/deluge/ui/console/main.py +++ b/deluge/ui/console/main.py @@ -244,7 +244,7 @@ class ConsoleUI(component.Component): # line. for cmd in self._commands: if cmd.startswith(line): - possible_matches.append(cmd) + possible_matches.append(cmd + " ") line_prefix = "" else: @@ -264,7 +264,7 @@ class ConsoleUI(component.Component): # return it, else we need to print out the matches without modifying # the line. elif len(possible_matches) == 1: - new_line = line_prefix + possible_matches[0] + " " + new_line = line_prefix + possible_matches[0] return (new_line, len(new_line)) else: if second_hit: @@ -272,6 +272,12 @@ class ConsoleUI(component.Component): self.write(" ") for match in possible_matches: self.write(match) + else: + p = " ".join(line.split(" ")[:-1]) + new_line = " ".join([p, os.path.commonprefix(possible_matches)]) + if len(new_line) > len(line): + line = new_line + cursor = len(line) return (line, cursor) def tab_complete_torrent(self, line): @@ -289,9 +295,9 @@ class ConsoleUI(component.Component): # Find all possible matches for torrent_id, torrent_name in self.torrents: if torrent_id.startswith(line): - possible_matches.append(torrent_id) + possible_matches.append(torrent_id + " ") if torrent_name.startswith(line): - possible_matches.append(torrent_name) + possible_matches.append(torrent_name + " ") return possible_matches