From 1a613bd18fd52e712c8d648e97f9c51375a5841d Mon Sep 17 00:00:00 2001 From: David Revay Date: Thu, 29 Dec 2022 20:47:57 +1100 Subject: [PATCH] feat: add near support --- graph.d2 | 2 +- src/py_d2/D2Shape.py | 12 ++++++++++++ tests/test_py_d2/test_d2_shape.py | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/graph.d2 b/graph.d2 index a8cfe30..88f8a12 100644 --- a/graph.d2 +++ b/graph.d2 @@ -8,4 +8,4 @@ shape_name2: { fill: blue } } -shape_name1 -> shape_name2 +shape_name1 -> shape_name2 \ No newline at end of file diff --git a/src/py_d2/D2Shape.py b/src/py_d2/D2Shape.py index c29d98a..9acf96a 100644 --- a/src/py_d2/D2Shape.py +++ b/src/py_d2/D2Shape.py @@ -41,11 +41,18 @@ 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 @@ -53,6 +60,7 @@ def __init__( self.shapes = shapes or [] self.style = style self.connections = connections or [] + self.near = near def add_shape(self, shape: D2Shape): self.shapes.append(shape) @@ -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() @@ -78,3 +89,4 @@ def lines(self) -> List[str]: def __repr__(self) -> str: lines = self.lines() return "\n".join(lines) + return "\n".join(lines) diff --git a/tests/test_py_d2/test_d2_shape.py b/tests/test_py_d2/test_d2_shape.py index d75995a..915e4f0 100644 --- a/tests/test_py_d2/test_d2_shape.py +++ b/tests/test_py_d2/test_d2_shape.py @@ -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}" \ No newline at end of file