Engineering story
Engineering story
Recent work made Mermaid rendering width-aware and added flowchart and sequence-diagram paths, Markdown extraction, themed Unicode output, and a Loom-free docs renderer.
Terminal & TUI Stack
A width-aware Mermaid diagram engine for terminal emulators, interactive CLIs and coding sessions.
Terminal preview
cat deploy.mmd | termaid --border rounded --theme plain ╭──────────╮
│ End User │
╰───── HTTPS Request
│
│
╭─────▼─────╮
│ Cloud CDN │
╰───── Forward
│
│
╭──────▼──────╮
│ API Gateway │
╰──────│──────╯
│
├──────────╮
╭─────────╯ │
│ │
╭───────▼───────╮ ╭───────▼──────╮
│ Redis Session │ │ Core Service │
╰───────────────╯ ╰───────│──────╯
│
╭─────────────────────┤
│ ╭─╯
╭──────▼─────╮ ┌────────▼───────┐
│ Primary DB │ ⬡ Kafka Events ⬡
╰────────────╯ └────────────────┘
Static preview retained from the original page.
Why termaid?
Standard ASCII generators break in real terminal viewports. Here is how termaid solves it.
When a standard ASCII diagram exceeds terminal columns (W), line wrapping tears rectangular borders apart into illegible noise.
termaid measures bounding boxes before drawing against an active 5% safety margin (W × 0.95). It dynamically re-routes wide LR graphs to vertical TD layouts and applies compact spacing. If a diagram still cannot fit cleanly, it gracefully falls back to displaying the raw Mermaid code block rather than printing corrupted ASCII.
LLMs often include HTML tags (<br/>, <span>, <b>), non-standard line breaks, or inline comments. termaid automatically sanitizes and normalizes dirty input before parsing.
| Feature | Basic Renderers | termaid |
|---|---|---|
| LLM HTML Output | Crashes or misaligns | Sanitizes & unescapes |
| Terminal Width | Ignored (line wrap) | 5% margin buffer |
| Auto-Rotation | None | Dynamic LR → TD |
| Overflow Guard | Mangled ASCII | Safe syntax fallback |
| Diagram Scope | Flowcharts only | Flowchart + Sequence |
Engine Capabilities
Engineered for high resilience, speed, and clean terminal aesthetics.
🔀
Supports rectangles, rounded boxes, stadiums, cylinders, circles, diamonds, hexagons, subroutines, and flag nodes with Sugiyama topological ranking.
⏱️
Renders lifelines, solid/dashed arrows, autonumbering, notes, and container blocks (alt, opt, loop, par).
📐
Discrete grid pathfinder routes arrows around obstacles with automatic bitmask junction resolution (┼, ┬, ┴, ╭, ╰).
🛡️
Context deadlines bound rendering work so host tools can limit the time spent laying out a diagram.
📝
Run termaid extract README.md to locate and transform all embedded Mermaid blocks into terminal diagrams in-place.
🎨
Adaptive, Dark, Light, High-Contrast, and Plain palettes with customizable border styles (Rounded, Sharp, Double, ASCII).
CLI Quickstart
Simple UNIX pipeable tool for everyday command-line workflows.
echo -e "flowchart TD\n A[Start] --> B{Valid?}\n B -->|Yes| C[Run]\n B -->|No| D[Halt]" | termaid
termaid -f architecture.mmd --theme dark --border rounded
termaid extract docs/Architecture.md
Embeddable Go Library API
The renderer can be used by Go command-line and terminal UI tools. Its packages use terminal-width support from golang.org/x/term; the repository also includes a separate Loom-based interactive example.
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/termaid/termaid"
)
func main() {
diagram := `
sequenceDiagram
autonumber
Client->>Gateway: POST /v1/checkout
Gateway->>DB: INSERT order
DB-->>Gateway: 201 Created
Gateway-->>Client: 200 OK
`
opts := termaid.DefaultOptions()
opts.Width = 100
opts.Theme = termaid.ThemeAdaptive
opts.BorderStyle = termaid.BorderStyleRounded
opts.Timeout = 500 * time.Millisecond
opts.FallbackOnOverflow = true
ctx := context.Background()
output, err := termaid.Render(ctx, diagram, opts)
if err != nil {
log.Fatal(err)
}
fmt.Println(output)
}
// Or import modular subpackages directly:
// import "github.com/termaid/termaid/sanitize"
// import "github.com/termaid/termaid/canvas"
The module path shown here is the repository’s current internal module identifier. The source repository is private; this example describes the API rather than a public installation path.
Engineering story
Recent work made Mermaid rendering width-aware and added flowchart and sequence-diagram paths, Markdown extraction, themed Unicode output, and a Loom-free docs renderer.
License
The source repository is private. Source files declare AGPL-3.0-or-later.