Skip to content

Commit

Permalink
Merge dc2fa0e into 83a1301
Browse files Browse the repository at this point in the history
  • Loading branch information
etimberg committed Oct 1, 2018
2 parents 83a1301 + dc2fa0e commit efe1bcb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
29 changes: 28 additions & 1 deletion asciigraf/asciigraf.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,40 @@ def graph_from_ascii(network_string):
(pos, node_label) for pos in node_label_char_positions
)

# we'll treat edge labels (e.g. "(my_label)") as consecutive "-" edge chars
# First pass to put edge chars in the map
edge_chars = OrderedDict(
(pos, (char if char in EDGE_CHARS else "-"))
for pos, char in char_iter(network_string)
if char in EDGE_CHARS or pos in line_label_char_positions
)

# Second pass to correct vertical labels. This needs to be
# a correction so that the OrderedDict is correctly setup
for root_position, label in list(line_labels.items()):
is_vertical_label = any(
above in edge_chars and edge_chars[above] == '|'
for above in (
root_position + Point(i, -1)
for i, char in enumerate(label)
)
)

if is_vertical_label:
for i, char in enumerate(label):
pos = root_position + Point(i, 0)
above = pos + Point(0, -1)

try:
if edge_chars[above] == '|':
edge_chars[pos] = '|'
del line_labels[root_position]
line_labels[pos] = label
else:
del edge_chars[pos]
except KeyError:
# pass
del edge_chars[pos]

edge_char_to_edge_map = {}
edges = []

Expand Down
43 changes: 43 additions & 0 deletions tests/test_asciigraf.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,46 @@ def test_line_labels():

assert graph.get_edge_data("D", "E")["label"] == "string"
assert graph.get_edge_data("D", "E")["length"] == 15


def test_vertical_line_labels():
graph = graph_from_ascii("""
A
|
(Vertical)
|
B
""")

assert set(graph.nodes()) == {
"A", "B"
}

assert set(graph.edges()) == {
("A", "B")
}

assert graph.get_edge_data("A", "B")["label"] == "Vertical"
assert graph.get_edge_data("A", "B")["length"] == 3


def test_vertical_line_adjacent_labels():
graph = graph_from_ascii("""
C
A---D |
(Vertical)
|
B
""")

assert set(graph.nodes()) == {
"A", "B", "C", "D"
}

assert set(graph.edges()) == {
("A", "D"),
("C", "B")
}

assert graph.get_edge_data("C", "B")["label"] == "Vertical"
assert graph.get_edge_data("C", "B")["length"] == 3

0 comments on commit efe1bcb

Please sign in to comment.