Author: andar

Revision: 5205

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

Diff:
Modified: trunk/deluge/ui/console/commands/config.py
===================================================================
--- trunk/deluge/ui/console/commands/config.py  2009-04-27 23:18:01 UTC (rev 
5204)
+++ trunk/deluge/ui/console/commands/config.py  2009-04-28 01:15:12 UTC (rev 
5205)
@@ -134,14 +134,5 @@
             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) ]

Modified: trunk/deluge/ui/console/commands/debug.py
===================================================================
--- trunk/deluge/ui/console/commands/debug.py   2009-04-27 23:18:01 UTC (rev 
5204)
+++ trunk/deluge/ui/console/commands/debug.py   2009-04-28 01:15:12 UTC (rev 
5205)
@@ -26,6 +26,7 @@
 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 @@
         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)]

Modified: trunk/deluge/ui/console/commands/help.py
===================================================================
--- trunk/deluge/ui/console/commands/help.py    2009-04-27 23:18:01 UTC (rev 
5204)
+++ trunk/deluge/ui/console/commands/help.py    2009-04-28 01:15:12 UTC (rev 
5205)
@@ -54,3 +54,6 @@
                 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)]

Modified: trunk/deluge/ui/console/commands/info.py
===================================================================
--- trunk/deluge/ui/console/commands/info.py    2009-04-27 23:18:01 UTC (rev 
5204)
+++ trunk/deluge/ui/console/commands/info.py    2009-04-28 01:15:12 UTC (rev 
5205)
@@ -92,7 +92,6 @@
     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 @@
                     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)

Modified: trunk/deluge/ui/console/commands/pause.py
===================================================================
--- trunk/deluge/ui/console/commands/pause.py   2009-04-27 23:18:01 UTC (rev 
5204)
+++ trunk/deluge/ui/console/commands/pause.py   2009-04-28 01:15:12 UTC (rev 
5205)
@@ -46,3 +46,7 @@
 
         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)

Modified: trunk/deluge/ui/console/commands/resume.py
===================================================================
--- trunk/deluge/ui/console/commands/resume.py  2009-04-27 23:18:01 UTC (rev 
5204)
+++ trunk/deluge/ui/console/commands/resume.py  2009-04-28 01:15:12 UTC (rev 
5205)
@@ -46,3 +46,7 @@
 
         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)

Modified: trunk/deluge/ui/console/commands/rm.py
===================================================================
--- trunk/deluge/ui/console/commands/rm.py      2009-04-27 23:18:01 UTC (rev 
5204)
+++ trunk/deluge/ui/console/commands/rm.py      2009-04-28 01:15:12 UTC (rev 
5205)
@@ -50,3 +50,7 @@
             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)

Modified: trunk/deluge/ui/console/main.py
===================================================================
--- trunk/deluge/ui/console/main.py     2009-04-27 23:18:01 UTC (rev 5204)
+++ trunk/deluge/ui/console/main.py     2009-04-28 01:15:12 UTC (rev 5205)
@@ -225,6 +225,8 @@
     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 @@
         # 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 @@
             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.



--~--~---------~--~----~------------~-------~--~----~
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