Author: andar

Revision: 5213

Log:
        Add path tab-complete to the add command

Diff:
Modified: trunk/deluge/ui/console/commands/add.py
===================================================================
--- trunk/deluge/ui/console/commands/add.py     2009-04-28 15:59:56 UTC (rev 
5212)
+++ trunk/deluge/ui/console/commands/add.py     2009-04-29 02:41:10 UTC (rev 
5213)
@@ -48,6 +48,9 @@
             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 @@
                 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

Modified: trunk/deluge/ui/console/main.py
===================================================================
--- trunk/deluge/ui/console/main.py     2009-04-28 15:59:56 UTC (rev 5212)
+++ trunk/deluge/ui/console/main.py     2009-04-29 02:41:10 UTC (rev 5213)
@@ -244,7 +244,7 @@
             # 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 @@
         # 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 @@
                 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 @@
         # 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
 



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"deluge-commit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/deluge-commit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to