Skip to content

Commit

Permalink
Icons and empty labels support (#19)
Browse files Browse the repository at this point in the history
* Added icon support on shapes

* Added support for empty labels
  • Loading branch information
Bert-Farina authored Oct 10, 2024
1 parent f2379dc commit 84f62ad
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ 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
- [x] Support for empty labels
- [ ] SQL table shapes
- [ ] Class shapes
- [ ] Comments
Expand Down
7 changes: 5 additions & 2 deletions src/py_d2/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions src/py_d2/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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]
Expand Down
10 changes: 10 additions & 0 deletions tests/test_py_d2/test_d2_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -137,6 +142,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}"
Expand Down

0 comments on commit 84f62ad

Please sign in to comment.