Center pipeline graphs and share port rows to reduce graph height

This commit is contained in:
2026-10-10 02:08:52 +01:00
parent d73a021cd4
commit c74f7f3b41
10 changed files with 128 additions and 65 deletions
+3 -14
View File
@@ -240,18 +240,6 @@ func renderGraphAppearance(graph protocol.Graph, selected string, appearance *gr
put(x, row, vertical, color, active)
put(x+w-1, row, vertical, color, active)
}
if !protocol.IsTerminal(node.key) {
text(x+1, y+2, strings.Repeat("─", w-2), color, false)
incoming := 0
for _, edge := range layout.edges {
if edge.target == node.key {
incoming++
}
}
if incoming > 0 {
text(x+1, y+3+incoming, strings.Repeat("─", w-2), color, false)
}
}
text(x, y+h-1, bottom, color, active)
}
line := func(p graphPoint, r rune, color lipgloss.Color) {
@@ -319,10 +307,11 @@ func renderGraphAppearance(graph protocol.Graph, selected string, appearance *gr
} else {
put(end.x, end.y, '▶', color, bold)
target := nodes[edge.target]
text(target.x+2, edge.to, fmt.Sprintf("in [%d]", edge.id), color, bold)
text(target.x+2, edge.to, fmt.Sprintf("[%d]", edge.id), color, bold)
}
source := nodes[edge.source]
text(source.x+2, edge.from, fmt.Sprintf("%s [%d]", edge.label, edge.id), color, bold)
label := fmt.Sprintf("%s [%d]", edge.label, edge.id)
text(source.x+source.width-2-len(label), edge.from, label, color, bold)
}
result := make([]string, layout.height)
for row, cells := range canvas {
+55
View File
@@ -0,0 +1,55 @@
package tui
import (
"context"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
"github.com/charmbracelet/x/ansi"
)
func assertCenteredGraphBody(t *testing.T, view string, graph []string, width, height int) {
t.Helper()
widest := 0
for _, line := range graph {
widest = max(widest, ansi.StringWidth(line))
}
inner, rows := width-4, height-5
left, top := max(0, (inner-widest)/2), max(0, (rows-len(graph))/2)
actual := strings.Split(ansi.Strip(view), "\n")
for i, line := range graph {
want := " " + strings.Repeat(" ", left) + ansi.Strip(line)
if strings.TrimRight(actual[3+top+i], " ") != strings.TrimRight(want, " ") {
t.Fatalf("graph not centered at row %d:\nactual %q\nwant %q", i, actual[3+top+i], want)
}
}
}
func TestPipelineAndRunGraphsCenterWhenTheyFit(t *testing.T) {
graph, err := pipeline.Parse(pipeline.Template([]string{"worker"}, true))
if err != nil {
t.Fatal(err)
}
m := New(context.Background(), "")
m.width, m.height = 160, 60
m.detail = &protocol.PipelineDetail{Graph: graph}
m.keys.detail = true
// Stale panning/scroll values from a larger graph must not prevent centering.
m.pipelineTop, m.graphPan = 99, 99
for _, focused := range []bool{false, true} {
m.stageFocus = focused
assertCenteredGraphBody(t, m.graphPanel(m.width, 40), m.graphLines(), m.width, 40)
}
m.runDetail = &protocol.RunDetail{Graph: graph}
assertCenteredGraphBody(t, m.runGraphPanel(40, 99, 99), m.runGraphLines(m.width-4), m.width, 40)
}
func TestGraphViewportKeepsOversizedDiagramPannable(t *testing.T) {
lines := []string{"0123456789abcdefghij", "ABCDEFGHIJKLMNOPQRST"}
view := strings.Split(ansi.Strip(graphViewport("Graph", lines, 14, 7, 99, 99, true, "pan")), "\n")
if strings.TrimSpace(view[3]) != "abcdefghij" || strings.TrimSpace(view[4]) != "KLMNOPQRST" {
t.Fatal("oversized graph should clamp pan without centering/cropping the wrong region", view)
}
}
+15 -3
View File
@@ -18,11 +18,12 @@ func TestCompactGraphDimensions(t *testing.T) {
for name, graph := range map[string]protocol.Graph{"linear": linear, "nested": nestedForkGraph(t)} {
layout := layoutGraph(graph)
t.Logf("%s: %d columns × %d rows", name, layout.width, layout.height)
// Measured before compaction: linear 175×15; nested forks 147×48.
if name == "linear" && (layout.width > 100 || layout.height > 15) {
// Before compaction: linear 175×15; nested forks 147×48. Sharing input
// and outcome rows trades a little width for substantially less height.
if name == "linear" && (layout.width > 108 || layout.height > 12) {
t.Fatal("linear graph grew", layout.width, layout.height)
}
if name == "nested" && (layout.width > 118 || layout.height > 41) {
if name == "nested" && (layout.width > 134 || layout.height > 32) {
t.Fatal("fork graph grew", layout.width, layout.height)
}
assertForkRouteClearance(t, layout)
@@ -36,6 +37,17 @@ func TestCompactGraphDimensions(t *testing.T) {
if !strings.Contains(view, fmt.Sprintf("%s [%d]", edge.label, edge.id)) {
t.Fatal("compaction lost outcome", edge)
}
if !edge.topEntry {
for _, node := range layout.nodes {
if node.key == edge.target {
line := renderGraph(graph)[edge.to]
input := ansi.Strip(ansi.Cut(line, node.x+2, node.x+2+len(fmt.Sprintf("[%d]", edge.id))))
if input != fmt.Sprintf("[%d]", edge.id) {
t.Fatal("paired outcome overwrote input ID", edge, input)
}
}
}
}
}
}
}
+2 -15
View File
@@ -77,11 +77,6 @@ func layoutForkGraph(graph protocol.Graph, columns map[string]int, order []strin
for _, node := range r.nodes {
old[node.key] = node
}
incoming, outgoing := map[string]int{}, map[string]int{}
for _, edge := range r.edges {
incoming[edge.target]++
outgoing[edge.source]++
}
// A deterministic spanning tree gives sibling branches disjoint leaf ranges.
// Shared descendants are subsequently centered over their incoming branches.
tree := map[string][]string{}
@@ -168,10 +163,6 @@ func layoutForkGraph(graph protocol.Graph, columns map[string]int, order []strin
}
for i := 0; i < len(graph.Stages); i++ {
node := &r.nodes[i]
node.height = incoming[node.key] + outgoing[node.key] + 4
if incoming[node.key] > 0 {
node.height++
}
// Only stacked branches set lane spacing; a tall trunk/merge must not
// inflate the spacing between every leaf in the graph.
if columnCounts[columns[node.key]] > 1 {
@@ -234,14 +225,10 @@ func layoutForkGraph(graph protocol.Graph, columns map[string]int, order []strin
sort.SliceStable(outputIDs, func(i, j int) bool { return targetRow(outputIDs[i]) < targetRow(outputIDs[j]) })
node := nodes[stage.Key]
for row, id := range inputIDs {
r.edges[id].to = node.y + 3 + row
}
divider := 0
if len(inputIDs) > 0 {
divider = 1
r.edges[id].to = node.y + 2 + row
}
for row, id := range outputIDs {
r.edges[id].from = node.y + 3 + len(inputIDs) + divider + row
r.edges[id].from = node.y + 2 + row
}
for i, edge := range r.edges {
if edge.source == stage.Key && edge.bottomExit {
+31 -17
View File
@@ -37,7 +37,7 @@ func layoutSingleRowGraph(graph protocol.Graph) graphLayout {
}
widths[e.source] = max(widths[e.source], len(fmt.Sprintf("%s [%d]", e.label, e.id))+4)
if !protocol.IsTerminal(e.target) {
widths[e.target] = max(widths[e.target], len(fmt.Sprintf("in [%d]", e.id))+4)
widths[e.target] = max(widths[e.target], len(fmt.Sprintf("[%d]", e.id))+4)
}
if !protocol.IsTerminal(e.target) && index[e.target] != index[e.source]+1 {
arcs++
@@ -47,12 +47,29 @@ func layoutSingleRowGraph(graph protocol.Graph) graphLayout {
gapStarts := make([]int, count+1)
x, mainY, height := 0, arcs, 4
for i, s := range graph.Stages {
inputs, outputs, bottom := 0, 0, 0
for _, id := range incoming[s.Key] {
inputs = max(inputs, len(fmt.Sprintf("[%d]", r.edges[id].id)))
}
sideOutputs := 0
for _, id := range outgoing[s.Key] {
e := r.edges[id]
if e.bottomExit {
bottom = 1
if len(incoming[s.Key]) > 0 {
outputs = max(outputs, len(fmt.Sprintf("%s [%d]", e.label, e.id)))
}
continue
}
sideOutputs++
outputs = max(outputs, len(fmt.Sprintf("%s [%d]", e.label, e.id)))
}
if inputs > 0 && outputs > 0 {
widths[s.Key] = max(widths[s.Key], inputs+outputs+5)
}
gapStarts[i] = x
x += gapCount[i] + 3
h := len(incoming[s.Key]) + len(outgoing[s.Key]) + 4
if len(incoming[s.Key]) > 0 {
h++
}
h := max(len(incoming[s.Key]), sideOutputs+bottom) + 3
height = max(height, h)
r.nodes = append(r.nodes, graphNode{key: s.Key, x: x, y: mainY, width: widths[s.Key], height: h})
r.positions[s.Key] = mainY
@@ -61,15 +78,12 @@ func layoutSingleRowGraph(graph protocol.Graph) graphLayout {
gapStarts[count] = x
x += gapCount[count] + 3
r.width = x
for i := range r.nodes {
r.nodes[i].height = height
}
nodes := map[string]graphNode{}
for _, n := range r.nodes {
nodes[n.key] = n
}
// Inputs occupy the first section; outcomes follow a divider. Within each
// section, upper arcs precede adjacent routes and downward terminal routes.
// Incoming IDs and outgoing labels occupy opposite sides of shared rows.
// Upper arcs precede adjacent routes; failure exits get their own bottom row.
type port struct {
edge int
input bool
@@ -99,19 +113,19 @@ func layoutSingleRowGraph(graph protocol.Graph) graphLayout {
ports = append(ports, port{id, false, rank})
}
sort.SliceStable(ports, func(i, j int) bool { return ports[i].rank < ports[j].rank })
for row, p := range ports {
inputRow, outputRow := 0, 0
for _, p := range ports {
if p.input {
r.edges[p.edge].to = mainY + 3 + row
r.edges[p.edge].to = mainY + 2 + inputRow
inputRow++
} else {
r.edges[p.edge].from = mainY + 3 + row
if len(incoming[stage.Key]) > 0 {
r.edges[p.edge].from++
}
r.edges[p.edge].from = mainY + 2 + outputRow
outputRow++
}
}
for _, id := range outgoing[stage.Key] {
if r.edges[id].bottomExit {
r.edges[id].from = mainY + height - 2
r.edges[id].from = mainY + nodes[stage.Key].height - 2
}
}
}
+6 -9
View File
@@ -110,7 +110,7 @@ func TestAdjacentFlowStaysBetweenBoxes(t *testing.T) {
if edge.outcome == "success" && !edge.topEntry {
from, to := nodes[edge.source], nodes[edge.target]
for _, point := range edge.path {
if point.x < from.x+from.width-1 || point.x > to.x || point.y < from.y+3 || point.y >= from.y+from.height-1 {
if point.x < from.x+from.width-1 || point.x > to.x || point.y < from.y+2 || point.y >= from.y+from.height-1 {
t.Fatalf("normal flow leaves the gap: %+v", edge)
}
}
@@ -191,18 +191,15 @@ func TestOrderedPortsAndBottomFailureExits(t *testing.T) {
if incoming == 0 {
continue
}
divider := node.y + 3 + incoming
title := node.y + 1
for _, edge := range layout.edges {
if edge.target == node.key && edge.to >= divider {
t.Fatal("inputs must precede the section divider")
if edge.target == node.key && (edge.to <= title || edge.to >= node.y+node.height-1) {
t.Fatal("inputs must stay within compact port rows")
}
if edge.source == node.key && edge.from <= divider {
t.Fatal("outcomes must follow the section divider")
if edge.source == node.key && edge.from <= title {
t.Fatal("outcomes must follow the title")
}
}
if !strings.Contains(renderGraph(graph)[divider], strings.Repeat("─", node.width-2)) {
t.Fatal("missing input/outcome separator")
}
}
view := renderGraph(graph)
for row := 0; row < nodes["planner"].y+nodes["planner"].height; row++ {
+1 -1
View File
@@ -181,7 +181,7 @@ func TestEdgesAreColoredIncludingInjectedErrors(t *testing.T) {
if edge.outcome == "success" && color != 2 {
t.Fatal("success outcomes must use terminal green")
}
port, row := fmt.Sprintf("in [%d]", edge.id), edge.to
port, row := fmt.Sprintf("[%d]", edge.id), edge.to
if edge.topEntry {
port = fmt.Sprintf("[%d]", edge.id)
row--
+6 -4
View File
@@ -191,14 +191,16 @@ func graphViewport(label string, lines []string, width, height, scrollTop, pan i
rows := max(0, height-5)
top := max(0, min(scrollTop, max(0, len(lines)-rows)))
end := min(len(lines), top+rows)
body := make([]string, 0, end-top)
for _, line := range lines[top:end] {
body = append(body, ansi.Cut(line, pan, pan+inner))
}
graphWidth := 0
for _, line := range lines {
graphWidth = max(graphWidth, ansi.StringWidth(line))
}
pan = max(0, min(pan, max(0, graphWidth-inner)))
leftPad, topPad := max(0, (inner-graphWidth)/2), max(0, (rows-len(lines))/2)
body := make([]string, topPad)
for _, line := range lines[top:end] {
body = append(body, strings.Repeat(" ", leftPad)+ansi.Cut(line, pan, pan+inner-leftPad))
}
if pan > 0 {
label = "← " + label
}