124 lines
3.6 KiB
Python
124 lines
3.6 KiB
Python
def print_graph(left_els, middle, right_els):
|
|
left_off = 1
|
|
right_off = 1
|
|
middle_off = 1
|
|
|
|
middle_els = middle.split(".")
|
|
for i, el in enumerate(middle_els[1:]):
|
|
middle_els[i + 1] = "." + el
|
|
left_els = [el + " " for el in left_els]
|
|
right_els = [" " + el for el in right_els]
|
|
|
|
left_width = max(len(e) for e in left_els)
|
|
middle_width_inner = max(len(e) for e in middle_els)
|
|
middle_width = middle_width_inner + 2 * middle_off
|
|
right_width = max(len(e) for e in right_els)
|
|
|
|
left_height = len(left_els)
|
|
middle_height = len(middle_els) + 1
|
|
right_height = len(right_els)
|
|
|
|
total_width = left_width + middle_width + right_width + left_off + right_off + 2
|
|
total_height = max(left_height, middle_height, right_height)
|
|
horiz_pos = min(total_height - middle_height, left_height, right_height)
|
|
|
|
lines = []
|
|
left_els = (
|
|
[None] * max(horiz_pos - left_height + 1, 0)
|
|
+ list(left_els)
|
|
+ [None] * (total_height - left_height)
|
|
)
|
|
middle_els = (
|
|
[" " * middle_width] * (horiz_pos)
|
|
+ ["─" * middle_width]
|
|
+ [
|
|
" " * middle_off + format(el, f"<{middle_width_inner}") + " " * middle_off
|
|
for el in middle_els[::-1]
|
|
]
|
|
+ [" " * middle_width] * (total_height - horiz_pos)
|
|
)
|
|
right_els = (
|
|
[None] * max(horiz_pos - right_height + 1, 0)
|
|
+ list(right_els)
|
|
+ [None] * (total_height - right_height)
|
|
)
|
|
for i, (left_el, middle_el, right_el) in enumerate(zip(left_els, middle_els, right_els)):
|
|
line = get_left(left_el, i, horiz_pos, left_height, left_width, left_off)
|
|
line += middle_el
|
|
line += get_right(right_el, i, horiz_pos, right_height, right_width, right_off)
|
|
|
|
lines.append(line)
|
|
|
|
print(f"{horiz_pos = }, {total_height = }, {middle_height = }")
|
|
|
|
print("\n".join(lines[::-1]))
|
|
|
|
|
|
def get_left(left_el, i, horiz_pos, left_height, left_width, left_off):
|
|
if not left_el:
|
|
return " " * (left_width + left_off + 1)
|
|
line = format(left_el, f">{left_width}") + "─" * left_off
|
|
if i == horiz_pos:
|
|
if left_height == 1:
|
|
line += "─"
|
|
elif i == 0:
|
|
line += "┴"
|
|
elif i == left_height:
|
|
line += "┬"
|
|
else:
|
|
line += "┼"
|
|
else:
|
|
if i == left_height - 1:
|
|
line += "╮"
|
|
elif i == 0:
|
|
line += "╯"
|
|
else:
|
|
line += "┤"
|
|
return line
|
|
|
|
|
|
def get_right(right_el, i, horiz_pos, right_height, right_width, right_off):
|
|
if not right_el:
|
|
return " " * (right_width + right_off + 1)
|
|
if i == horiz_pos:
|
|
if right_height == 1:
|
|
line = "─"
|
|
elif i == 0:
|
|
line = "┴"
|
|
elif i == right_height:
|
|
line = "┬"
|
|
else:
|
|
line = "┼"
|
|
else:
|
|
if i == right_height - 1:
|
|
line = "╭"
|
|
elif i == 0:
|
|
line = "╰"
|
|
else:
|
|
line = "├"
|
|
line += "─" * right_off + format(right_el, f"<{right_width}")
|
|
return line
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print_graph(
|
|
("ads", "s", "45 sdksd dkfj"),
|
|
"main.fn.bonjour.s",
|
|
("asdf", "bonjour", "gamma", "wavelengt", "dt"),
|
|
)
|
|
print()
|
|
print()
|
|
print_graph(("ads", "s", "45 sdksd dkfj"), "main.fn.bonjour.s", ("asdf",))
|
|
print()
|
|
print()
|
|
print_graph(("ads", "s", "45 sdksd dkfj"), "main", ("asdf",))
|
|
print()
|
|
print()
|
|
print_graph(
|
|
("ads", "s", "45 sdksd dkfj", "a", "b", "c"),
|
|
"main.ollol",
|
|
("asdf", "some super long variable name"),
|
|
)
|
|
print()
|
|
print()
|