py-d2 icon indicating copy to clipboard operation
py-d2 copied to clipboard

Connection properties / styles

Open smheidrich opened this issue 1 year ago • 0 comments

Description

D2 supports properties like style for connections, same as it does for shapes (cf. e.g. the font color example). But py-d2 only seems to support them for shapes, with no constructor parameters available to set them for connections.

It would be nice if D2 supported style and other arbitrary properties for connections with a similar API as for shapes.

Why

Potential use cases are styling connection labels in different colors or text sizes, which can be useful for many applications in which connections model something important (such as transitions between states of a state machine).

Alternatives

The only workarounds I'm aware of if someone needs this feature are to either write code to output D2 connections themselves or to hack support for properties like styles into their own subclass of py-d2's D2Connection class like so:

class CustomD2Connection(D2Connection):
  style: D2Style

  def __init__(self, *args, **kwargs):
    if "style" in kwargs:
      self.style = kwargs.pop("style")
    else:
      self.style = None
    super().__init__(*args, **kwargs)

  def lines(self) -> list[str]:
    lines = super().lines()
    if self.style is not None:
      lines[0] = f"{lines[0]} {{"
      lines.extend(self.style.lines())
      lines.append("}")
    return lines

But it would be better if this was supported by py-d2 directly.

smheidrich avatar Sep 15 '24 13:09 smheidrich