Commit 9b8fed3f authored by Yannick Kirschhoffer's avatar Yannick Kirschhoffer

Layout issue when more than two ports exposed. This closes #9

parent 70b19d99
import math
from docker_ascii_map.widget import *
from docker_ascii_map.docker_config import Configuration, Container
......@@ -16,7 +18,7 @@ def build_container_widget(container: Container, encoding: str) -> Widget:
return container_widget
def build_ordered_network_list(config):
def build_ordered_network_list(config: Configuration) -> List[str]:
networks = set()
for container in config.containers:
......@@ -39,6 +41,18 @@ def build_ordered_network_list(config):
return networks
def build_container_wrapper(container: Container, container_widget: Widget) -> Widget:
ports_count = len(container.ports)
total_padding = ports_count - 2
if total_padding <= 0:
return container_widget
else:
return Padding(container_widget,
Size(0, int(math.floor(total_padding / 2))),
Size(0, int(math.ceil(total_padding / 2))))
class Renderer:
def __init__(self):
pass
......@@ -60,7 +74,7 @@ class Renderer:
if [net] == container.networks:
container_widget = build_container_widget(container, encoding)
cnt_widgets_map[container] = container_widget
net_widgets.append(container_widget)
net_widgets.append(build_container_wrapper(container, container_widget))
net_box = Border(VBox(net_widgets), net)
net_widgets_map[net] = net_box
......
......@@ -205,19 +205,28 @@ class Annotations(Widget):
raster = Raster()
raster.write(self._width, 0, self._content.render(hints))
widget_annotations_map = {}
used_y = []
for widget, annotation_text in self._annotations:
if widget not in widget_annotations_map.keys():
widget_annotations_map[widget] = []
widget_annotations_map[widget].append(annotation_text)
for widget, annotations in widget_annotations_map.items():
bounds = raster.origin_bounds(widget)
y = int(bounds.y + bounds.h / 2) - 1
y = int(bounds.y + (bounds.h - len(annotations)) / 2)
while y in used_y:
y += 1
for annotation in annotations:
while y in used_y:
y += 1
used_y.append(y)
used_y.append(y)
raster.write(0, y, annotation_text)
raster.write(self._width, y, ' ]-')
raster.draw_line(self._width + 3, y, bounds.x - 1, y)
raster.write(0, y, annotation)
raster.write(self._width, y, ' ]-')
raster.draw_line(self._width + 3, y, bounds.x - 1, y)
return raster
......@@ -131,6 +131,7 @@ class RenderingTests(unittest.TestCase):
PortMapping(private_port=8080, public_port=80),
PortMapping(private_port=22, public_port=22),
PortMapping(private_port=25, public_port=25),
PortMapping(private_port=5432, public_port=54),
]),
])
renderer = Renderer()
......@@ -144,9 +145,11 @@ class RenderingTests(unittest.TestCase):
' | im |\n'
' +--------+\n'
' +- net2 -+\n'
'80 ]-+ [✓] n2 |\n'
'22 ]-+ im |\n'
'25 ]-+--------+\n'
'80 ]-+ |\n'
'22 ]-+ [✓] n2 |\n'
'25 ]-+ im |\n'
'54 ]-+ |\n'
' +--------+\n'
, text)
......
......@@ -213,6 +213,21 @@ class ModelTests(unittest.TestCase):
, str(model.render())
)
def test_Annotation_Alignment(self):
self.maxDiff = None
w1 = Paragraph(['Hello', 'World !', 'Line 3', 'Line 4'])
model = Annotations(w1, [(w1, '23'), (w1, '22'), (w1, '24'), (w1, '24')])
text = str(model.render())
self.assertEqual(
'23 ]-Hello \n'
'22 ]-World !\n'
'24 ]-Line 3 \n'
'24 ]-Line 4 \n'
, text
)
if __name__ == '__main__':
unittest.main()
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