davidsusu / tree-printer

Java library for visualizing tree structures in the command line
Apache License 2.0
39 stars 8 forks source link

Add support for declarative creation of trees #15

Open davidsusu opened 6 months ago

davidsusu commented 6 months ago

We should provide better support for constructing trees. A nice feature would be if we could create trees in a declarative way.

Here is an example of the conception:

ImmutableTree
    .of(
        "root node",
            BEGIN, "child 1",
                "grandchild 1,1",
                "grandchild 1,2",
            END,
             BEGIN, "child 2",
                BEGIN, "grandchild 2,1",
                    "grand-grandchild 2,1,1"
                END,
              "grandchild 2,2",
          END,
      DONE
  ).decorate(
      PAD, 2,
      BORDER, 3, Color.RED,
      BACKGROUND, Color.BLUE,
      DONE
  )

Alternative to the tree part:

L, "root node",
    P, "child 1",
        P, "grandchild 1,1",
        P, "grandchild 1,2",
        P, "grandchild 1,3",
        P, "grandchild 1,4",
        L, "grandchild 1,5",
    L, "child 2",
        P, "grandchild 2,1",
            L, "grand-grandchild 2,1,1"
        L, "grandchild 2,2",
DONE

Or:

"root node",
__, "child 1",
__, __, "grandchild 1,1",
__, __, "grandchild 1,2",
__, __, "grandchild 1,3",
__, __, "grandchild 1,4",
__, __, "grandchild 1,5",
___, "child 2",
__, __, "grandchild 2,1",
__, __, __, "grand-grandchild 2,1,1"
__, __, "grandchild 2,2",
DONE

The optionally present constant DONE is here only because Java doesn't support trailing commas. In other JVM languages, there could be no necessity for using it.

We could also support stateful method chaining builders, while I don't favor this method e. g.:

.node("child1").begin()
    .node("grandchild1.1")
    .node("grandchild1.2").begin()
        .node("grandchild1.2.1")
    .end()
    .node("grandchild1.3")
.end()

Later we should find a way to fully separate content and presentation. Currently, the handling of the outfit is shared between the tree printer and the tree itself.

Please comment and/or react if you have an opinion. :+1: :angry: :thinking: