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

Icons and empty labels support #19

Merged
merged 2 commits into from
Oct 10, 2024
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
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