Browse Source

Merge pull request #384 from timfreund/master

Enable monochrome output in the 'up' and 'logs' commands
Aanand Prasad 11 năm trước cách đây
mục cha
commit
f2bf7f9e0d
3 tập tin đã thay đổi với 35 bổ sung11 xóa
  1. 7 4
      fig/cli/log_printer.py
  2. 12 3
      fig/cli/main.py
  3. 16 4
      tests/unit/log_printer_test.py

+ 7 - 4
fig/cli/log_printer.py

@@ -10,11 +10,11 @@ from .utils import split_buffer
 
 
 class LogPrinter(object):
-    def __init__(self, containers, attach_params=None, output=sys.stdout):
+    def __init__(self, containers, attach_params=None, output=sys.stdout, monochrome=False):
         self.containers = containers
         self.attach_params = attach_params or {}
         self.prefix_width = self._calculate_prefix_width(containers)
-        self.generators = self._make_log_generators()
+        self.generators = self._make_log_generators(monochrome)
         self.output = output
 
     def run(self):
@@ -35,12 +35,15 @@ class LogPrinter(object):
             prefix_width = max(prefix_width, len(container.name_without_project))
         return prefix_width
 
-    def _make_log_generators(self):
+    def _make_log_generators(self, monochrome):
         color_fns = cycle(colors.rainbow())
         generators = []
 
         for container in self.containers:
-            color_fn = color_fns.next()
+            if monochrome:
+                color_fn = lambda s: s
+            else:
+                color_fn = color_fns.next()
             generators.append(self._make_log_generator(container, color_fn))
 
         return generators

+ 12 - 3
fig/cli/main.py

@@ -137,11 +137,17 @@ class TopLevelCommand(Command):
         """
         View output from containers.
 
-        Usage: logs [SERVICE...]
+        Usage: logs [options] [SERVICE...]
+
+        Options:
+            --no-color  Produce monochrome output.
         """
         containers = self.project.containers(service_names=options['SERVICE'], stopped=True)
+
+        monochrome = options['--no-color']
+
         print("Attaching to", list_containers(containers))
-        LogPrinter(containers, attach_params={'logs': True}).run()
+        LogPrinter(containers, attach_params={'logs': True}, monochrome=monochrome).run()
 
     def ps(self, options):
         """
@@ -325,11 +331,14 @@ class TopLevelCommand(Command):
         Options:
             -d             Detached mode: Run containers in the background,
                            print new container names.
+            --no-color     Produce monochrome output.
             --no-deps      Don't start linked services.
             --no-recreate  If containers already exist, don't recreate them.
         """
         detached = options['-d']
 
+        monochrome = options['--no-color']
+
         start_links = not options['--no-deps']
         recreate = not options['--no-recreate']
         service_names = options['SERVICE']
@@ -344,7 +353,7 @@ class TopLevelCommand(Command):
 
         if not detached:
             print("Attaching to", list_containers(to_attach))
-            log_printer = LogPrinter(to_attach, attach_params={"logs": True})
+            log_printer = LogPrinter(to_attach, attach_params={"logs": True}, monochrome=monochrome)
 
             try:
                 log_printer.run()

+ 16 - 4
tests/unit/log_printer_test.py

@@ -7,16 +7,28 @@ from .. import unittest
 
 
 class LogPrinterTest(unittest.TestCase):
-    def test_single_container(self):
+    def get_default_output(self, monochrome=False):
         def reader(*args, **kwargs):
             yield "hello\nworld"
 
         container = MockContainer(reader)
-        output = run_log_printer([container])
+        output = run_log_printer([container], monochrome=monochrome)
+        return output
+
+    def test_single_container(self):
+        output = self.get_default_output()
 
         self.assertIn('hello', output)
         self.assertIn('world', output)
 
+    def test_monochrome(self):
+        output = self.get_default_output(monochrome=True)
+        self.assertNotIn('\033[', output)
+
+    def test_polychrome(self):
+        output = self.get_default_output()
+        self.assertIn('\033[', output)
+
     def test_unicode(self):
         glyph = u'\u2022'.encode('utf-8')
 
@@ -29,10 +41,10 @@ class LogPrinterTest(unittest.TestCase):
         self.assertIn(glyph, output)
 
 
-def run_log_printer(containers):
+def run_log_printer(containers, monochrome=False):
     r, w = os.pipe()
     reader, writer = os.fdopen(r, 'r'), os.fdopen(w, 'w')
-    printer = LogPrinter(containers, output=writer)
+    printer = LogPrinter(containers, output=writer, monochrome=monochrome)
     printer.run()
     writer.close()
     return reader.read()