Skip to content

Commit

Permalink
feat: add near support
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBlenny committed Dec 29, 2022
1 parent 0f4039a commit 1a613bd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion graph.d2
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ shape_name2: {
fill: blue
}
}
shape_name1 -> shape_name2
shape_name1 -> shape_name2
12 changes: 12 additions & 0 deletions src/py_d2/D2Shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,26 @@ class D2Shape:
def __init__(
self,
name: str,
# The label of this shape
label: Optional[str] = None,
# The actual 2D shape of this shape
shape: Optional[Shape] = None,
# A list of child shapes (when this shape is a container)
shapes: Optional[List[D2Shape]] = None,
# The style of this shape
style: Optional[D2Style] = None,
# Connections for the child shapes (NOT the connections for this shape)
connections: Optional[List[D2Connection]] = None,
# A shape this is near
near: Optional[str] = None,
):
self.name = name
self.label = label
self.shape = shape
self.shapes = shapes or []
self.style = style
self.connections = connections or []
self.near = near

def add_shape(self, shape: D2Shape):
self.shapes.append(shape)
Expand All @@ -68,6 +76,9 @@ def lines(self) -> List[str]:
if self.shape:
properties.append(f"shape: {self.shape.value}")

if self.near:
properties.append(f"near: {self.near}")

if self.style:
properties += self.style.lines()

Expand All @@ -78,3 +89,4 @@ def lines(self) -> List[str]:
def __repr__(self) -> str:
lines = self.lines()
return "\n".join(lines)
return "\n".join(lines)
4 changes: 4 additions & 0 deletions tests/test_py_d2/test_d2_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,7 @@ def test_d2_shape_shapes():
assert str(shape_image) == "shape_name: {\n shape: image\n}"
assert str(shape_classs) == "shape_name: {\n shape: class\n}"
assert str(shape_sequence_diagram) == "shape_name: {\n shape: sequence_diagram\n}"

def test_d2_shape_near():
shape = D2Shape(name="shape_name", near="some_other_shape")
assert str(shape) == "shape_name: {\n near: some_other_shape\n}"

0 comments on commit 1a613bd

Please sign in to comment.