Skip to content

Feature request: opt-in hard max_width budget (Error::TooWide) instead of a soft hint #32

Closed

Description

@vshylov

Summary

Feature request (filed separately as requested in #29): an opt-in way to make max_width a hard budget instead of a soft hint.

Current behavior (0.56.1, working as designed per your comment)

  • Sequence / pie / ER layouts ignore max_width entirely; class / gantt / timeline receive it for truncation but can overflow their minimum column widths.
  • On my corpus (31 LLM-generated diagrams, mostly flowchart + sequence): 14 of 28 successful renders exceed the requested width. Examples: a 5-message sequence renders 69 cols at Some(60); a classDiagram renders 203 cols at Some(90).

Use case

A TUI chat feed with a fixed panel width. Overflowing lines can't be soft-wrapped — wrapping breaks box-drawing frames — so the embedder must post-check every line and discard the whole render when any line overflows (that's what we ship: post-check + fall back to fenced source). A real budget would turn many of those all-or-nothing failures into successfully rendered diagrams (tighter node labels with truncation, stacked layouts, wrapped edge labels — whatever the layout can do).

Possible shapes (whatever fits your architecture)

  1. RenderOptions { max_width_strict: bool } — layouts must fit or return a new Error::TooWide (embedder can fall back knowingly, no post-check needed).
  2. Keep max_width soft, but document per-diagram-kind whether it's honored, and expose the natural width so embedders can pre-check cheaply.
  3. Width-aware layout improvements per kind over time (sequence first — it's the kind that fits terminals best), with Error::TooWide when even the minimal layout can't fit.

Option 1/3's explicit TooWide error is the most useful contract for embedders: "fits or tells you it can't".

No urgency from our side — the downstream post-check + fallback works fine. This is about how many diagrams get to render at all.

Context: same corpus/embedding as #29 (Rust/ratatui TUI chat client rendering LLM-generated Mermaid; mermaid-text 0.56.1 works great for us now — thanks for the fast turnaround).

Activity

  1. leboiko commented on Jul 21, 2026

    @leboiko
    Owner

    Shipped your Option 1, @vshylov — thanks for the crisp write-up, the "fits or tells you it can't" framing made the design obvious.

    What landed (mermaid-text 0.57.0)

    RenderOptions::max_width_strict (default false) turns max_width into a hard budget. When the widest line still exceeds it, you get a new error instead of an over-wide string:

    use mermaid_text::{render_with_options, RenderOptions, Error};
    
    let opts = RenderOptions {
        max_width: Some(60),
        max_width_strict: true,
        ..Default::default()
    };
    
    match render_with_options(src, &opts) {
        Ok(diagram) => paint(&diagram),                 // guaranteed ≤ 60 cols
        Err(Error::TooWide { requested, actual }) => {  // e.g. 60 vs 203
            fall_back_to_fenced_source(src);            // no post-measuring needed
        }
        Err(e) => report(e),
    }
    • Width is measured in display columns — it ignores the ANSI escapes color: true emits, and counts multi-byte labels by their rendered width, so nothing is falsely rejected.
    • It composes with every diagram kind (central post-check). For kinds that honour max_width, compaction runs first and only genuinely-unfittable renders return TooWide — so your classDiagram-at-203-vs-Some(90) case now errors cleanly instead of overflowing.
    • CLI too: mermaid-text --width 60 --strict exits non-zero if it can't fit.

    Available now on crates.io; also bundled in the viewer (markdown-reader 1.34.75).

    On Option 3 (width-aware layout)

    Deliberately not in this change — tighter labels / stacked layouts / wrapped edge labels are a larger, per-kind effort I'd rather do incrementally (sequence first, as you suggested) behind the same TooWide contract, so strict gradually turns more of your all-or-nothing failures into successful renders. Tracking that separately. If you hit a specific diagram whose minimal layout is needlessly wide, a sample in a new issue would help me prioritise which kind to tackle first.

    Thanks again — genuinely useful pair of issues.

  2. leboiko commented on Jul 21, 2026

    @leboiko
    Owner

    Closing as completed — the hard-budget contract (max_width_strict + Error::TooWide) shipped in mermaid-text 0.57.0. The broader width-aware-layout work (Option 3) is tracked separately; a new issue with a specific over-wide diagram would be the best way to prioritise it. Thanks again, @vshylov.

  3. vshylov commented on Jul 23, 2026

    @vshylov
    Author

    Thank you too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions