Skip to content

Commit fe11b41

Browse files
Merge pull request #82 from SAY-5/fix-long-chain-index-panic
2 parents a4f2321 + 03d4bd0 commit fe11b41

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

cmd/graph.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,10 @@ func (g *graph) setSubgraphs(textSubgraphs []*textSubgraph) {
181181
}
182182

183183
func (g *graph) createMapping() {
184-
// Set mapping coord for every node in the graph
185-
highestPositionPerLevel := []int{}
186-
// Init array with 0 values
187-
// TODO: I'm sure there's a better way of doing this
188-
for i := 0; i < 100; i++ {
189-
highestPositionPerLevel = append(highestPositionPerLevel, 0)
190-
}
184+
// Set mapping coord for every node in the graph.
185+
// Keyed by level so it grows with the graph instead of assuming a fixed
186+
// number of levels; a missing key reads as the zero value.
187+
highestPositionPerLevel := map[int]int{}
191188

192189
// TODO: should the mapping be bottom-to-top instead of top-to-bottom?
193190
// Set root nodes to level 0

cmd/render_graph_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
11
package cmd
22

33
import (
4+
"fmt"
45
"strings"
56
"testing"
67

78
"github.com/AlexanderGrooff/mermaid-ascii/pkg/diagram"
89
"github.com/mattn/go-runewidth"
910
)
1011

12+
func TestRenderGraphHandlesLongChainWithoutPanic(t *testing.T) {
13+
config := diagram.NewTestConfig(true, "cli")
14+
15+
var b strings.Builder
16+
b.WriteString("graph TD\n")
17+
for i := 1; i < 30; i++ {
18+
fmt.Fprintf(&b, "N%d --> N%d\n", i, i+1)
19+
}
20+
21+
output, err := RenderDiagram(b.String(), config)
22+
if err != nil {
23+
t.Fatalf("RenderDiagram() error = %v", err)
24+
}
25+
26+
if !strings.Contains(output, "N30") {
27+
t.Fatalf("expected output to contain the last node\noutput:\n%s", output)
28+
}
29+
}
30+
1131
func TestRenderGraphKeepsDisplayWidthForWideNodeLabels(t *testing.T) {
1232
config := diagram.NewTestConfig(true, "cli")
1333
output, err := RenderDiagram("graph LR\nA[\"中A\"] --> B", config)

0 commit comments

Comments
 (0)