Files
pipelines/internal/tui/pipeline.go
T

332 lines
8.9 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{"Omnisearch · 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, editDraftMode(m.detail.Name, pipeline.ExecutionSource(m.detail.Source), true, false, draft.Draft{Server: m.url, 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":
m.runOpen = true
m.keys.runs = true
m.runPipelineID = m.requestedPipelineID
m.runDetail = nil
m.requestedRunID = 0
m.runStartRequest = ""
m.runIndex = 0
m.runTop = 0
m.runError = ""
return m.runRequest(protocol.Message{Type: protocol.ListRuns})
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
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
return m, nil
}
if m.submitID != "" {
return m, nil
}
if m.pendingDraft == nil {
m.confirmOpen = false
m.keys.confirm = false
return m, nil
}
switch msg.String() {
case "e":
m.confirmOpen = false
m.keys.confirm = false
return m, editDraftMode(m.pendingDraft.name, m.pendingDraft.source, true, m.pendingDraft.createOnly, draft.Draft{Server: m.url, 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 = "Submitting…"
m.appendLine("Submitting " + 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 {
lines := []string{"Submit edited pipeline", m.pendingDraft.name, "", "Creates a new immutable version.", "s submit · e edit again · Esc keep draft"}
if m.pendingDraft.createOnly {
lines[0] = "Create pipeline from template"
lines[3] = "Creates a NEW pipeline only when you submit."
}
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)
}