Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add LinkShape and LinkHead enum to the flowchart diagram #55

Merged
merged 3 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mermaid/flowchart/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from mermaid.graph import Graph

from .link import Link
from .link import Link, LinkHead, LinkShape
from .node import Node


Expand Down
35 changes: 28 additions & 7 deletions mermaid/flowchart/link.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from enum import Enum
from typing import Union

from .node import Node

LINK_SHAPES: dict[str, str] = {
Expand All @@ -16,20 +19,38 @@
}


class LinkShape(Enum):
NORMAL = 'normal'
DOTTED = 'dotted'
THICK = 'thick'
HIDEN = 'hiden'


class LinkHead(Enum):
NONE = 'none'
ARROW = 'arrow'
LEFT_ARROW = 'left-arrow'
BULLET = 'bullet'
CROSS = 'cross'


class Link:
def __init__(self,
origin: Node,
end: Node,
shape: str = 'normal',
head_left: str = 'none',
head_right: str = 'arrow',
shape: Union[str, LinkShape] = 'normal',
head_left: Union[str, LinkHead] = 'none',
head_right: Union[str, LinkHead] = 'arrow',
message: str = '') -> None:
self.oigin: Node = origin
self.end: Node = end
self.head_left = LINK_HEADS[head_left]
self.head_right = LINK_HEADS[head_right]
self.shape = LINK_SHAPES[shape]
self.message = f'|{message}|' if message else message
self.head_left: str = LINK_HEADS[
head_left if isinstance(head_left, str) else head_left.value]
self.head_right: str = LINK_HEADS[
head_right if isinstance(head_right, str) else head_right.value]
self.shape: str = LINK_SHAPES[shape if isinstance(shape, str
) else shape.value]
self.message: str = f'|{message}|' if message else message

def __str__(self) -> str:
if self.message:
Expand Down
14 changes: 13 additions & 1 deletion mermaid/tests/test_flowchart.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from mermaid.flowchart import FlowChart, Link, Node
from mermaid.flowchart import FlowChart, Link, LinkHead, LinkShape, Node
from mermaid.flowchart.node import NodeShape


Expand Down Expand Up @@ -81,6 +81,18 @@ def test_str_link_with_no_default_value(self):
message=message)
expect_string: str = f'first_node o-.-x|{message}| second_node'
self.assertEqual(expect_string, str(link))

def test_str_link_with_enum_shape(self):
link: Link = Link(self.node_1, self.node_2, shape=LinkShape.DOTTED)
expect_string: str = 'first_node -.-> second_node'
self.assertEqual(expect_string, str(link))

def test_str_link_with_enum_link_head(self):
link: Link = Link(self.node_1,
self.node_2,
head_left=LinkHead.BULLET,
head_right=LinkHead.CROSS)
expect_string: str = 'first_node o--x second_node'
self.assertEqual(expect_string, str(link))


Expand Down