Skip to content

Fix(tui): sequence, class and ER diagrams lose nothing in the preview - #41

Merged
adibhanna merged 8 commits into
mainfrom
claude/interesting-poincare-c5d579
Nov 4, 2026
Merged

adibhanna merged 8 commits into
mainfrom
claude/interesting-poincare-c5d579

Conversation

@adibhanna

@adibhanna adibhanna commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #39, so this targets its branch. Retarget to main once #39 merges.

The library's text drawings of sequence, class and ER diagrams lose content at any pane width, even when the diagram fits. #39 only falls back to the source when one is wider than the pane.

What the library loses

Diagram Lost or wrong
Sequence Long messages are clipped (a really long message about t). Notes, loop/alt blocks, autonumber, titles and activations are dropped. -x, -) and -> draw like ->>. A<<->>B creates a participant called A<<. parser->>db loses the message because the parser reads a par block. Non-ASCII labels get gaps (hé llo wö rld).
ER With three or more entities, every relationship and label is dropped. With two, only one relationship is drawn and the boxes can swap, which would put each cardinality on the wrong entity. Attribute comments are dropped. From the parser code, |o, o| and }o relationships never parse.
Class Relationship types, cardinalities and labels are dropped, and arrows appear between unrelated boxes. Method arguments are dropped, and +eat(a) void becomes +eat: void(). From the renderer and parser code, static and abstract marks, namespaces and notes are dropped, and A<|--B and A <-- B do not parse.

ER and class order comes from a Go map, so it changes between parses. It is not alphabetical, as first reported.

What changes

  • Sequence diagrams are drawn only with just participant lines and plain ->> or -->> messages, when the parser agrees with each statement, every label comes out whole with each character one cell wide, and the drawing fits. Anything else shows the source with a note, including actor, a late or repeated alias, <br in a message, and a name holding an arrow. A drawing over about 1 million cells also shows the source, before the library allocates its grid.
  • Class and ER diagrams always show the source with a note. No fork of the library.
  • Unclosed fences: a note ending before its fence closes kept losing the last statement, in every diagram type. The last line is now dropped only when it closes the fence. A diagram with no nodes yet shows its source instead of vanishing.
  • CRLF embeds: a note embedded with ![[...]] no longer puts carriage returns in rows.
  • Math: source wraps rather than ending in …, which the lower minimum below would otherwise make more likely.
  • Very narrow panes: renderPreview had a 10-column minimum that cut letters off every row. It is now 1. This affects all preview content, not just Mermaid.
  • README: "Reading view, embeds and diagrams" describes this, and says the zen style shows every Mermaid block as code. That renderer never calls the Mermaid drawing code, and this change does not alter that.

Known limits

  • Class and ER diagrams no longer draw boxes at all, including simple ones. Drawing them in zn would mean working around the parser gaps above, so it is left out here.
  • Somewhere below about 44 columns with the sidebar open, the app layout itself is cut off, so the preview still loses text (at 38 columns, and at 34 it shows nothing). This is not specific to Mermaid.
  • Front matter, %%{init}%% and classDiagram-v2 show the source under an "invalid mermaid header" error. All the content is there, but the error is misleading.

Testing

  • go test ./... and go vet ./... pass, and the Mermaid tests pass with -race. The new tests failed before the fix, except TestMermaidPlainSequenceIsStillDrawn, which guards behaviour that should not change.
  • Real TUI in tmux on a scratch vault: default style at 48 and 120 columns, zen at 120. That run predates the review fixes, which are covered by tests only.
  • A new test renders a rich note in panes of 1 to 16 cells in both styles, with no panic.
  • One run each, parsing and laying out at 120 columns: 20,000 participants took 34 ms and 21 MB, and 2,000 by 2,000 took 6 ms and 3 MB.
  • Pictures through mmdc were not run in a real Kitty terminal. That code is unchanged.

A left-to-right flowchart wider than the pane was cut off with no way to see
the rest, and its labels kept their source quotes. Width was only part of it:
the library's text layout routes no edges, so its boxes overlap, an edge back
to an earlier node and every edge label are dropped, and a branch is drawn
through the boxes between.

zn now keeps the library's parser and draws flowcharts and state diagrams
itself: a row of boxes when a left-to-right diagram fits, otherwise a column
with wrapped labels. An edge to the box drawn next is an arrow between them,
and any other edge is listed under its source by the name of its target.
Parsing is cached by content and only the layout follows the width.

