480 lines
15 KiB
Go
480 lines
15 KiB
Go
package tui
|
||
|
||
import (
|
||
"fmt"
|
||
"sort"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/charmbracelet/lipgloss"
|
||
"github.com/charmbracelet/x/ansi"
|
||
|
||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||
)
|
||
|
||
type paneGeometry struct {
|
||
graphWidth, graphHeight, stageWidth, stageHeight int
|
||
}
|
||
|
||
func (m Model) pipelineGeometry(height int) paneGeometry {
|
||
remaining := max(0, height-3)
|
||
if remaining >= 15 {
|
||
bottom := remaining / 2
|
||
if m.stageFocus {
|
||
bottom = remaining - max(6, remaining/3)
|
||
}
|
||
return paneGeometry{graphWidth: m.width, graphHeight: remaining - bottom, stageWidth: m.width, stageHeight: bottom}
|
||
}
|
||
if m.stageFocus {
|
||
return paneGeometry{stageWidth: m.width, stageHeight: remaining}
|
||
}
|
||
return paneGeometry{graphWidth: m.width, graphHeight: remaining}
|
||
}
|
||
|
||
func fitBlock(lines []string, width, height int) string {
|
||
if height <= 0 {
|
||
return ""
|
||
}
|
||
rows := make([]string, height)
|
||
for i := range rows {
|
||
if i < len(lines) {
|
||
rows[i] = ansi.Truncate(lines[i], max(1, width), "…")
|
||
}
|
||
rows[i] += strings.Repeat(" ", max(0, width-lipgloss.Width(rows[i])))
|
||
}
|
||
return strings.Join(rows, "\n")
|
||
}
|
||
|
||
func panel(width, height int, headers, body []string, footer string, focused bool) string {
|
||
if height <= 0 || width <= 0 {
|
||
return ""
|
||
}
|
||
if width < 6 || height < len(headers)+3 {
|
||
return fitBlock(append(append([]string{}, headers...), body...), width, height)
|
||
}
|
||
inner := width - 4
|
||
contentHeight := height - len(headers) - 3
|
||
lines := append([]string{}, headers...)
|
||
visible := strings.Split(fitBlock(body, inner, contentHeight), "\n")
|
||
if contentHeight > 0 {
|
||
lines = append(lines, visible...)
|
||
}
|
||
lines = append(lines, lipgloss.NewStyle().Foreground(themeMuted).Render(footer))
|
||
color := themeMuted
|
||
if focused {
|
||
color = themeAccent
|
||
}
|
||
return lipgloss.NewStyle().Border(lipgloss.RoundedBorder()).BorderForeground(color).
|
||
Padding(0, 1).Width(width - 2).Render(fitBlock(lines, inner, height-2))
|
||
}
|
||
|
||
func bar(title, right string, width int, focused bool) string {
|
||
left := ansi.Truncate(title, max(1, width), "…")
|
||
if lipgloss.Width(left)+lipgloss.Width(right)+1 <= width {
|
||
left += strings.Repeat(" ", width-lipgloss.Width(left)-lipgloss.Width(right)) + right
|
||
}
|
||
left += strings.Repeat(" ", max(0, width-lipgloss.Width(left)))
|
||
s := lipgloss.NewStyle().Foreground(themeAccent).Bold(true)
|
||
if focused {
|
||
s = s.Reverse(true)
|
||
}
|
||
return s.Render(left)
|
||
}
|
||
|
||
func dateLabel(value string) string {
|
||
if stamp, err := time.Parse(time.RFC3339, value); err == nil {
|
||
return stamp.Format("02 Jan 2006 15:04") + " UTC"
|
||
}
|
||
if value == "" {
|
||
return "—"
|
||
}
|
||
return value
|
||
}
|
||
|
||
func (m Model) pipelineToolbar() []string {
|
||
d := m.detail
|
||
state := lipgloss.NewStyle().Foreground(themeSuccess).Bold(true).Render("LATEST")
|
||
if d.Version != d.LatestVersion {
|
||
state = lipgloss.NewStyle().Foreground(themeAccent).Render(fmt.Sprintf("HISTORY · latest v%d", d.LatestVersion))
|
||
}
|
||
buttons := action(fmt.Sprintf("v Version %d ▾", d.Version), true) + " " + action("e Edit source", false) + " " + action("Esc Home", false)
|
||
if lipgloss.Width(buttons)+lipgloss.Width(state)+1 <= m.width {
|
||
buttons += strings.Repeat(" ", m.width-lipgloss.Width(buttons)-lipgloss.Width(state)) + state
|
||
}
|
||
meta := lipgloss.NewStyle().Foreground(themeMuted).Render("Created ") + dateLabel(d.CreatedAt) + " │ " + lipgloss.NewStyle().Foreground(themeMuted).Render("Updated ") + dateLabel(d.UpdatedAt)
|
||
if settings := d.Graph.Settings; settings != nil {
|
||
var schedules []string
|
||
for _, rule := range settings.Schedules {
|
||
when := "at " + strings.Join(rule.Times, ",")
|
||
if rule.Every != "" {
|
||
when = "every " + rule.Every
|
||
}
|
||
days := "everyday"
|
||
if len(rule.Days) > 0 {
|
||
days = strings.Join(rule.Days, ",")
|
||
}
|
||
zone := rule.Timezone
|
||
if zone == "" {
|
||
zone = "server local"
|
||
}
|
||
schedules = append(schedules, days+" "+when+" ("+zone+")")
|
||
}
|
||
meta = "Schedule: " + strings.Join(schedules, "; ")
|
||
if d.Version != d.LatestVersion {
|
||
meta += " · historical, inactive"
|
||
}
|
||
}
|
||
return []string{buttons, meta, lipgloss.NewStyle().Foreground(themeMuted).Render(strings.Repeat("─", m.width))}
|
||
}
|
||
|
||
func (m Model) pipelineView(height int) string {
|
||
if m.detailLoading {
|
||
label := "Loading pipeline…"
|
||
if m.requestedVersion > 0 {
|
||
label = fmt.Sprintf("Loading version %d…", m.requestedVersion)
|
||
}
|
||
return fitBlock([]string{action(label, true)}, m.width, height)
|
||
}
|
||
if m.detailError != "" {
|
||
return fitBlock([]string{lipgloss.NewStyle().Foreground(themeError).Render("Cannot load pipeline: " + m.detailError), "Esc returns home."}, m.width, height)
|
||
}
|
||
if m.detail == nil {
|
||
return fitBlock([]string{"No pipeline selected."}, m.width, height)
|
||
}
|
||
toolbar := m.pipelineToolbar()
|
||
if height <= 3 {
|
||
return fitBlock(toolbar, m.width, height)
|
||
}
|
||
g := m.pipelineGeometry(height)
|
||
graph := m.graphPanel(g.graphWidth, g.graphHeight)
|
||
stages := m.stagePanel(g.stageWidth, g.stageHeight)
|
||
content := graph
|
||
if graph != "" && stages != "" {
|
||
content = graph + "\n" + stages
|
||
} else if stages != "" {
|
||
content = stages
|
||
}
|
||
return fitBlock(append(toolbar, strings.Split(content, "\n")...), m.width, height)
|
||
}
|
||
|
||
func (m Model) selectedStage() *protocol.Stage {
|
||
if m.detail == nil || len(m.detail.Graph.Stages) == 0 {
|
||
return nil
|
||
}
|
||
index := max(0, min(m.stageIndex, len(m.detail.Graph.Stages)-1))
|
||
return &m.detail.Graph.Stages[index]
|
||
}
|
||
|
||
func (m Model) graphLines() []string {
|
||
if m.detail == nil {
|
||
return nil
|
||
}
|
||
selected := m.selectedStage()
|
||
if selected == nil {
|
||
return renderGraph(m.detail.Graph)
|
||
}
|
||
return renderGraph(m.detail.Graph, selected.Key)
|
||
}
|
||
|
||
func (m Model) graphPanel(width, height int) string {
|
||
if width <= 0 || height <= 0 {
|
||
return ""
|
||
}
|
||
inner := max(1, width-4)
|
||
rows := max(0, height-5)
|
||
lines := m.graphLines()
|
||
top := max(0, min(m.pipelineTop, 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, m.graphPan, m.graphPan+inner))
|
||
}
|
||
graphWidth := 0
|
||
for _, line := range lines {
|
||
graphWidth = max(graphWidth, ansi.StringWidth(line))
|
||
}
|
||
label := "Graph"
|
||
if !m.stageFocus {
|
||
label = "▸ " + label
|
||
}
|
||
if m.graphPan > 0 {
|
||
label = "← " + label
|
||
}
|
||
if m.graphPan+inner < graphWidth {
|
||
label += " →"
|
||
}
|
||
headers := []string{bar(label, fmt.Sprintf("%d–%d / %d", min(top+1, len(lines)), end, len(lines)), inner, !m.stageFocus), lipgloss.NewStyle().Foreground(themeMuted).Render(strings.Repeat("─", inner))}
|
||
footer := "Tab pane · r runs · * fallback error"
|
||
if graphWidth > inner {
|
||
footer = "←/→ pan · Tab pane · * fallback error"
|
||
}
|
||
return panel(width, height, headers, body, footer, !m.stageFocus)
|
||
}
|
||
|
||
func (m Model) stageTabs(width int) string {
|
||
if m.selectedStage() == nil {
|
||
return "No stages"
|
||
}
|
||
stages := m.detail.Graph.Stages
|
||
index := max(0, min(m.stageIndex, len(stages)-1))
|
||
if width < 4 {
|
||
return stageTab(stages[index].Key, ansi.Truncate(fmt.Sprintf("%d %s", index+1, stages[index].Key), max(1, width), "…"), true, m.detail.Graph)
|
||
}
|
||
start := index
|
||
labelFor := func(i int) string {
|
||
label := fmt.Sprintf("%d %s", i+1, stages[i].Key)
|
||
if i == index && width < lipgloss.Width(label)+2 {
|
||
return stages[i].Key
|
||
}
|
||
return label
|
||
}
|
||
used := min(width, lipgloss.Width(labelFor(index))+2)
|
||
for start > 0 {
|
||
prior := lipgloss.Width(labelFor(start-1)) + 2
|
||
marker := 0
|
||
if start > 1 {
|
||
marker = 2
|
||
}
|
||
if used+prior+marker > width {
|
||
break
|
||
}
|
||
start--
|
||
used += prior
|
||
}
|
||
result := ""
|
||
if start > 0 && used+2 <= width {
|
||
result = lipgloss.NewStyle().Foreground(themeMuted).Render("‹ ")
|
||
}
|
||
for i := start; i < len(stages); i++ {
|
||
label := labelFor(i)
|
||
if i == index {
|
||
label = ansi.Truncate(label, max(1, width-lipgloss.Width(result)-2), "…")
|
||
}
|
||
tab := stageTab(stages[i].Key, " "+label+" ", i == index, m.detail.Graph)
|
||
if lipgloss.Width(result)+lipgloss.Width(tab) > width {
|
||
if i > index && lipgloss.Width(result)+2 <= width {
|
||
result += " ›"
|
||
}
|
||
break
|
||
}
|
||
result += tab
|
||
}
|
||
return ansi.Truncate(result, width, "…")
|
||
}
|
||
|
||
type stageField struct{ title, text string }
|
||
|
||
func (m Model) stageFields() []stageField {
|
||
stage := m.selectedStage()
|
||
if stage == nil {
|
||
return nil
|
||
}
|
||
keys := make([]string, 0, len(stage.Configs))
|
||
for k := range stage.Configs {
|
||
keys = append(keys, k)
|
||
}
|
||
sort.Strings(keys)
|
||
configs := make([]string, 0, len(keys))
|
||
if stage.Type != "" {
|
||
configs = append(configs, lipgloss.NewStyle().Foreground(themeMuted).Render("type ")+stage.Type)
|
||
}
|
||
for _, k := range keys {
|
||
configs = append(configs, lipgloss.NewStyle().Foreground(themeMuted).Render(k+" ")+stage.Configs[k])
|
||
}
|
||
var edges []graphEdge
|
||
maxLabel := 0
|
||
for _, edge := range graphEdges(m.detail.Graph) {
|
||
if edge.source != stage.Key {
|
||
continue
|
||
}
|
||
edges = append(edges, edge)
|
||
maxLabel = max(maxLabel, lipgloss.Width(outcomeLabel(edge.outcome, fmt.Sprintf("%s [%d]", edge.label, edge.id))))
|
||
}
|
||
routes := make([]string, 0, len(edges))
|
||
for _, edge := range edges {
|
||
label := outcomeLabel(edge.outcome, fmt.Sprintf("%s [%d]", edge.label, edge.id))
|
||
padding := strings.Repeat(" ", maxLabel-lipgloss.Width(label))
|
||
target := nodeLabel(edge.target)
|
||
routes = append(routes, label+padding+" "+lipgloss.NewStyle().Foreground(themeMuted).Render("→")+" "+stageTitle(edge.target, target, false, m.detail.Graph))
|
||
}
|
||
fields := []stageField{{"OUTPUT", ansi.Strip(stage.Output)}}
|
||
if stage.Type == "" || stage.Type == "model" {
|
||
fields = append(fields, stageField{"PROMPT", ansi.Strip(stage.Prompt)})
|
||
}
|
||
fields = append(fields, stageField{"CONFIGURATION", strings.Join(configs, "\n")}, stageField{"OUTCOMES", strings.Join(routes, "\n")})
|
||
if tools, ok := stage.Blocks["tools"]; ok {
|
||
if strings.TrimSpace(tools) == "" {
|
||
tools = "None"
|
||
}
|
||
fields = append(fields, stageField{"TOOLS", ansi.Strip(tools)})
|
||
}
|
||
if stage.Notify != nil {
|
||
fields = append(fields, stageField{"NOTIFY", "On: " + strings.Join(stage.Notify.On, ", ") + "\nConnection: global\nArtifacts: " + strings.Join(stage.Notify.Artifacts, ", ")})
|
||
}
|
||
return fields
|
||
}
|
||
|
||
func (m Model) stageColumns(width int) (string, []string) {
|
||
header, rows, _ := m.stageColumnsWithHeaders(width)
|
||
return header, rows
|
||
}
|
||
|
||
func (m Model) stageColumnsWithHeaders(width int) (string, []string, []string) {
|
||
fields := m.stageFields()
|
||
if width < 110 {
|
||
var compact []stageField
|
||
var capabilities []string
|
||
for _, field := range fields {
|
||
if field.title == "TOOLS" || field.title == "NOTIFY" {
|
||
capabilities = append(capabilities, field.title+"\n"+field.text)
|
||
} else {
|
||
compact = append(compact, field)
|
||
}
|
||
}
|
||
for i := range compact {
|
||
if compact[i].title == "CONFIGURATION" && len(capabilities) > 0 {
|
||
compact[i].text = strings.Join(capabilities, "\n\n") + "\n\n" + compact[i].text
|
||
}
|
||
}
|
||
fields = compact
|
||
}
|
||
if len(fields) == 0 {
|
||
return "Stages", []string{"No stages."}, []string{"Stages"}
|
||
}
|
||
count := 4
|
||
if width >= 110 && len(fields) > 4 {
|
||
count = 5
|
||
}
|
||
if width < 90 {
|
||
count = 4
|
||
}
|
||
if width < 68 {
|
||
count = 3
|
||
}
|
||
if width < 48 {
|
||
count = 2
|
||
}
|
||
if width < 28 {
|
||
count = 1
|
||
}
|
||
columnWidth := max(1, (width-(count-1)*3)/count)
|
||
separator := lipgloss.NewStyle().Foreground(themeMuted).Render(" │ ")
|
||
var header string
|
||
var result []string
|
||
var rowHeaders []string
|
||
for start := 0; start < len(fields); start += count {
|
||
end := min(len(fields), start+count)
|
||
columns, labels := []string{}, []string{}
|
||
height := 1
|
||
for _, field := range fields[start:end] {
|
||
rows := strings.Split(ansi.Wrap(field.text, columnWidth, ""), "\n")
|
||
height = max(height, len(rows))
|
||
columns = append(columns, strings.Join(rows, "\n"))
|
||
labels = append(labels, fitBlock([]string{lipgloss.NewStyle().Foreground(themeAccent).Bold(true).Render(field.title)}, columnWidth, 1))
|
||
}
|
||
label := strings.Join(labels, separator)
|
||
if start == 0 {
|
||
header = label
|
||
} else {
|
||
result = append(result, "", label)
|
||
rowHeaders = append(rowHeaders, label, label)
|
||
}
|
||
for i := range columns {
|
||
columns[i] = fitBlock(strings.Split(columns[i], "\n"), columnWidth, height)
|
||
}
|
||
joined := columns[0]
|
||
divider := strings.TrimSuffix(strings.Repeat(separator+"\n", height), "\n")
|
||
for _, column := range columns[1:] {
|
||
joined = lipgloss.JoinHorizontal(lipgloss.Top, joined, divider, column)
|
||
}
|
||
joinedRows := strings.Split(joined, "\n")
|
||
result = append(result, joinedRows...)
|
||
for range joinedRows {
|
||
rowHeaders = append(rowHeaders, label)
|
||
}
|
||
}
|
||
return header, result, rowHeaders
|
||
}
|
||
|
||
func (m Model) stageLines(width int) []string {
|
||
_, rows := m.stageColumns(width)
|
||
return rows
|
||
}
|
||
|
||
func (m Model) stagePanel(width, height int) string {
|
||
if width <= 0 || height <= 0 {
|
||
return ""
|
||
}
|
||
inner := max(1, width-4)
|
||
rows := max(0, height-6)
|
||
columns, lines, rowHeaders := m.stageColumnsWithHeaders(inner)
|
||
top := max(0, min(m.stageTop, max(0, len(lines)-rows)))
|
||
if top < len(rowHeaders) {
|
||
columns = rowHeaders[top]
|
||
}
|
||
label := "Stages"
|
||
if m.stageFocus {
|
||
label = "▸ " + label
|
||
}
|
||
headers := []string{bar(label, fmt.Sprintf("%d / %d · rows %d–%d/%d", m.stageIndex+1, len(m.detail.Graph.Stages), min(top+1, len(lines)), min(len(lines), top+rows), len(lines)), inner, m.stageFocus), m.stageTabs(inner), columns}
|
||
return panel(width, height, headers, lines[top:min(len(lines), top+rows)], "n/p stage · Tab pane · j/k scroll · PgUp/Dn page", m.stageFocus)
|
||
}
|
||
|
||
func (m *Model) resetPipelineView() {
|
||
m.pipelineTop = 0
|
||
m.graphPan = 0
|
||
m.stageIndex = 0
|
||
m.stageTop = 0
|
||
m.stageFocus = false
|
||
m.revealStage()
|
||
}
|
||
|
||
func (m *Model) revealStage() {
|
||
if m.detail == nil {
|
||
return
|
||
}
|
||
if stage := m.selectedStage(); stage != nil {
|
||
row := graphStageRow(m.detail.Graph, stage.Key)
|
||
page := max(1, m.pipelineGeometry(max(0, m.height-4)).graphHeight-5)
|
||
if row < m.pipelineTop {
|
||
m.pipelineTop = row
|
||
} else if row >= m.pipelineTop+page {
|
||
m.pipelineTop = row - page + 1
|
||
}
|
||
if !compactGraph(m.detail.Graph) {
|
||
width := max(1, m.pipelineGeometry(max(0, m.height-4)).graphWidth-4)
|
||
for _, node := range layoutGraph(m.detail.Graph).nodes {
|
||
if node.key != stage.Key {
|
||
continue
|
||
}
|
||
visible := min(page, node.height)
|
||
if node.y < m.pipelineTop {
|
||
m.pipelineTop = node.y
|
||
} else if node.y+visible > m.pipelineTop+page {
|
||
m.pipelineTop = node.y + visible - page
|
||
}
|
||
if node.x < m.graphPan {
|
||
m.graphPan = node.x
|
||
} else if node.x+node.width > m.graphPan+width {
|
||
m.graphPan = node.x + node.width - width
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
func (m *Model) clampPipelineScroll() {
|
||
if m.detail == nil {
|
||
return
|
||
}
|
||
m.stageIndex = max(0, min(m.stageIndex, max(0, len(m.detail.Graph.Stages)-1)))
|
||
g := m.pipelineGeometry(max(0, m.height-4))
|
||
m.pipelineTop = max(0, min(m.pipelineTop, max(0, len(m.graphLines())-max(1, g.graphHeight-5))))
|
||
m.stageTop = max(0, min(m.stageTop, max(0, len(m.stageLines(max(1, g.stageWidth-4)))-max(1, g.stageHeight-6))))
|
||
width := 0
|
||
for _, line := range m.graphLines() {
|
||
width = max(width, lipgloss.Width(line))
|
||
}
|
||
m.graphPan = max(0, min(m.graphPan, max(0, width-max(1, g.graphWidth-4))))
|
||
}
|