Sfoglia il codice sorgente

Change completionmatches() so it only looks at the current fragment

Mike Naberezny 11 anni fa
parent
commit
ce0e0305a7
2 ha cambiato i file con 21 aggiunte e 13 eliminazioni
  1. 13 8
      supervisor/supervisorctl.py
  2. 8 5
      supervisor/tests/test_supervisorctl.py

+ 13 - 8
supervisor/supervisorctl.py

@@ -223,7 +223,7 @@ class Controller(cmd.Cmd):
             raise
         return True
 
-    def completionmatches(self, text, line, onlygroups=False):
+    def completionmatches(self, text, onlygroups=False):
         groups=[]
         programs=[]
         groupwiseprograms={}
@@ -245,11 +245,12 @@ class Controller(cmd.Cmd):
             # add/remove/update require only the group name
             results = [i+' ' for i in groups if i.startswith(text)]
         else:
-            current = line.split()[-1]
-            if ':' in current:
-                g = current.split(':')[0]
-                results = [i+' ' for i in groupwiseprograms[g]
-                           if i.startswith(text)]
+            if ':' in text:
+                group, piece = text.split(':', 1)
+                results = []
+                for p in groupwiseprograms.get(group, []):
+                    if p.startswith(piece):
+                        results.append('%s:%s ' % (group, p))
             else:
                 results = [i for i in total if i.startswith(text)]
         return results
@@ -276,10 +277,10 @@ class Controller(cmd.Cmd):
             # commands that accept a process name
             elif cmd in ('clear', 'fg', 'pid', 'restart', 'start', 'stop',
                          'status', 'tail'):
-                results = self.completionmatches(text, line)
+                results = self.completionmatches(text)
             # commands that accept a group name
             elif cmd in ('add', 'remove', 'update'):
-                results = self.completionmatches(text, line, onlygroups=True)
+                results = self.completionmatches(text, onlygroups=True)
         if len(results) > state:
             return results[state]
 
@@ -1151,6 +1152,10 @@ def main(args=None, options=None):
     if options.interactive:
         try:
             import readline
+            delims = readline.get_completer_delims()
+            delims = delims.replace(':', '') # "group:process" as one word
+            readline.set_completer_delims(delims)
+
             if options.history_file:
                 try:
                     readline.read_history_file(options.history_file)

+ 8 - 5
supervisor/tests/test_supervisorctl.py

@@ -114,11 +114,14 @@ class ControllerTests(unittest.TestCase):
         controller.stdout=StringIO()
         plugin = DummyPlugin()
         controller.options.plugin=(plugin,)
-        for i in ['add','remove']:
-            result = controller.completionmatches('',i+' ',1)
-            self.assertEqual(result,['foo ','bar ','baz '])
-        result = controller.completionmatches('','fg baz:')
-        self.assertEqual(result,['baz_01 '])
+        results = controller.completionmatches('', onlygroups=True)
+        self.assertEqual(results, ['foo ','bar ','baz '])
+        results = controller.completionmatches('f', onlygroups=True)
+        self.assertEqual(results, ['foo '])
+        results = controller.completionmatches('', onlygroups=False)
+        self.assertEqual(results, ['foo ', 'bar ', 'baz:baz_01 '])
+        results = controller.completionmatches('b', onlygroups=False)
+        self.assertEqual(results, ['bar ', 'baz:baz_01 '])
 
     def test_nohelp(self):
         options = DummyClientOptions()