Add live stage progress and centered auto-fitting run graphs

This commit is contained in:
2026-10-10 00:37:35 +01:00
parent dac34b0097
commit f8ffde92f7
5 changed files with 270 additions and 16 deletions
+80 -5
View File
@@ -5,6 +5,8 @@ import (
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/ansi"
)
// Each visit is a separate node, preserving cycles. Only recorded routes are drawn.
@@ -63,9 +65,13 @@ func (m Model) runGraphState() (protocol.Graph, string, *graphAppearance) {
}
return graph, selected, nil
}
appearance := &graphAppearance{nodes: map[string]bool{}, routes: map[graphRoute]bool{}}
appearance := &graphAppearance{nodes: map[string]bool{}, routes: map[graphRoute]bool{}, labels: map[string]string{}}
for _, stage := range graph.Stages {
appearance.labels[stage.Key] = stage.Key + " · ○ pending"
}
for _, step := range r.Steps {
appearance.nodes[step.Stage] = true
appearance.labels[step.Stage] = step.Stage + " · " + ansi.Strip(m.progress(m.stepProgressStatus(step)))
if step.Target != "" && step.Outcome != "" {
target := protocol.ResolveTarget(step.Outcome, step.Target)
appearance.routes[graphRoute{step.Stage, step.Outcome, target}] = true
@@ -90,7 +96,7 @@ func (m Model) runsView(height int) string {
if !m.runGraphFocus {
top, pan = m.runGraphFollow()
}
graph := graphViewport(m.runGraphTitle(), m.runGraphLines(max(3, m.width-4)), m.width, graphHeight, top, pan, m.runGraphFocus, "g graph/details · arrows pan · j/k scroll · dim = not executed")
graph := m.runGraphPanel(graphHeight, top, pan)
details := m.runsDetailsView(height - graphHeight)
return fitBlock(strings.Split(graph+"\n"+details, "\n"), m.width, height)
}
@@ -115,15 +121,84 @@ func (m Model) runGraphTitle() string {
}
func (m Model) runGraphFollow() (int, int) {
graph, selected, _ := m.runGraphState()
graph, selected, appearance := m.runGraphState()
if len(graph.Stages) == 0 {
return 0, 0
}
layout := layoutGraph(graph)
rows := max(1, (m.height-4)/2-5)
lines, compact := m.fittedRunGraph(max(1, m.width-4), rows)
if compact {
for i, line := range lines {
if strings.HasPrefix(ansi.Strip(line), "▸ ") {
return max(0, i-rows/2), 0
}
}
return 0, 0
}
layout := layoutGraphAppearance(graph, appearance)
for _, node := range layout.nodes {
if node.key == selected {
return node.y, max(0, node.x-(m.width-4-node.width)/2)
return max(0, node.y-rows/2), max(0, node.x-(m.width-4-node.width)/2)
}
}
return 0, 0
}
func (m Model) fittedRunGraph(width, rows int) ([]string, bool) {
lines := m.runGraphLines(width)
widest := 0
for _, line := range lines {
widest = max(widest, ansi.StringWidth(line))
}
if widest <= width && len(lines) <= rows {
return lines, false
}
graph, selected, appearance := m.runGraphState()
if appearance == nil {
return lines, false
}
// Dense overview preserves every declared branch, rather than hiding nodes
// or scaling terminal text into unreadable glyphs.
lines = nil
for _, stage := range graph.Stages {
prefix := " "
if stage.Key == selected {
prefix = "▸ "
}
line := prefix + appearance.title(stage.Key, appearance.label(stage.Key), stage.Key == selected, graph)
for _, outcome := range outcomes(stage) {
target := protocol.ResolveTarget(outcome, stage.Edges[outcome])
color, bold := appearance.edgeStyle(graphEdge{source: stage.Key, outcome: outcome, target: target})
line += " " + lipgloss.NewStyle().Foreground(color).Bold(bold).Render(outcome+" → "+nodeLabel(target))
}
lines = append(lines, strings.Split(ansi.Wrap(line, max(1, width), ""), "\n")...)
}
lines = append(lines, appearance.title(protocol.SuccessEnd, "SUCCESS", false, graph)+" "+appearance.title(protocol.FailureEnd, "FAILURE", false, graph))
return lines, true
}
func (m Model) runGraphPanel(height, top, pan int) string {
width, rows := max(1, m.width-4), max(0, height-5)
lines, compact := m.fittedRunGraph(width, rows)
widest := 0
for _, line := range lines {
widest = max(widest, ansi.StringWidth(line))
}
pan = max(0, min(pan, max(0, widest-width)))
top = max(0, min(top, max(0, len(lines)-rows)))
leftPad, topPad := max(0, (width-widest)/2), max(0, (rows-len(lines))/2)
body := make([]string, topPad)
for _, line := range lines[top:min(len(lines), top+rows-topPad)] {
body = append(body, strings.Repeat(" ", leftPad)+ansi.Cut(line, pan, pan+width-leftPad))
}
label := m.runGraphTitle()
if compact {
label += " · compact"
}
footer := "Tab pane · n/p stage · dim = not executed"
if widest > width || len(lines) > rows {
footer = "h/j/k/l pan · c center · Tab pane · n/p stage"
}
header := runPaneTitle(strings.TrimPrefix(label, "▸ "), "", width, m.runGraphFocus)
return panel(m.width, height, []string{header, ""}, body, footer, m.runGraphFocus)
}