From 1df173d684df7d7e0325e3c692e6c838bef81daf Mon Sep 17 00:00:00 2001 From: Asmageddon Date: Tue, 29 May 2012 17:35:53 +0200 Subject: [PATCH] Added tab_complete_path method to LegacyUI and ConsoleUI, made add command use it for completion --- deluge/ui/console/commands/add.py | 68 +---------------------------- deluge/ui/console/main.py | 4 ++ deluge/ui/console/modes/legacy.py | 71 +++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 67 deletions(-) diff --git a/deluge/ui/console/commands/add.py b/deluge/ui/console/commands/add.py index c875c796c..a9003398c 100644 --- a/deluge/ui/console/commands/add.py +++ b/deluge/ui/console/commands/add.py @@ -103,70 +103,4 @@ class Command(BaseCommand): return defer.DeferredList(deferreds) def complete(self, line): - self.console = component.get("ConsoleUI") - - line = line.replace("\ ", " ") - 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)) - try: - for f in os.listdir(line): - # Skip hidden - if f.startswith("."): - continue - f = os.path.join(line, f) - if os.path.isdir(f): - if os.sep == '\\': # Windows path support :| - f += "\\" - else: # \o/ Unix - f += "/" - elif not f.endswith(".torrent"): - continue - ret.append(f) - except OSError: - self.console.write("{!error!}Permission denied: {!info!}%s" % line) - else: - try: - # 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)) - except OSError: - self.console.write("{!error!}Permission denied: {!info!}%s" % line) - else: - # This path does not exist, so lets do a listdir on it's parent - # and find any matches. - try: - ret = [] - if os.path.isdir(os.path.dirname(line)): - 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): - if os.sep == '\\': # Windows path support :| - p += "\\" - else: # \o/ Unix - p += "/" - ret.append(p) - except OSError: - self.console.write("{!error!}Permission denied: {!info!}%s" % line) - - #Sort by date instead of whatever we might get from os.listdir - ret = sorted(ret, key=lambda p: os.stat(p).st_mtime, reverse=True) - #Directories first - ret = sorted(ret, key=lambda p: os.path.isdir(p), reverse=True) - - #Highlight directory names - for i, filename in enumerate(ret): - if os.path.isdir(filename): - ret[i] = "{!cyan!}%s" % filename - - for i in range(0, len(ret)): - ret[i] = ret[i].replace(" ", r"\ ") - return ret + return component.get("ConsoleUI").tab_complete_path(line, ext=".torrent", sort="date") \ No newline at end of file diff --git a/deluge/ui/console/main.py b/deluge/ui/console/main.py index 4ee8cf3cc..55a5c689d 100644 --- a/deluge/ui/console/main.py +++ b/deluge/ui/console/main.py @@ -442,6 +442,10 @@ Please use commands from the command line, eg:\n if self.interactive and isinstance(self.screen,deluge.ui.console.modes.legacy.Legacy): return self.screen.tab_complete_torrent(line) + def tab_complete_path(self, line, type="file", ext="", sort="name", dirs_first=True): + if self.interactive and isinstance(self.screen,deluge.ui.console.modes.legacy.Legacy): + return self.screen.tab_complete_path(line, type=type, ext=ext, sort=sort, dirs_first=dirs_first) + def set_mode(self, mode): reactor.removeReader(self.screen) self.screen = mode diff --git a/deluge/ui/console/modes/legacy.py b/deluge/ui/console/modes/legacy.py index d5be1c778..685531348 100644 --- a/deluge/ui/console/modes/legacy.py +++ b/deluge/ui/console/modes/legacy.py @@ -676,6 +676,77 @@ class Legacy(BaseMode, component.Component): cursor = len(line) return (line, cursor) + def tab_complete_path(self, line, type="file", ext="", sort="name", dirs_first=1): + self.console = component.get("ConsoleUI") + + line = line.replace("\ ", " ") + 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)) + try: + for f in os.listdir(line): + # Skip hidden + if f.startswith("."): + continue + f = os.path.join(line, f) + if os.path.isdir(f): + if os.sep == '\\': # Windows path support :| + f += "\\" + else: # \o/ Unix + f += "/" + elif not f.endswith(ext): + continue + ret.append(f) + except OSError: + self.console.write("{!error!}Permission denied: {!info!}%s" % line) + else: + try: + # 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)) + except OSError: + self.console.write("{!error!}Permission denied: {!info!}%s" % line) + else: + # This path does not exist, so lets do a listdir on it's parent + # and find any matches. + try: + ret = [] + if os.path.isdir(os.path.dirname(line)): + 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): + if os.sep == '\\': # Windows path support :| + p += "\\" + else: # \o/ Unix + p += "/" + ret.append(p) + except OSError: + self.console.write("{!error!}Permission denied: {!info!}%s" % line) + + if sort == "date": + ret = sorted(ret, key=lambda p: os.stat(p).st_mtime, reverse=True) + + if dirs_first == 1: + ret = sorted(ret, key=lambda p: os.path.isdir(p), reverse=True) + elif dirs_first == -1: + ret = sorted(ret, key=lambda p: os.path.isdir(p), reverse=False) + + #Highlight directory names + for i, filename in enumerate(ret): + if os.path.isdir(filename): + ret[i] = "{!cyan!}%s" % filename + + for i in range(0, len(ret)): + ret[i] = ret[i].replace(" ", r"\ ") + return ret def tab_complete_torrent(self, line): """