The parser ends a quoted label at its first closing bracket and loses the edge
after it, drops a label given after a node's first use, and skips spellings
like `A -- yes --> B`. zn works around these. When a statement still cannot
have been read in full, the block shows its source and says so. Sequence, class
and ER diagrams still come from the library, and show their source when they do
not fit.

Tables and aliases in the reading view had bugs of their own. In the default
style, aliased links pushed column rules out of line and a wrapped alias showed
its token brackets. In the zen style, an alias's pipe split the cell, wide cells
were cut off, and tables with no outer pipes did not render. `[[Note\|alias]]`
now follows Note, not `Note\`.
The library's text drawings lose content at any pane width. A long sequence
message is clipped, and notes and blocks are dropped. An ER diagram drops its
relationships and a class diagram their types, labels and method arguments.
ER and class order also changes from one parse to the next.

A sequence diagram is now drawn only when it has just participants and plain
`->>` or `-->>` messages, each label comes out whole, and it fits. Other
sequence diagrams, and all class and ER diagrams, show their source with a note.

The preview's 10-column minimum cut letters off every row in a very narrow
pane, so wrapping now follows the pane. This applies to all preview content.
Review of the first version found a panic and diagram content lost with no
note. A label holding the runes zn used to hide quoted text ended the app, so
zn now picks runes the note does not hold. IDs that start with a keyword, like
title-bar, vanished with their edges. `A --o B --> C` drew a false edge label.

The read-in-full check now compares the edges and the node IDs of each
statement, for every statement that can name one, so a circle or cross edge,
trailing words and a statement word used as an ID show the source. Quote marks
open only where a label can start and no longer pair across statements, arrows
inside a label stay text, and `A-->B[x]` with no spaces is read as an edge.

A second label for a node and a group never closed show the source too. Two
nodes with one label show their IDs, and an empty label shows its ID.

In the reading view, a note's own token brackets and hard spaces are kept, a
table cell's padding goes at its end, and a zen table only protects pipes inside
a real wikilink.
… into claude/interesting-poincare-c5d579

# Conflicts:
#	internal/tui/mermaidtext_test.go
A fence the note never closes kept losing its last statement with no note, in
every diagram type. The last line is now dropped only when it closes the fence.
An embedded CRLF note no longer puts carriage returns in rows, and a `%%` comment
holding a semicolon no longer forces the source.

A sequence diagram now shows its source for a late or repeated alias, a label
with a wide letter, combining mark, tab or control character, a name holding an
arrow, an `actor`, or a `<br` in a message. The drawing is also capped before the
library allocates its grid, so 2,000 participants by 2,000 messages stays small.

Math source wraps rather than ending in an ellipsis, which the lower preview
minimum had made more likely. A diagram with no nodes yet shows its source
instead of vanishing.
@adibhanna
adibhanna changed the base branch from claude/tui-mermaid-width-28f594 to main November 4, 2026 22:34
…th (#39)

Mermaid text diagrams were cut off at the pane's edge, and the library's text layout also dropped edge labels and edges back to earlier nodes. zn keeps the library's parser and now draws flowcharts and state diagrams itself: a row of boxes when one fits, otherwise a column with wrapped labels. Any edge not drawn between neighbouring boxes is listed under its source.

A statement the parser cannot read in full shows the block's source with a note, so nothing goes missing without saying so. IDs in any script, comments, entities and the common edge spellings are read.

Tables in the reading view also change. Aliased links keep columns in line, and a wrapped alias shows no token brackets. In the zen style an alias stays in one cell, wide cells wrap, and tables with no outer pipes render.
Note text now shows as text and does not act on the terminal. The filter lives in internal/termsafe and runs on every TUI frame and, when stdout or stderr is a terminal, on CLI output. Pipes and files keep every byte.

The editor, both reading views and the views that list note text draw a control rune as a one-cell picture such as a visible ESC, so the cursor stays on its rune and a note's own colours stay out of the terminal.

Opening a note with bytes that are not valid UTF-8, or with CRLF line ends, no longer marks it changed, so quitting leaves the file as it is until a real edit. Very long words are broken before Glamour, whose word wrap is quadratic in a word's length.
@adibhanna
adibhanna merged commit 10b475f into main Nov 4, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant