Fixed parent edge centration issue.

parent 35dfadad
...@@ -42,9 +42,9 @@ def generate_graph(root, separator=' ', verbose=False): ...@@ -42,9 +42,9 @@ def generate_graph(root, separator=' ', verbose=False):
# Leaves do not have children and therefore the length of its title is # Leaves do not have children and therefore the length of its title is
# the width of the leaf. # the width of the leaf.
if not isinstance(node, Node): if isinstance(node, Leaf):
node_width[node] = title_len node_width[node] = title_len
node_middle[node] = int(title_len / 2) node_middle[node] = int((title_len - 1) / 2)
return title_len return title_len
node_len = len(node) node_len = len(node)
...@@ -64,7 +64,7 @@ def generate_graph(root, separator=' ', verbose=False): ...@@ -64,7 +64,7 @@ def generate_graph(root, separator=' ', verbose=False):
middle += max(middle_pos - int(node_len % 2 == 0), 0) * separator_len middle += max(middle_pos - int(node_len % 2 == 0), 0) * separator_len
# Add a separator between each node (thus n - 1 separators). # Add a separator between each node (thus n - 1 separators).
width += separator_len * (len(node) - 1) width += separator_len * (node_len - 1)
# If the title of the node is wider than the sum of its children, the # If the title of the node is wider than the sum of its children, the
# title's width should be used. # title's width should be used.
...@@ -80,7 +80,7 @@ def generate_graph(root, separator=' ', verbose=False): ...@@ -80,7 +80,7 @@ def generate_graph(root, separator=' ', verbose=False):
return width return width
def format_lines(node): def format_lines(node):
if not isinstance(node, Node): if isinstance(node, Leaf):
# Leaf titles do not need to be centered, since the parent will # Leaf titles do not need to be centered, since the parent will
# center those lines. And if there are no parents, the entire graph # center those lines. And if there are no parents, the entire graph
# consists of a single leaf, so in that case there still is no # consists of a single leaf, so in that case there still is no
...@@ -171,7 +171,7 @@ def generate_graph(root, separator=' ', verbose=False): ...@@ -171,7 +171,7 @@ def generate_graph(root, separator=' ', verbose=False):
if i < middle_node: if i < middle_node:
marker = (left_sign if i == 0 else tsplit_dn_sign) marker = (left_sign if i == 0 else tsplit_dn_sign)
edge_line += center_text(marker, box_widths[i], edge_line += center_text(marker, box_widths[i],
middle=0, middle=node_middle[child],
right=dash_sign) right=dash_sign)
else: else:
if i == node_len - 1: if i == node_len - 1:
...@@ -180,7 +180,7 @@ def generate_graph(root, separator=' ', verbose=False): ...@@ -180,7 +180,7 @@ def generate_graph(root, separator=' ', verbose=False):
marker = tsplit_dn_sign marker = tsplit_dn_sign
edge_line += center_text(marker, box_widths[i], edge_line += center_text(marker, box_widths[i],
middle=0, middle=node_middle[child],
left=dash_sign) left=dash_sign)
try: try:
...@@ -200,10 +200,10 @@ def generate_graph(root, separator=' ', verbose=False): ...@@ -200,10 +200,10 @@ def generate_graph(root, separator=' ', verbose=False):
calculate_node_sizes(root) calculate_node_sizes(root)
#if verbose: if verbose: # pragma: nocover
# print '------- node_{width,middle} ---------' print '------- node_{width,middle} ---------'
# for node, width in node_width.iteritems(): for node, width in node_width.iteritems():
# print node.title(), 'width:', width, 'middle:', node_middle[node] print node.title(), 'width:', width, 'middle:', node_middle[node]
lines = format_lines(root) lines = format_lines(root)
......
...@@ -157,7 +157,7 @@ class TestGraph(unittest.TestCase): ...@@ -157,7 +157,7 @@ class TestGraph(unittest.TestCase):
root = Node('+', Node('+', Node('+', ac, ad), bc), bd) root = Node('+', Node('+', Node('+', ac, ad), bc), bd)
g = generate_graph(root, verbose=True) g = generate_graph(root)
self.assertEqualGraphs(g, """ self.assertEqualGraphs(g, """
+ +
╭───┴─╮ ╭───┴─╮
......
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