Files
pipelines/internal/tui/pipeline.go
T
alan c42a19f7d3 Improve workflow UI, retain stage diffs, and add portable pipeline transfers
Make OpenCode authoring responsive and expand the managed skill references. Add conflict-aware import/export, real-provider reference workflows, and tracked V2 generation for reliable budget accounting.
2026-10-10 20:55:42 +01:00

443 lines
13 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package tui
import (
"fmt"
"strconv"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/sahilm/fuzzy"
"git.a-wan.net/alan/pipelines/internal/draft"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func selectionWindow(count, index, capacity int) (int, int) {
start := max(0, min(index-capacity+1, max(0, count-capacity)))
return start, min(count, start+capacity)
}
func (m Model) searchMatches() []protocol.PipelineSummary {
query := strings.TrimPrefix(strings.TrimSpace(m.commandInput.Value()), "/")
if query == "" {
return m.pipelines
}
names := make([]string, len(m.pipelines))
for i, p := range m.pipelines {
names[i] = p.Name
}
result := []protocol.PipelineSummary{}
for _, match := range fuzzy.Find(query, names) {
result = append(result, m.pipelines[match.Index])
}
return result
}
func (m Model) updateSearch(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
items := m.searchMatches()
switch msg.String() {
case "esc":
m.paletteOpen = false
m.searchOpen = false
m.keys.palette = false
return m, nil
case "down", "ctrl+n":
m.commandIndex = min(m.commandIndex+1, max(0, len(items)-1))
return m, nil
case "up", "ctrl+p":
m.commandIndex = max(0, m.commandIndex-1)
return m, nil
case "enter":
if len(items) == 0 {
return m, nil
}
selected := items[min(m.commandIndex, len(items)-1)]
m.paletteOpen = false
m.searchOpen = false
m.keys.palette = false
return m.requestPipeline(selected.ID, 0)
}
var cmd tea.Cmd
m.commandInput, cmd = m.commandInput.Update(msg)
m.commandIndex = 0
return m, cmd
}
func (m Model) searchView(height int) string {
field := m.commandInput
field.Width = max(1, min(54, m.width-10))
lines := []string{"Search pipelines", field.View(), ""}
items := m.searchMatches()
if len(items) == 0 {
lines = append(lines, "No matching pipelines")
}
start, end := selectionWindow(len(items), m.commandIndex, max(1, height-5))
for i := start; i < end; i++ {
prefix := " "
if i == m.commandIndex {
prefix = "› "
}
lines = append(lines, menuOption(fmt.Sprintf("%s%s · v%d · %d stages", prefix, items[i].Name, items[i].Version, items[i].Stages), i == m.commandIndex))
}
return popup(m.width, height, 64, lines)
}
func (m Model) requestPipeline(id int64, version int) (tea.Model, tea.Cmd) {
m.requestedPipelineID = id
m.requestedVersion = version
m.detailLoading = true
m.detailError = ""
m.keys.detail = true
if m.conn == nil {
m.status = "Reconnecting…"
m.detailRequest = ""
if m.reconnecting {
return m, nil
}
m.reconnecting = true
return m, m.connect()
}
m.nextID++
m.detailRequest = strconv.FormatUint(m.nextID, 10)
m.detailLoading = true
m.detailError = ""
m.keys.detail = true
return m, m.send(protocol.Message{Type: protocol.GetPipeline, ID: m.detailRequest, PipelineID: id, Version: version})
}
func (m Model) goHome() Model {
m.requestedPipelineID = 0
m.requestedVersion = 0
m.keys.detail = false
m.detail = nil
m.detailRequest = ""
m.detailLoading = false
m.detailError = ""
m.pipelineTop = 0
m.graphPan = 0
m.stageIndex = 0
m.stageTop = 0
m.stageFocus = false
m.versionsOpen = false
m.keys.versions = false
return m
}
func (m Model) openVersions() (tea.Model, tea.Cmd) {
if m.detail == nil || m.detailLoading {
m.appendLine("open a pipeline first")
return m, nil
}
m.versionsOpen = true
m.keys.versions = true
m.versionIndex = 0
for i, v := range m.detail.Versions {
if v.Version == m.detail.Version {
m.versionIndex = i
break
}
}
return m, nil
}
func (m Model) updateVersions(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
versions := m.detail.Versions
switch msg.String() {
case "esc":
m.versionsOpen = false
m.keys.versions = false
return m, nil
case "j", "down":
m.versionIndex = min(m.versionIndex+1, max(0, len(versions)-1))
case "k", "up":
m.versionIndex = max(0, m.versionIndex-1)
case "enter":
if len(versions) == 0 {
return m, nil
}
m.versionsOpen = false
m.keys.versions = false
return m.requestPipeline(m.detail.ID, versions[m.versionIndex].Version)
}
return m, nil
}
func (m Model) versionsView(height int) string {
lines := []string{"Versions · " + m.detail.Name, "Select a version to view or edit", ""}
start, end := selectionWindow(len(m.detail.Versions), m.versionIndex, max(1, height-5))
for i := start; i < end; i++ {
v := m.detail.Versions[i]
prefix := " "
if i == m.versionIndex {
prefix = "› "
}
label := ""
if v.Version == m.detail.Version {
label += " · viewing"
}
if v.Version == m.detail.LatestVersion {
label += " · latest"
}
lines = append(lines, menuOption(fmt.Sprintf("%sv%d · %d stages · %s%s", prefix, v.Version, v.Stages, dateLabel(v.CreatedAt), label), i == m.versionIndex))
}
return popup(m.width, height, 76, lines)
}
func (m Model) editPipeline() (tea.Model, tea.Cmd) {
if m.detail == nil || m.detailLoading {
m.appendLine("open a pipeline first")
return m, nil
}
if m.submitID != "" {
m.appendLine("submission already in progress")
return m, nil
}
return m.openAuthorPicker(editedMsg{name: m.detail.Name, source: pipeline.ExecutionSource(m.detail.Source), baseVersion: m.detail.LatestVersion})
}
func (m Model) updatePipeline(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "esc":
return m.goHome(), nil
case "v":
return m.openVersions()
case "r":
return m.openRuns()
case "c", "enter":
if m.stageFocus {
m.stageConfigExpanded = !m.stageConfigExpanded
if m.stageConfigExpanded {
_, _, labels := m.stageColumnsWithHeaders(max(1, m.width-4))
for i, label := range labels {
if strings.Contains(label, "CONFIGURATION") {
m.stageTop = i
break
}
}
}
} else {
m.revealStage()
}
case "e":
return m.editPipeline()
case "s":
if m.pendingDraft != nil {
m.confirmOpen = true
m.keys.confirm = true
}
case "j", "down":
if m.stageFocus {
m.stageTop++
} else {
m.pipelineTop++
}
case "k", "up":
if m.stageFocus {
m.stageTop--
} else {
m.pipelineTop--
}
case "ctrl+d", "pgdown":
g := m.pipelineGeometry(max(0, m.height-4))
if m.stageFocus {
m.stageTop += max(1, (g.stageHeight-6)/2)
} else {
m.pipelineTop += max(1, (g.graphHeight-5)/2)
}
case "ctrl+u", "pgup":
g := m.pipelineGeometry(max(0, m.height-4))
if m.stageFocus {
m.stageTop -= max(1, (g.stageHeight-6)/2)
} else {
m.pipelineTop -= max(1, (g.graphHeight-5)/2)
}
case "tab", "shift+tab":
m.stageFocus = !m.stageFocus
if !m.stageFocus {
m.revealStage()
}
case "1", "2", "3", "4", "5", "6", "7", "8", "9":
if m.detail != nil && len(m.detail.Graph.Stages) > 0 {
index := int(msg.String()[0] - '1')
if index >= len(m.detail.Graph.Stages) {
break
}
m.stageIndex = index
m.stageTop = 0
m.revealStage()
}
case "[", "]", "n", "p":
if m.detail != nil && len(m.detail.Graph.Stages) > 0 {
delta := 1
if msg.String() == "[" || msg.String() == "p" {
delta = -1
}
m.stageIndex = (m.stageIndex + delta + len(m.detail.Graph.Stages)) % len(m.detail.Graph.Stages)
m.stageTop = 0
m.revealStage()
}
case "h", "left":
m.graphPan = max(0, m.graphPan-4)
case "l", "right":
width := 0
for _, line := range m.graphLines() {
width = max(width, lipgloss.Width(line))
}
m.graphPan = min(m.graphPan+4, max(0, width-max(1, m.pipelineGeometry(max(0, m.height-4)).graphWidth-4)))
}
m.clampPipelineScroll()
return m, nil
}
func (m Model) updateConfirm(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
if msg.String() == "esc" {
m.confirmOpen = false
m.keys.confirm = false
m.draftNameInput = newInput()
m.templateConfirm = false
return m, nil
}
if m.submitID != "" {
return m, nil
}
if m.pendingDraft == nil {
m.confirmOpen = false
m.keys.confirm = false
return m, nil
}
if m.templateConfirm {
if msg.Type != tea.KeyEnter && msg.String() != "ctrl+s" {
var cmd tea.Cmd
m.draftNameInput, cmd = m.draftNameInput.Update(msg)
return m, cmd
}
name := strings.TrimSpace(m.draftNameInput.Value())
if name == "" || len(name) > 200 || strings.ContainsAny(name, "\r\n\x00") {
m.submitNotice = "Name must be 1–200 bytes without line breaks or NUL."
return m, nil
}
if m.pendingDraft.path == "" {
d, err := draft.Create(name, m.pendingDraft.source, m.url, true, 0)
if err != nil {
m.submitNotice = "Could not retain draft: " + err.Error()
return m, nil
}
m.pendingDraft.path = d.Path
} else if err := draft.SetName(m.pendingDraft.path, name); err != nil {
m.submitNotice = "Could not save draft name: " + err.Error()
return m, nil
}
m.pendingDraft.name = name
msg = tea.KeyMsg{Type: tea.KeyEnter}
}
if strings.TrimSpace(m.pendingDraft.name) == "" {
if m.draftNameInput.Prompt != "Name: " {
m.draftNameInput = newInput()
m.draftNameInput.Prompt = "Name: "
m.draftNameInput.Placeholder = "Pipeline name"
m.draftNameInput.Focus()
}
if msg.Type == tea.KeyEnter {
name := strings.TrimSpace(m.draftNameInput.Value())
if name == "" || len(name) > 200 || strings.ContainsAny(name, "\r\n\x00") {
m.submitNotice = "Name must be 1–200 bytes without line breaks or NUL."
return m, nil
}
if m.pendingDraft.path == "" {
d, err := draft.Create(name, m.pendingDraft.source, m.url, m.pendingDraft.createOnly, m.pendingDraft.baseVersion)
if err != nil {
m.submitNotice = "Could not retain draft: " + err.Error()
return m, nil
}
m.pendingDraft.path = d.Path
} else {
if err := draft.SetName(m.pendingDraft.path, name); err != nil {
m.submitNotice = "Could not save draft name: " + err.Error()
return m, nil
}
}
m.pendingDraft.name = name
for i := range m.draftItems {
if m.draftItems[i].Path == m.pendingDraft.path {
m.draftItems[i].Name = name
}
}
m.draftNameInput = newInput()
m.submitNotice = ""
return m, nil
}
var cmd tea.Cmd
m.draftNameInput, cmd = m.draftNameInput.Update(msg)
return m, cmd
}
switch msg.String() {
case "e":
m.confirmOpen = false
m.keys.confirm = false
return m.confirmMutation("Edit retained draft?", func() tea.Cmd {
return editDraftMode(m.pendingDraft.name, m.pendingDraft.source, true, m.pendingDraft.createOnly, draft.Draft{Path: m.pendingDraft.path, Name: m.pendingDraft.name, Server: m.url, CreateOnly: m.pendingDraft.createOnly, BaseVersion: m.pendingDraft.baseVersion})
})
case "s", "enter":
if m.conn == nil {
m.submitNotice = "Not connected; draft retained"
return m, nil
}
m.nextID++
m.submitID = strconv.FormatUint(m.nextID, 10)
m.submitNotice = "Updating pipeline…"
if m.pendingDraft.createOnly || m.pendingDraft.baseVersion == 0 {
m.submitNotice = "Creating pipeline…"
}
m.appendLine(m.submitNotice + " " + m.pendingDraft.name)
return m, m.send(protocol.Message{Type: protocol.Submit, ID: m.submitID, Name: m.pendingDraft.name, Source: m.pendingDraft.source, CreateOnly: m.pendingDraft.createOnly, ExpectedVersion: m.pendingDraft.baseVersion})
}
return m, nil
}
func (m Model) confirmView(height int) string {
if m.templateConfirm {
field := m.draftNameInput
field.Width = max(1, min(60, m.width-12))
cancelHint := "Esc back"
if m.pendingDraft.path != "" {
cancelHint = "Esc keep draft"
}
lines := []string{"Create pipeline", field.View(), "Uses this template without opening an editor.", "Creates a new pipeline; existing pipelines are never replaced.", "Destination: " + m.url, m.submitNotice, "Enter create pipeline · " + cancelHint}
if strings.Contains(m.pendingDraft.source, "```schedule") {
lines = append(lines, "WARNING: creation activates the included schedule.")
}
return popup(m.width, height, 90, lines)
}
if strings.TrimSpace(m.pendingDraft.name) == "" {
field := m.draftNameInput
if field.Prompt != "Name: " {
field = newInput()
field.Prompt = "Name: "
field.Placeholder = "Pipeline name"
field.Focus()
}
field.Width = max(1, min(60, m.width-12))
hint := "Enter save name and review · Esc keep draft"
if m.pendingDraft.path == "" {
hint = "Enter save draft and review · Esc back"
}
return popup(m.width, height, 76, []string{"Name this pipeline", "", field.View(), m.submitNotice, hint})
}
lines := []string{"Update pipeline", m.pendingDraft.name, "", "Creates a new immutable version; previous versions are retained.", "s update pipeline · e edit again · Esc keep draft"}
if m.pendingDraft.createOnly || m.pendingDraft.baseVersion == 0 {
lines[0] = "Create pipeline"
lines[3] = "Creates a new pipeline after confirmation."
lines[4] = "s create pipeline · e edit again · Esc keep draft"
}
lines = append(lines, "Destination: "+m.url)
if strings.Contains(m.pendingDraft.source, "```schedule") {
lines = append(lines, "WARNING: submitting activates the included schedule.")
}
if m.submitNotice != "" {
lines = append(lines, "", m.submitNotice)
}
return popup(m.width, height, 76, lines)
}