274 lines
7.4 KiB
Go
274 lines
7.4 KiB
Go
package tui
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"github.com/charmbracelet/lipgloss"
|
|
"github.com/charmbracelet/x/ansi"
|
|
)
|
|
|
|
func outcomes(stage protocol.Stage) []string {
|
|
keys := make([]string, 0, len(stage.Edges))
|
|
for key := range stage.Edges {
|
|
keys = append(keys, key)
|
|
}
|
|
sort.Slice(keys, func(i, j int) bool {
|
|
priority := func(k string) int {
|
|
if k == "success" {
|
|
return 0
|
|
}
|
|
if k == "error" {
|
|
return 2
|
|
}
|
|
return 1
|
|
}
|
|
if a, b := priority(keys[i]), priority(keys[j]); a != b {
|
|
return a < b
|
|
}
|
|
return keys[i] < keys[j]
|
|
})
|
|
return keys
|
|
}
|
|
|
|
func compactGraph(graph protocol.Graph) bool {
|
|
edges := 0
|
|
for _, stage := range graph.Stages {
|
|
edges += len(stage.Edges)
|
|
}
|
|
return len(graph.Stages) > 200 || edges > 500
|
|
}
|
|
|
|
type graphPoint struct{ x, y int }
|
|
type graphEdge struct {
|
|
id int
|
|
source, target, outcome, label string
|
|
from, to, lane, x int
|
|
left bool
|
|
path []graphPoint
|
|
topEntry bool
|
|
bottomExit bool
|
|
}
|
|
type graphNode struct {
|
|
key string
|
|
x, y, width, height int
|
|
}
|
|
type graphLayout struct {
|
|
nodes []graphNode
|
|
edges []graphEdge
|
|
nodeX, nodeWidth, width, height int
|
|
positions map[string]int
|
|
}
|
|
|
|
func graphEdges(graph protocol.Graph) []graphEdge {
|
|
index := map[string]int{protocol.SuccessEnd: len(graph.Stages), protocol.FailureEnd: len(graph.Stages) + 1}
|
|
for i, s := range graph.Stages {
|
|
index[s.Key] = i
|
|
}
|
|
var edges []graphEdge
|
|
for i, stage := range graph.Stages {
|
|
for _, outcome := range outcomes(stage) {
|
|
label := outcome
|
|
if outcome == "error" && stage.FallbackError {
|
|
label += "*"
|
|
}
|
|
target := protocol.ResolveTarget(outcome, stage.Edges[outcome])
|
|
edges = append(edges, graphEdge{id: len(edges) + 1, source: stage.Key, target: target, outcome: outcome, label: label, left: index[target] <= i})
|
|
}
|
|
}
|
|
return edges
|
|
}
|
|
|
|
func graphStageRow(graph protocol.Graph, key string) int {
|
|
if !compactGraph(graph) {
|
|
return layoutGraph(graph).positions[key]
|
|
}
|
|
row := 0
|
|
for _, stage := range graph.Stages {
|
|
if stage.Key == key {
|
|
return row
|
|
}
|
|
row += len(stage.Edges) + 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func renderGraph(graph protocol.Graph, selection ...string) []string {
|
|
if len(graph.Stages) == 0 {
|
|
return []string{"(empty graph)"}
|
|
}
|
|
selected := ""
|
|
if len(selection) > 0 {
|
|
selected = selection[0]
|
|
}
|
|
if compactGraph(graph) {
|
|
var lines []string
|
|
edges, index := graphEdges(graph), 0
|
|
for _, stage := range graph.Stages {
|
|
lines = append(lines, stageTitle(stage.Key, "["+stage.Key+"]", stage.Key == selected, graph))
|
|
for range outcomes(stage) {
|
|
e := edges[index]
|
|
index++
|
|
lines = append(lines, " "+outcomeLabel(e.outcome, fmt.Sprintf("└─ %s [%d] → ", e.label, e.id))+stageTitle(e.target, "["+nodeLabel(e.target)+"]", false, graph))
|
|
}
|
|
}
|
|
return append(lines, stageTitle(protocol.FailureEnd, "[FAILURE]", false)+" "+stageTitle(protocol.SuccessEnd, "[SUCCESS]", false))
|
|
}
|
|
layout := layoutGraph(graph)
|
|
type cell struct {
|
|
glyph rune
|
|
color lipgloss.Color
|
|
bold bool
|
|
}
|
|
canvas := make([][]cell, layout.height)
|
|
for row := range canvas {
|
|
canvas[row] = make([]cell, layout.width)
|
|
for x := range canvas[row] {
|
|
canvas[row][x].glyph = ' '
|
|
}
|
|
}
|
|
put := func(x, y int, r rune, color lipgloss.Color, bold bool) { canvas[y][x] = cell{r, color, bold} }
|
|
text := func(x, y int, s string, color lipgloss.Color, bold bool) {
|
|
for _, r := range s {
|
|
put(x, y, r, color, bold)
|
|
x++
|
|
}
|
|
}
|
|
nodes := map[string]graphNode{}
|
|
for _, node := range layout.nodes {
|
|
nodes[node.key] = node
|
|
x, w, y, h := node.x, node.width, node.y, node.height
|
|
color := stageColor(node.key, graph)
|
|
active := node.key == selected
|
|
top, bottom, vertical := "╭"+strings.Repeat("─", w-2)+"╮", "╰"+strings.Repeat("─", w-2)+"╯", '│'
|
|
if active {
|
|
top = "╔" + strings.Repeat("═", w-2) + "╗"
|
|
bottom = "╚" + strings.Repeat("═", w-2) + "╝"
|
|
vertical = '║'
|
|
}
|
|
text(x, y, top, color, active)
|
|
for row := y + 1; row < y+h-1; row++ {
|
|
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) {
|
|
// Edges are drawn in stable ID order. The earlier route stays intact at
|
|
// every crossing, including its color; never invent a joining glyph.
|
|
if canvas[p.y][p.x].glyph == ' ' {
|
|
put(p.x, p.y, r, color, false)
|
|
}
|
|
}
|
|
for _, edge := range layout.edges {
|
|
color := outcomeColor(edge.outcome)
|
|
path := []graphPoint{}
|
|
for i := 1; i < len(edge.path); i++ {
|
|
a, b := edge.path[i-1], edge.path[i]
|
|
dx, dy := 0, 0
|
|
if b.x > a.x {
|
|
dx = 1
|
|
} else if b.x < a.x {
|
|
dx = -1
|
|
}
|
|
if b.y > a.y {
|
|
dy = 1
|
|
} else if b.y < a.y {
|
|
dy = -1
|
|
}
|
|
if len(path) == 0 {
|
|
path = append(path, a)
|
|
}
|
|
for a != b {
|
|
a.x += dx
|
|
a.y += dy
|
|
path = append(path, a)
|
|
}
|
|
}
|
|
for i := 1; i < len(path)-1; i++ {
|
|
p, a, b := path[i], path[i-1], path[i+1]
|
|
glyph := '─'
|
|
if a.x == b.x {
|
|
glyph = '│'
|
|
} else if a.y != b.y {
|
|
left, right, up, down := a.x < p.x || b.x < p.x, a.x > p.x || b.x > p.x, a.y < p.y || b.y < p.y, a.y > p.y || b.y > p.y
|
|
switch {
|
|
case right && down:
|
|
glyph = '┌'
|
|
case left && down:
|
|
glyph = '┐'
|
|
case right && up:
|
|
glyph = '└'
|
|
case left && up:
|
|
glyph = '┘'
|
|
}
|
|
}
|
|
line(p, glyph, color)
|
|
}
|
|
start, end := path[0], path[len(path)-1]
|
|
if edge.bottomExit {
|
|
put(start.x, start.y, '▼', color, true)
|
|
} else {
|
|
put(start.x, start.y, '├', color, false)
|
|
put(start.x+1, start.y, '→', color, false)
|
|
}
|
|
if edge.topEntry {
|
|
put(end.x, end.y, '▼', color, true)
|
|
text(end.x-1, end.y-1, fmt.Sprintf("[%d]", edge.id), color, true)
|
|
} else {
|
|
put(end.x, end.y, '▶', color, true)
|
|
target := nodes[edge.target]
|
|
text(target.x+2, edge.to, fmt.Sprintf("in [%d]", edge.id), color, true)
|
|
}
|
|
source := nodes[edge.source]
|
|
text(source.x+2, edge.from, fmt.Sprintf("%s [%d]", edge.label, edge.id), color, true)
|
|
}
|
|
result := make([]string, layout.height)
|
|
for row, cells := range canvas {
|
|
var b strings.Builder
|
|
end := len(cells)
|
|
for end > 0 && cells[end-1].glyph == ' ' {
|
|
end--
|
|
}
|
|
for x := 0; x < end; {
|
|
current, next := cells[x], x+1
|
|
for next < end && cells[next].color == current.color && cells[next].bold == current.bold {
|
|
next++
|
|
}
|
|
var run strings.Builder
|
|
for i := x; i < next; i++ {
|
|
run.WriteRune(cells[i].glyph)
|
|
}
|
|
style := lipgloss.NewStyle().Bold(current.bold)
|
|
if current.color != "" {
|
|
style = style.Foreground(current.color)
|
|
}
|
|
b.WriteString(style.Render(run.String()))
|
|
x = next
|
|
}
|
|
result[row] = b.String()
|
|
}
|
|
for _, node := range layout.nodes {
|
|
label, row := nodeLabel(node.key), node.y+1
|
|
left := ansi.Cut(result[row], 0, node.x+1)
|
|
title := " " + label + strings.Repeat(" ", node.width-3-ansi.StringWidth(label))
|
|
right := ansi.Cut(result[row], node.x+node.width-1, layout.width)
|
|
result[row] = left + stageTitle(node.key, title, node.key == selected, graph) + right
|
|
}
|
|
return result
|
|
}
|