ArthurSonzogni / Diagon

Interactive ASCII art diagram generators. :star2:
https://arthursonzogni.com/Diagon/
MIT License
1.47k stars 56 forks source link

Add support for new lines in Sequence #40

Closed tg-m closed 2 years ago

tg-m commented 2 years ago

When using diagon Sequence it is not possible (at least I couldn't find a way) to add new lines to the messages in the Sequence submodule.

This patch adds parameter --new_lines_at_bsn that results in substituting literal \n with new lines.

Example:

$ echo "Alice -> Bob: send(\n  msg="Hello...",\n  length=8,\n)" | \
       diagon Sequence --new_lines_at_bsn=true

output:

┌─────┐          ┌───┐
│Alice│          │Bob│
└──┬──┘          └─┬─┘
   │               │
   │send(          │
   │  msg=Hello...,│
   │  length=8,    │
   │)              │
   │──────────────>│
┌──┴──┐          ┌─┴─┐
│Alice│          │Bob│
└─────┘          └───┘
ArthurSonzogni commented 2 years ago

Thanks!

Please let me some time to review this patch. I believe this is a good thing to add.

ArthurSonzogni commented 2 years ago

Thank you!

I am also thinking about an alternative syntax:

A->B: |
  This is
  a multiline string

similar to YAML: https://yaml-multiline.info/

tg-m commented 2 years ago

TBH, my first impression was to abandon the custom grammar and switch to YAML, but I needed multi-line support quite fast, thus the patch.

Also... I think there is an issue with the Sequence grammar definition. It does not handle colons (:) in message text / label correctly.

YAML will give far more flexibility, but it's somewhat verbose... I had something like below in my mind, when I was looking at the grammar.

Basic

nodes: 
  - &ref_alice
    name: Alice
  - &ref_bob
    name: Bob
  - &ref_charlie
    name: Charlie
messages:
  - from: *ref_alice
    to: *ref_bob
    label: "Hello Alice! It's me, Bob."
  - from: *ref_bob
    to: *ref_charlie
    label: |-
      A label with...
      Not only one or two lines.
      Not even three!
      But four! :)

More detailed

options:
  message-arrow-ending: triangle
  node-style:
    - name
    - label
nodes: 
  - &ref_alice
    label: "Alice node"
    name: A
    rotate: 90
  - &ref_bob
    label: "Bob"
    name: B
  - &ref_charlie
    label: "Charlie"
    name: C
    node-style:
      - label
messages:
  - from: *ref_alice
    to: *ref_bob
    label: "Hello Alice! It's me, Bob."
    align: left
    order: 11
    message-arrow-ending: square ## custom arrow ending
  - from: *ref_bob
    to: *ref_charlie
    label: |-
      A label with...
      Not only one or two lines.
      Not even three!
      But four! :)
    align: centre ## or center for US speakers ;)
    order: 2
    ## message-arrow-ending: triangle ## defaults to what is in options
ArthurSonzogni commented 2 years ago

Sorry, I didn't meant switching toward yaml. I just meant updating the syntax, so that we can use something similar to YAML multiline strings.

tg-m commented 2 years ago

Yes, I was aware of that. After a little bit of sleep, I think that your grammar is more concise than YAML, thus better in this case.

It would be nice to have support for |, |- and |+ variants of YAML's |. See, e.g., https://yaml-multiline.info/. (Added: Oh, you've already pasted that link, I missed that...)