package tui import ( "git.a-wan.net/alan/pipelines/internal/protocol" "sort" ) // Layer acyclic graphs by dependency depth, not declaration order. In cyclic // graphs, forward branches get lanes while feedback routes retain upper arcs. func forkColumns(graph protocol.Graph) (map[string]int, []string, map[string][]string, map[string][]string, bool) { indices := map[string]int{} for i, stage := range graph.Stages { indices[stage.Key] = i } children, parents := map[string][]string{}, map[string][]string{} fork := false for _, stage := range graph.Stages { seen := map[string]bool{} for _, target := range stage.Edges { if _, exists := indices[target]; !exists || seen[target] { continue } seen[target] = true children[stage.Key] = append(children[stage.Key], target) parents[target] = append(parents[target], stage.Key) } sort.Slice(children[stage.Key], func(i, j int) bool { return indices[children[stage.Key][i]] < indices[children[stage.Key][j]] }) if len(children[stage.Key]) > 1 { fork = true } } if !fork { return nil, nil, nil, nil, false } remaining := map[string]int{} queue := []string{} for _, stage := range graph.Stages { remaining[stage.Key] = len(parents[stage.Key]) if remaining[stage.Key] == 0 { queue = append(queue, stage.Key) } } columns := map[string]int{} order := []string{} for len(queue) > 0 { sort.Slice(queue, func(i, j int) bool { return indices[queue[i]] < indices[queue[j]] }) key := queue[0] queue = queue[1:] order = append(order, key) for _, target := range children[key] { columns[target] = max(columns[target], columns[key]+1) remaining[target]-- if remaining[target] == 0 { queue = append(queue, target) } } } if len(order) != len(graph.Stages) { forward := protocol.Graph{} for _, stage := range graph.Stages { copy := protocol.Stage{Key: stage.Key, Edges: map[string]string{}} for outcome, target := range stage.Edges { if protocol.IsTerminal(target) || indices[target] > indices[stage.Key] { copy.Edges[outcome] = target } } forward.Stages = append(forward.Stages, copy) } return forkColumns(forward) } return columns, order, children, parents, true } func layoutForkGraph(graph protocol.Graph, columns map[string]int, order []string, children, parents map[string][]string) graphLayout { r := layoutSingleRowGraph(graph) // Reuse numbering, sizes, port order, terminals. old := map[string]graphNode{} for _, node := range r.nodes { old[node.key] = node } // A deterministic spanning tree gives sibling branches disjoint leaf ranges. // Shared descendants are subsequently centered over their incoming branches. tree := map[string][]string{} primary := map[string]string{} for _, key := range order { for _, target := range children[key] { if _, exists := primary[target]; !exists { primary[target] = key tree[key] = append(tree[key], target) } } } ticks := map[string]int{} nextLeaf := 0 var assign func(string) (int, int) assign = func(key string) (int, int) { if len(tree[key]) == 0 { leaf := nextLeaf nextLeaf++ ticks[key] = leaf * 2 return leaf, leaf } low, high := 0, 0 for i, target := range tree[key] { a, b := assign(target) if i == 0 { low = a } high = b } ticks[key] = low + high return low, high } for _, key := range order { if _, exists := primary[key]; !exists { assign(key) } } for _, key := range order { if len(parents[key]) > 1 { low, high := ticks[parents[key][0]], ticks[parents[key][0]] for _, parent := range parents[key] { low = min(low, ticks[parent]) high = max(high, ticks[parent]) } ticks[key] = (low + high) / 2 } else if len(parents[key]) == 1 && len(children[parents[key][0]]) == 1 { ticks[key] = ticks[parents[key][0]] } } maxColumn := 0 for _, column := range columns { maxColumn = max(maxColumn, column) } outCount, inCount := make([]int, maxColumn+2), make([]int, maxColumn+2) arcs := 0 for _, edge := range r.edges { outCount[columns[edge.source]+1]++ if !protocol.IsTerminal(edge.target) && columns[edge.target] != columns[edge.source]+1 { inCount[columns[edge.target]]++ arcs++ } } gaps, xs := make([]int, maxColumn+2), make([]int, maxColumn+1) widths := make([]int, maxColumn+1) for i := 0; i < len(graph.Stages); i++ { node := r.nodes[i] widths[columns[node.key]] = max(widths[columns[node.key]], node.width) } x := 0 for column := 0; column <= maxColumn; column++ { gaps[column] = x x += outCount[column] + inCount[column] + 3 xs[column] = x x += widths[column] } gaps[maxColumn+1] = x x += outCount[maxColumn+1] + 3 r.width = x maxHeight := 0 columnCounts := make([]int, maxColumn+1) for _, stage := range graph.Stages { columnCounts[columns[stage.Key]]++ } for i := 0; i < len(graph.Stages); i++ { node := &r.nodes[i] // 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 { maxHeight = max(maxHeight, node.height) } } stride := maxHeight + 2 for i := 0; i < len(graph.Stages); i++ { node := &r.nodes[i] node.x = xs[columns[node.key]] node.width = widths[columns[node.key]] node.y = arcs + ticks[node.key]*stride/2 } // Irregular cross-branch merges can otherwise share a lane with an unrelated // node. Resolve per-column collisions without changing left-to-right depth. bottom := 0 for column := 0; column <= maxColumn; column++ { ids := []int{} for i := 0; i < len(graph.Stages); i++ { if columns[r.nodes[i].key] == column { ids = append(ids, i) } } sort.SliceStable(ids, func(i, j int) bool { return r.nodes[ids[i]].y < r.nodes[ids[j]].y }) lastBottom := -2 for _, id := range ids { node := &r.nodes[id] node.y = max(node.y, lastBottom+2) lastBottom = node.y + node.height bottom = max(bottom, lastBottom) } } nodes := map[string]graphNode{} for i := 0; i < len(graph.Stages); i++ { node := r.nodes[i] nodes[node.key] = node r.positions[node.key] = node.y } // Match port order to the lanes: upper branch first, lower branch next. // This makes a split legible rather than crossing its two connectors. for _, stage := range graph.Stages { inputIDs, outputIDs := []int{}, []int{} for i, edge := range r.edges { if edge.target == stage.Key { inputIDs = append(inputIDs, i) } if edge.source == stage.Key && !edge.bottomExit { outputIDs = append(outputIDs, i) } } sort.SliceStable(inputIDs, func(i, j int) bool { return nodes[r.edges[inputIDs[i]].source].y < nodes[r.edges[inputIDs[j]].source].y }) targetRow := func(id int) int { if target, ok := nodes[r.edges[id].target]; ok { return target.y } return bottom + 1 } 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 + 2 + row } for row, id := range outputIDs { r.edges[id].from = node.y + 2 + row } for i, edge := range r.edges { if edge.source == stage.Key && edge.bottomExit { r.edges[i].from = node.y + node.height - 2 } } } usedOut, usedIn := make([]int, maxColumn+2), make([]int, maxColumn+2) arc := 0 for i := range r.edges { edge := &r.edges[i] source := nodes[edge.source] gap := columns[edge.source] + 1 edge.x = gaps[gap] + 1 + usedOut[gap] usedOut[gap]++ if protocol.IsTerminal(edge.target) { continue } target := nodes[edge.target] edge.left = columns[edge.target] <= columns[edge.source] edge.path = []graphPoint{{source.x + source.width - 1, edge.from}, {edge.x, edge.from}} if columns[edge.target] == columns[edge.source]+1 { edge.path = append(edge.path, graphPoint{edge.x, edge.to}, graphPoint{target.x, edge.to}) } else { gap := columns[edge.target] track := gaps[gap] + 1 + outCount[gap] + usedIn[gap] usedIn[gap]++ edge.path = append(edge.path, graphPoint{edge.x, arc}, graphPoint{track, arc}, graphPoint{track, edge.to}, graphPoint{target.x, edge.to}) edge.lane = arc arc++ } } failure, success := old[protocol.FailureEnd], old[protocol.SuccessEnd] pairWidth := failure.width + success.width + 4 r.width = max(r.width, pairWidth) failure.x = (r.width - pairWidth) / 2 success.x = failure.x + failure.width + 4 terminal := map[string]graphNode{protocol.FailureEnd: failure, protocol.SuccessEnd: success} stridePort := lenInt(len(r.edges)) + 3 reserved := map[int]bool{} for _, edge := range r.edges { if edge.topEntry { reserved[edge.x] = true } } ports := map[int]int{} for _, key := range []string{protocol.FailureEnd, protocol.SuccessEnd} { ids := []int{} for i, edge := range r.edges { if edge.target == key { ids = append(ids, i) } } sort.SliceStable(ids, func(i, j int) bool { return r.edges[ids[i]].x < r.edges[ids[j]].x }) candidate := terminal[key].x + 2 for _, id := range ids { for reserved[candidate] { candidate++ } ports[id] = candidate candidate += stridePort } } type span struct{ low, high int } var lanes [][]span for i := range r.edges { edge := &r.edges[i] if !edge.topEntry { continue } destination := ports[i] interval := span{min(edge.x, destination), max(edge.x, destination)} lane := 0 for ; lane < len(lanes); lane++ { free := true for _, other := range lanes[lane] { if interval.low <= other.high && other.low <= interval.high { free = false break } } if free { break } } if lane == len(lanes) { lanes = append(lanes, nil) } lanes[lane] = append(lanes[lane], interval) edge.lane = lane row := bottom + 1 + lane source := nodes[edge.source] edge.path = []graphPoint{{source.x + source.width - 1, edge.from}, {edge.x, edge.from}, {edge.x, row}, {destination, row}} if edge.bottomExit { center := source.x + source.width/2 exit := source.y + source.height - 1 edge.path = []graphPoint{{center, exit}, {center, exit + 1}, {edge.x, exit + 1}, {edge.x, row}, {destination, row}} } } endY := bottom + len(lanes) + 2 for i := len(graph.Stages); i < len(r.nodes); i++ { node := terminal[r.nodes[i].key] node.y = endY r.nodes[i] = node r.positions[node.key] = endY } for i := range r.edges { edge := &r.edges[i] if edge.topEntry { edge.to = endY last := edge.path[len(edge.path)-1] edge.path = append(edge.path, graphPoint{last.x, endY}) } } r.height = endY + 3 return r } func lenInt(value int) int { length := 1 for value >= 10 { length++ value /= 10 } return length }