Przeglądaj źródła

Expand the process column of the status table to fit the widest name

Mike Naberezny 11 lat temu
rodzic
commit
b9cc344d62

+ 3 - 0
CHANGES.txt

@@ -11,6 +11,9 @@
 - The ``status`` command in ``supervisorctl`` now supports group name
 - The ``status`` command in ``supervisorctl`` now supports group name
   syntax: ``status group:*``.
   syntax: ``status group:*``.
 
 
+- The process column in the table output by the ``status`` command in
+  ``supervisorctl`` now expands to fit the widest name.
+
 - The ``update`` command in ``supervisorctl`` now accepts optional group
 - The ``update`` command in ``supervisorctl`` now accepts optional group
   names.  When group names are specified, only those groups will be
   names.  When group names are specified, only those groups will be
   updated.  Patch by Gary M. Josack.
   updated.  Patch by Gary M. Josack.

+ 14 - 8
supervisor/supervisorctl.py

@@ -554,11 +554,19 @@ class DefaultControllerPlugin(ControllerPluginBase):
     def help_exit(self):
     def help_exit(self):
         self.ctl.output("exit\tExit the supervisor shell.")
         self.ctl.output("exit\tExit the supervisor shell.")
 
 
-    def _procrepr(self, info):
-        template = '%(name)-32s %(state)-10s %(desc)s'
-        name = make_namespec(info['group'], info['name'])
-        return template % {'name':name, 'state':info['statename'],
-                           'desc':info['description']}
+    def _show_statuses(self, process_infos):
+        namespecs, maxlen = [], 30
+        for i, info in enumerate(process_infos):
+            namespecs.append(make_namespec(info['group'], info['name']))
+            if len(namespecs[i]) > maxlen:
+                maxlen = len(namespecs[i])
+
+        template = '%(namespec)-' + str(maxlen+3) + 's%(state)-10s%(desc)s'
+        for i, info in enumerate(process_infos):
+            line = template % {'namespec': namespecs[i],
+                               'state': info['statename'],
+                               'desc': info['description']}
+            self.ctl.output(line)
 
 
     def do_status(self, arg):
     def do_status(self, arg):
         if not self.ctl.upcheck():
         if not self.ctl.upcheck():
@@ -592,9 +600,7 @@ class DefaultControllerPlugin(ControllerPluginBase):
                     else:
                     else:
                         msg = "%s: ERROR (no such process)" % name
                         msg = "%s: ERROR (no such process)" % name
                     self.ctl.output(msg)
                     self.ctl.output(msg)
-
-        for info in matching_infos:
-            self.ctl.output(self._procrepr(info))
+        self._show_statuses(matching_infos)
 
 
     def help_status(self):
     def help_status(self):
         self.ctl.output("status <name>\t\tGet status for a single process")
         self.ctl.output("status <name>\t\tGet status for a single process")

+ 40 - 0
supervisor/tests/test_supervisorctl.py

@@ -478,6 +478,46 @@ class TestDefaultControllerPlugin(unittest.TestCase):
         value = plugin.ctl.stdout.getvalue().strip()
         value = plugin.ctl.stdout.getvalue().strip()
         self.assertEqual(value, "Error: bad channel 'fudge'")
         self.assertEqual(value, "Error: bad channel 'fudge'")
 
 
+    def test_status_table_process_column_min_width(self):
+        plugin = self._makeOne()
+        result = plugin.do_status('')
+        self.assertEqual(result, None)
+        lines = plugin.ctl.stdout.getvalue().split("\n")
+        self.assertEqual(lines[0].index("RUNNING"), 33)
+
+    def test_status_table_process_column_expands(self):
+        plugin = self._makeOne()
+        options = plugin.ctl.options
+        def f(*arg, **kw):
+            from supervisor.states import ProcessStates
+            return [{'name': 'foo'*50, # long name
+                     'group':'foo',
+                     'pid': 11,
+                     'state': ProcessStates.RUNNING,
+                     'statename': 'RUNNING',
+                     'start': 0,
+                     'stop': 0,
+                     'spawnerr': '',
+                     'now': 0,
+                     'description':'foo description'},
+                    {
+                    'name': 'bar', # short name
+                    'group': 'bar',
+                    'pid': 12,
+                    'state': ProcessStates.FATAL,
+                    'statename': 'RUNNING',
+                    'start': 0,
+                    'stop': 0,
+                    'spawnerr': '',
+                    'now': 0,
+                    'description': 'bar description',
+                    }]
+        options._server.supervisor.getAllProcessInfo = f
+        self.assertEqual(plugin.do_status(''), None)
+        lines = plugin.ctl.stdout.getvalue().split("\n")
+        self.assertEqual(lines[0].index("RUNNING"), 157)
+        self.assertEqual(lines[1].index("RUNNING"), 157)
+
     def test_status_all_processes_no_arg(self):
     def test_status_all_processes_no_arg(self):
         plugin = self._makeOne()
         plugin = self._makeOne()
         result = plugin.do_status('')
         result = plugin.do_status('')