From bf42b73cc9c6da91cfdc841456aa92b35e35c9c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bert=20Fari=C3=B1a?= Date: Tue, 3 Sep 2024 13:18:17 +0100 Subject: [PATCH 1/2] Added icon support on shapes --- README.md | 2 +- src/py_d2/shape.py | 6 ++++++ tests/test_py_d2/test_d2_shape.py | 5 +++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 82f6d55..805eb96 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ See the [tests](/tests/test_py_d2) for more detailed usage examples. - [x] Shapes in shapes - [x] Arrow directions - [x] Markdown / block strings / code in shapes -- [ ] Icons in shapes +- [x] Icons in shapes - [ ] SQL table shapes - [ ] Class shapes - [ ] Comments diff --git a/src/py_d2/shape.py b/src/py_d2/shape.py index ebc55f7..d61e837 100644 --- a/src/py_d2/shape.py +++ b/src/py_d2/shape.py @@ -72,6 +72,8 @@ def __init__( shapes: Optional[List[D2Shape]] = None, # The style of this shape style: Optional[D2Style] = None, + # An icon for this shape + icon: Optional[str] = None, # Connections for the child shapes (NOT the connections for this shape) connections: Optional[List[D2Connection]] = None, # A shape this is near @@ -83,6 +85,7 @@ def __init__( self.shape = shape self.shapes = shapes or [] self.style = style + self.icon = icon self.connections = connections or [] self.near = near self.kwargs = kwargs @@ -107,6 +110,9 @@ def lines(self) -> List[str]: if self.style: properties += self.style.lines() + if self.icon: + properties.append(f"icon: {self.icon}") + for key, value in self.kwargs.items(): other_property = value.lines() other_property_line_1 = other_property[0] diff --git a/tests/test_py_d2/test_d2_shape.py b/tests/test_py_d2/test_d2_shape.py index 04738b2..dcbb6b7 100644 --- a/tests/test_py_d2/test_d2_shape.py +++ b/tests/test_py_d2/test_d2_shape.py @@ -137,6 +137,11 @@ def test_d2_shape_shapes(): assert str(shape_sequence_diagram) == "shape_name: {\n shape: sequence_diagram\n}" +def test_d2_shape_icon(): + shape = D2Shape(name="shape_name", icon="https://icons.terrastruct.com/essentials%2F117-database.svg") + assert str(shape) == "shape_name: {\n icon: https://icons.terrastruct.com/essentials%2F117-database.svg\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}" From 0021f437522f1405719e63b831f773aa08518836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bert=20Fari=C3=B1a?= Date: Tue, 3 Sep 2024 13:47:49 +0100 Subject: [PATCH 2/2] Added support for empty labels --- README.md | 1 + src/py_d2/helpers.py | 7 +++++-- tests/test_py_d2/test_d2_shape.py | 5 +++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 805eb96..24e31a0 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ See the [tests](/tests/test_py_d2) for more detailed usage examples. - [x] Arrow directions - [x] Markdown / block strings / code in shapes - [x] Icons in shapes +- [x] Support for empty labels - [ ] SQL table shapes - [ ] Class shapes - [ ] Comments diff --git a/src/py_d2/helpers.py b/src/py_d2/helpers.py index 639bd45..dd03205 100644 --- a/src/py_d2/helpers.py +++ b/src/py_d2/helpers.py @@ -13,10 +13,13 @@ def add_label_and_properties( has_properties: bool = properties is not None and len(properties) > 0 first_line = name - if label or has_properties: + if label is not None or has_properties: first_line += ":" - if label: + if label is not None and len(label) == 0: + first_line += ' ""' + + if label and len(label) > 0: first_line += f" {label}" if has_properties: diff --git a/tests/test_py_d2/test_d2_shape.py b/tests/test_py_d2/test_d2_shape.py index dcbb6b7..03ecb83 100644 --- a/tests/test_py_d2/test_d2_shape.py +++ b/tests/test_py_d2/test_d2_shape.py @@ -16,6 +16,11 @@ def test_d2_shape_label(): assert str(shape) == "shape_name: shape_label" +def test_d2_shape_empty_label(): + shape = D2Shape(name="shape_name", label="") + assert str(shape) == 'shape_name: ""' + + def test_d2_shape_style(): shape = D2Shape(name="shape_name", style=D2Style(fill="red")) assert str(shape) == "shape_name: {\n style: {\n fill: red\n }\n}"