Commit 276fb5e1 authored by Yannick Kirschhoffer's avatar Yannick Kirschhoffer

#6: Implementation of container colors based on their status.

parent d555590b
......@@ -7,14 +7,18 @@ from docker_ascii_map.docker_config import Configuration, Container
def build_container_widget(container: Container, encoding: str, color: bool) -> Widget:
lines = []
container_running = container.status == 'running'
if encoding == 'UTF-8':
statuschar = u"\u2713" if container.status == 'running' else u"\u274c"
statuschar = u"\u2713" if container_running else u"\u274c"
else:
statuschar = 'V' if container.status == 'running' else 'x'
statuschar = 'V' if container_running else 'x'
color = 'green' if container_running else 'red'
lines.append('[' + statuschar + '] ' + container.name)
lines.append(' ' + container.image)
container_widget = Paragraph(lines)
container_widget = Paragraph(lines, color=color)
return container_widget
......@@ -112,4 +116,4 @@ class Renderer:
ports_box = Annotations(links_box, portmaps)
root_box = ports_box
return str(root_box.render())
return root_box.render().text(color)
......@@ -21,5 +21,5 @@ if __name__ == '__main__':
config = config_parser.get_configuration()
# print(config)
text = renderer.render(config, sys.stdout.encoding, color_mode)
text = renderer.render(config, encoding=sys.stdout.encoding, color=color_mode)
print(text)
......@@ -28,10 +28,9 @@ class RasterCell:
class Raster:
def __init__(self, color: bool = False):
def __init__(self):
self._cells = []
self._default = RasterCell()
self._color = color
def write(self, x: int, y: int, text, origin: object = None, color: str = None):
if type(text) is Raster:
......@@ -104,12 +103,12 @@ class Raster:
return Boundary(b_x, b_y, b_w, b_h) if b_w > 0 else None
def __str__(self):
def text(self, color: bool = False):
text = ''
for line in self._cells:
for c in line:
if self._color and c.color:
if color and c.color:
text += termcolor.colored(c.character, c.color)
else:
text += c.character
......@@ -117,3 +116,6 @@ class Raster:
text += '\n'
return text
def __str__(self):
return self.text()
......@@ -148,8 +148,9 @@ class HBox(Widget):
class Paragraph(Widget):
def __init__(self, lines: List[str]):
def __init__(self, lines: List[str], color: str = None):
self._lines = lines
self._color = color
def preferred_size(self) -> Size:
return Size(max([len(l) for l in self._lines]), len(self._lines))
......@@ -158,7 +159,7 @@ class Paragraph(Widget):
r = Raster()
for l in self._lines:
r.write(0, r.size()[1], l, self)
r.write(0, r.size()[1], l, origin=self, color=self._color)
return r
......
......@@ -48,6 +48,24 @@ class RenderingTests(unittest.TestCase):
'+----------------------------+\n',
renderer.render(config, 'ascii'))
def test_container_color(self):
config = Configuration([
Container('n1', 'running', ['net1'], 'group1/image-long-name', []),
Container('n2', 'stopped', ['net2'], 'group2/image-short', []),
])
renderer = Renderer()
self.assertNotEquals(
'+- net1 ---------------------+\n'
'| [✓] n1 |\n'
'| group1/image-long-name |\n'
'+----------------------------+\n'
'+- net2 ---------------------+\n'
'| [❌] n2 |\n'
'| group2/image-short |\n'
'+----------------------------+\n',
renderer.render(config, color=True))
def test_dual_net_config(self):
self.maxDiff = None
config = Configuration([
......
......@@ -100,7 +100,7 @@ class RasterTests(unittest.TestCase):
)
def test_color_disabled(self):
r = Raster(color=False)
r = Raster()
r.write(0, 0, 'Red', color='red')
r.write(0, 1, 'Green', color='green')
self.assertEqual(
......@@ -110,12 +110,12 @@ class RasterTests(unittest.TestCase):
)
def test_color_enabled(self):
r = Raster(color=True)
r = Raster()
r.write(0, 0, 'Red', color='red')
r.write(0, 1, 'Green', color='green')
self.assertEqual(
'\x1b[31mR\x1b[0m\x1b[31me\x1b[0m\x1b[31md\x1b[0m\n\x1b[32mG\x1b[0m\x1b[32mr\x1b[0m\x1b[32me\x1b[0m\x1b[32me\x1b[0m\x1b[32mn\x1b[0m\n'
, str(r)
, r.text(True)
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment