terrastruct / d2

D2 is a modern diagram scripting language that turns text to diagrams.
https://d2lang.com
Mozilla Public License 2.0
16.16k stars 399 forks source link

Feature Request: icon shortnames #1281

Open HariSekhon opened 1 year ago

HariSekhon commented 1 year ago

I love how flexible your icon URL system is, but for common icons it would be much more convenient if we could just use icon short names instead of full URLs.

For example in the Python diagrams module which is another diagrams-as-code solution, you only use URLs to download icons that aren't part of the set, otherwise you can just from blah import someicon and then throughout the code just use someicon everywhere, which is much shorter.

I've worked around this so far using classes to load the icon once and then .class: someicon for each object.

A good example of this can be seen here:

https://github.com/HariSekhon/Diagrams-as-Code/blob/master/opentsdb_kubernetes_hbase.d2

alixander commented 1 year ago

oh hah interesting use case of classes!

i think this would be a lot more useful once we have an LSP though.

otherwise you'd always be guessing names.

HariSekhon commented 1 year ago

So in Python diagrams you still have to look up the icon here to find out the name to import obviously, but once it is imported, you can reused the short name from the top of the code over and over for any number of objects without using long fully qualified names. This reduces code clutter a lot.

Obviously I've tried to emulate this DRY pattern using classes at the top to create the short names, but it'd probably be better to have an icon shortname import mechanism to account for cases where different objects would need different classes due to other differing style attributes, which would force duplication of URLs in the code.

alixander commented 1 year ago

would https://github.com/terrastruct/d2/issues/105 solve this the same for you?

HariSekhon commented 1 year ago

I think variables would solve it in a generic way, although it'd probably be programmatically nicer for client coders to have first-class support for some kind of icon shortname declaration as it could clean up and cut down the syntax a bit.

HariSekhon commented 1 year ago

Thinking about this more, using variables, would result in quite a lot of clutter in that you'd still have to do something like:

$myvariable = http://......
someobject {
  icon: $myvariable
}

This is almost no improvement over the class workaround I've done above.

I think this could be improved to not have to re-declare the icon attribute over and over for every object.

Some kind of automatic matching between the icon and the object would be ideal.

In Python diagrams this is done like so:

SomeIcon("name")

so I'm not sure whether in D2 it would be better to do:

name("iconname") ...

Alternatively perhaps a single case insensitive regex match of objects names vs icons would be the most efficient:

icon .*apache.* https://icons.terrastruct.com/dev%2Fapache.svg

or probably better to not anchor the regex as most cases would only need this short less redundant syntax, and in rarer cases where it needs anchoring users could still use ^ and $ as per usual regex rules:

icon apache https://icons.terrastruct.com/dev%2Fapache.svg

then all objects throughout the rest of the code file with names or ids matching .*apache.* would be assigned that apache.svg icon:

Apache httpd1  # automatically assigned https://icons.terrastruct.com/dev%2Fapache.svg
Apache httpd2  # automatically assigned https://icons.terrastruct.com/dev%2Fapache.svg

Declaring the icon one liner for each type of object you use throughout the D2 code.

This would be awesome in improving D2 codability and significantly cut down on redundant code.