Skip to content

Fix(tui): Mermaid flowcharts show every node and edge at any pane width - #39

Merged
adibhanna merged 7 commits into
mainfrom
claude/tui-mermaid-width-28f594
Nov 4, 2026
Merged

adibhanna merged 7 commits into
mainfrom
claude/tui-mermaid-width-28f594

Conversation

@adibhanna

@adibhanna adibhanna commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

A Discord report (Greed) showed a Mermaid flowchart in the preview cut off at "jj describ…". Steps D, E and F and the loop back to A could not be seen at all, so the diagram misled, and labels kept their source quotes.

Why not just re-render top-down

The library's text layout routes no edges, so it loses information at any width. Left-to-right boxes overlap, an edge back to an earlier node and every edge label are dropped, and a branch draws through the boxes between (O│e). Greed's loop back would still have been missing.

What changes

Flowcharts and state diagrams. zn keeps the library's parser, with no fork, and draws them itself in internal/tui/mermaidtext.go:

  • 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. Any other edge is listed under its source, like └─▶ edit files in @.
  • Edge labels, dotted, thick and two-way edges, subgraph names and <br/> in labels all show.

Parser gaps, worked around in zn. These belong upstream in the library's parser/flowchart.go; nothing has been filed there.

  • A quoted label ended at its first closing bracket and lost the edge after it, and semicolons split statements inside quotes.
  • A label given after a node's first use was dropped.
  • A -- yes --> B, A -. yes .-> B, A == yes ==> B and longer arrows lost the edge silently.

When zn cannot be sure. A statement the parser cannot have read in full (A --x B, A ~~~ B, A@{ ... }, a state note) shows the block's source with a note. So does a sequence, class or ER diagram wider than the pane. Errors now wrap instead of being cut off.

Tables and aliases, reproduced while checking the rest of the report:

Bug Style
Aliased links pushed table columns out of line default
Long aliases that wrapped showed literal ⟦ ⟧ default
An alias's pipe split a table cell zen
Wide table cells were cut off zen
Tables with no outer pipes did not render zen
[[Note|alias]] links pointed at Note\ both

Before and after

Real TUI at 100 columns in split view. Before:

┌──────────────────…
▶ "edit files in @…
└──────────────────…

After, end of the column:

┌────────────────────────┐
│ jj git push --bookmark │
│ name                   │
└─┬──────────────────────┘
  └─▶ edit files in @

At 240 columns it stays one row, with the loop listed below:

┌──────────────────┐  ┌─────────────────────┐  ┌───…
│ edit files in @ │─▶│ jj diff / jj status │─▶│ jj…
└──────────────────┘  └─────────────────────┘  └───…
jj git push --bookmark name ─▶ edit files in @

Speed

Parsing is cached by content, and only the layout follows the width. A 500-node diagram took about 5 ms to parse and 2 ms to lay out, in one run.

Known limits

  • Kitty and mmdc picture code is unchanged. TestMermaidPictureStillReplacesTheText covers it with a fake mmdc; it was not run in a real Kitty terminal.
  • Sequence diagrams still clip long messages and ER diagrams still drop relationships, even when they fit the pane. Separate session.
  • Wikilink syntax inside inline code is rewritten in preview. Separate session.
  • Wide tables in the default style can still cut a header with …. That comes from lipgloss, which truncates headers, and is left as is.
  • [[Bookmarks|bookmarks]] shows as bookmarks in preview here, so the Bookmarks|bookmarks in Greed's screenshot was not reproduced there. Tables not rendering in the default style was not reproduced either; there was no screenshot of it.
  • Node shapes draw as plain boxes. The library's text renderer also ignored them.

Testing

  • go test ./... and go vet ./... pass, and the affected tests pass with -race. Tests in internal/tui/mermaidtext_test.go failed before the fix.
  • Real TUI runs in tmux on a scratch vault, before and after, at 70, 100, 200 and 240 columns, in preview and split.
  • No release here. A follow-up zn 0.16.1 is Adib's call.

Review fixes

An independent review of the first commit found a panic and diagram content lost with no note. The second commit fixes these, with a test for each:

  • Panic. A label holding the runes zn used to hide quoted text ended the app. zn now picks runes the note does not hold.
  • Lost nodes and edges. IDs starting with a keyword (title-bar, click) vanished, and A --o B --> C drew a false edge label.
  • A stricter check. Each statement's edges and node IDs must match what the parser found, otherwise the block shows its source. Second labels and unclosed groups show the source too.
  • Quotes and arrows. Quote marks open only where a label can start and do not pair across statements. Arrows in a label stay text. A-->B[x] with no spaces is read as an edge.
  • Labels. Two nodes with one label show their IDs, and an empty label shows its ID.
  • Reading view. A note's own ⟦, ⟧ and hard spaces are kept. A table cell's padding goes at its end. A zen table only protects pipes inside a real wikilink.

Second review

A second review found more that was misread, fixed in later commits with tests:

  • A state's :::class became a label and could lose the edge.
  • A %% comment holding a semicolon drew false edges.
  • A --> C(f(x)) lost its closer without a note, so a leftover closer now shows the source.
  • IDs in any script now draw, as do IDs with dots. Entities such as #amp; decode. An empty diagram says it has nothing to draw. CRLF front matter is skipped.
  • A zen table row of many links and a label of many private-use runes are now linear, and a zen table keeps a row with no outer pipes.

Not fixed

  • A composite state or subgraph box shows its ID. state "Big one" as B {...} then A --> B draws a box B, and only the group list says Big one.
  • A literal rune in the U+10FFF0 range reads as one of the preview's own stand-ins.
  • [[Note\|]] and backlinks of [[Note\|alias]] depend on the escaped-pipe PRs in this release.
  • Rows can overflow in panes under 9 columns, and ZWJ emoji measure differently in the two width functions.

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\`.
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.
Table padding treated a pipe inside a link's alias as a column rule, so `a|b`
showed as `a |b`. A rule inside a link token is now skipped.

The test helper zenTable becomes zenTableText, since another open pull request
adds a helper with that name.
At the end of the file it conflicted with the tests another open pull request
adds there.
A second review of the text diagrams found more that was misread. A state's
:::class became a label and could lose the edge, and a %% comment holding a
semicolon drew false edges from its text, because comments were cut after the
line was split into statements. `A --> C(f(x))` lost its closer without a note,
since any leftover closer counted as a doubled shape's; closers are now only
allowed when the shape opened doubled.

IDs in any script now draw. The parser's IDs are ASCII, so others are renamed
while it reads them, and this also covers IDs with dots. Entities like #amp; and
#35; decode, an empty diagram says it has nothing to draw, and front matter with
CRLF line ends is skipped.

Two long inputs are now linear: a zen table row of many links, and picking mask
runes for a label holding many private-use runes. A zen table also keeps a row
with no outer pipes once its separator has been seen.
A diagram with no node yet can also be marked as one zn cannot fully draw. The
note that it has nothing to draw is the true one, so it is checked first.
The note that a diagram has nothing to draw is for one that really is empty. A
statement the parser could not read also leaves no nodes, and that still says
the source is shown.
@adibhanna
adibhanna merged commit 6024f13 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