c42a19f7d3
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.
215 lines
7.0 KiB
Go
215 lines
7.0 KiB
Go
package tui
|
||
|
||
import (
|
||
"fmt"
|
||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||
tea "github.com/charmbracelet/bubbletea"
|
||
"github.com/charmbracelet/glamour"
|
||
"github.com/charmbracelet/lipgloss"
|
||
"github.com/charmbracelet/x/ansi"
|
||
"strings"
|
||
)
|
||
|
||
func (m Model) openTemplates() (tea.Model, tea.Cmd) {
|
||
if m.conn == nil {
|
||
m.appendLine("Connect to the server to browse templates.")
|
||
return m, nil
|
||
}
|
||
m.templateOpen = true
|
||
m.keys.templates = true
|
||
m.templateIndex = 0
|
||
m.templateTop = 0
|
||
m.templateGraphFocus = false
|
||
m.templateGraphTop, m.templateGraphPan = 0, 0
|
||
m.templateError = ""
|
||
m.nextID++
|
||
m.templateRequest = fmt.Sprint(m.nextID)
|
||
return m, m.send(protocol.Message{Type: protocol.ListTemplates, ID: m.templateRequest})
|
||
}
|
||
|
||
func (m *Model) renderTemplate() {
|
||
m.templateTop = 0
|
||
m.templateGraphTop, m.templateGraphPan = 0, 0
|
||
m.templateLines = nil
|
||
if len(m.templates) == 0 {
|
||
return
|
||
}
|
||
t := m.templates[m.templateIndex]
|
||
display := ansi.Strip(pipeline.WithDefaultModel(t.Source, m.defaultModel))
|
||
source := display
|
||
if !m.templateRaw {
|
||
renderer, err := glamour.NewTermRenderer(glamour.WithStandardStyle("dark"), glamour.WithWordWrap(max(1, m.width-8)))
|
||
if err == nil {
|
||
source, err = renderer.Render("# " + t.Name + "\n\n" + t.Description + "\n\n" + source)
|
||
}
|
||
if err != nil {
|
||
m.templateError = "Markdown rendering failed; showing source."
|
||
source = display
|
||
}
|
||
}
|
||
m.templateLines = strings.Split(source, "\n")
|
||
}
|
||
|
||
func (m Model) updateTemplates(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||
if m.templateGraphFocus {
|
||
handled := true
|
||
switch msg.String() {
|
||
case "j", "down":
|
||
m.templateGraphTop++
|
||
case "k", "up":
|
||
m.templateGraphTop = max(0, m.templateGraphTop-1)
|
||
case "h", "left":
|
||
m.templateGraphPan = max(0, m.templateGraphPan-4)
|
||
case "l", "right":
|
||
m.templateGraphPan += 4
|
||
case "c", "home":
|
||
m.templateGraphTop, m.templateGraphPan = 0, 0
|
||
case "pgdown", "ctrl+d":
|
||
m.templateGraphTop += max(1, m.height/3)
|
||
case "pgup", "ctrl+u":
|
||
m.templateGraphTop = max(0, m.templateGraphTop-max(1, m.height/3))
|
||
default:
|
||
handled = false
|
||
}
|
||
if handled {
|
||
return m.clampTemplateGraph(), nil
|
||
}
|
||
}
|
||
switch msg.String() {
|
||
case "tab", "shift+tab":
|
||
m.templateGraphFocus = !m.templateGraphFocus
|
||
case "s", "enter":
|
||
if len(m.templates) == 0 || m.submitID != "" {
|
||
return m, nil
|
||
}
|
||
t := m.templates[m.templateIndex]
|
||
source := pipeline.WithDefaultModel(t.Source, m.defaultModel)
|
||
m.pendingDraft = &editedMsg{name: t.Name, source: source, createOnly: true}
|
||
m.templateConfirm = true
|
||
m.confirmOpen = true
|
||
m.keys.confirm = true
|
||
m.submitNotice = ""
|
||
m.draftNameInput = newInput()
|
||
m.draftNameInput.Prompt = "Name: "
|
||
m.draftNameInput.SetValue(t.Name)
|
||
return m, m.draftNameInput.Focus()
|
||
case "e":
|
||
if len(m.templates) > 0 {
|
||
return m.newPipeline(m.templates[m.templateIndex].Source)
|
||
}
|
||
case "esc":
|
||
m.templateOpen = false
|
||
m.keys.templates = false
|
||
return m, nil
|
||
case "n", "right", "]":
|
||
if len(m.templates) > 0 {
|
||
m.templateIndex = (m.templateIndex + 1) % len(m.templates)
|
||
m.renderTemplate()
|
||
}
|
||
case "p", "left", "[":
|
||
if len(m.templates) > 0 {
|
||
m.templateIndex = (m.templateIndex + len(m.templates) - 1) % len(m.templates)
|
||
m.renderTemplate()
|
||
}
|
||
case "v":
|
||
m.templateRaw = !m.templateRaw
|
||
m.renderTemplate()
|
||
case "j", "down":
|
||
m.templateTop++
|
||
case "k", "up":
|
||
m.templateTop--
|
||
case "pgdown", "ctrl+d":
|
||
m.templateTop += max(1, m.height-12)
|
||
case "pgup", "ctrl+u":
|
||
m.templateTop -= max(1, m.height-12)
|
||
case "home":
|
||
m.templateTop = 0
|
||
case "end":
|
||
m.templateTop = len(m.templateLines)
|
||
}
|
||
descriptionHeight, _ := m.templatePaneHeights(max(0, m.height-4))
|
||
m.templateTop = max(0, min(m.templateTop, max(0, len(m.templateLines)-max(1, descriptionHeight-7))))
|
||
return m.clampTemplateGraph(), nil
|
||
}
|
||
|
||
func (m Model) clampTemplateGraph() Model {
|
||
if len(m.templates) == 0 {
|
||
return m
|
||
}
|
||
graph, err := pipeline.Parse(m.templates[m.templateIndex].Source)
|
||
if err != nil {
|
||
return m
|
||
}
|
||
lines := renderGraph(graph)
|
||
widest := 0
|
||
for _, line := range lines {
|
||
widest = max(widest, ansi.StringWidth(line))
|
||
}
|
||
_, height := m.templatePaneHeights(max(0, m.height-4))
|
||
m.templateGraphTop = max(0, min(m.templateGraphTop, max(0, len(lines)-max(0, height-5))))
|
||
m.templateGraphPan = max(0, min(m.templateGraphPan, max(0, widest-max(1, m.width-4))))
|
||
return m
|
||
}
|
||
|
||
func (m Model) templatesView(height int) string {
|
||
if len(m.templates) == 0 {
|
||
if m.templateError != "" {
|
||
return fitBlock([]string{"Templates unavailable", m.templateError, "Use :refresh to try again. Esc back."}, m.width, height)
|
||
}
|
||
return fitBlock([]string{"Templates", m.loading("Loading examples…"), m.templateError, "Esc back"}, m.width, height)
|
||
}
|
||
descriptionHeight, graphHeight := m.templatePaneHeights(height)
|
||
description := m.templateDescriptionView(descriptionHeight)
|
||
graph := m.templateGraphView(graphHeight)
|
||
return fitBlock(strings.Split(description+"\n"+graph, "\n"), m.width, height)
|
||
}
|
||
|
||
func (m Model) templatePaneHeights(height int) (int, int) {
|
||
if height < 14 {
|
||
if m.templateGraphFocus {
|
||
return min(2, height), max(0, height-2)
|
||
}
|
||
return max(0, height-2), min(2, height)
|
||
}
|
||
graph := max(6, height/3)
|
||
if m.templateGraphFocus {
|
||
graph = height - max(6, height/4)
|
||
}
|
||
return height - graph, graph
|
||
}
|
||
|
||
func (m Model) templateGraphView(height int) string {
|
||
if height <= 2 {
|
||
return fitBlock([]string{bar("Template graph", "Tab to expand", m.width, false)}, m.width, height)
|
||
}
|
||
t := m.templates[m.templateIndex]
|
||
graph, err := pipeline.Parse(pipeline.WithDefaultModel(t.Source, m.defaultModel))
|
||
if err != nil {
|
||
return panel(m.width, height, []string{"Template graph"}, []string{"Graph unavailable: " + err.Error()}, "", m.templateGraphFocus)
|
||
}
|
||
return graphViewport("Template graph", renderGraph(graph), m.width, height, m.templateGraphTop, m.templateGraphPan, m.templateGraphFocus, "Tab description · n/p template · hjkl pan · c center")
|
||
}
|
||
|
||
func (m Model) templateDescriptionView(height int) string {
|
||
t := m.templates[m.templateIndex]
|
||
mode := "Rendered Markdown"
|
||
if m.templateRaw {
|
||
mode = "Markdown source"
|
||
}
|
||
title := fmt.Sprintf("Templates · %d/%d · %s", m.templateIndex+1, len(m.templates), t.Name)
|
||
note := "Preview only · create a pipeline with an editable, prefilled name."
|
||
if t.Scheduled {
|
||
note = "SCHEDULED: submission activates its schedule; edit/remove settings first."
|
||
}
|
||
rows := max(0, height-7)
|
||
top := max(0, min(m.templateTop, max(0, len(m.templateLines)-rows)))
|
||
end := min(len(m.templateLines), top+rows)
|
||
headers := []string{runPaneTitle(title, mode, max(1, m.width-4), !m.templateGraphFocus), action("s Create pipeline", true) + " " + action("e Customize", false), lipgloss.NewStyle().Foreground(themeMuted).Render(note), bar("", fmt.Sprintf("%d–%d / %d", min(top+1, len(m.templateLines)), end, len(m.templateLines)), max(1, m.width-4), false)}
|
||
body := m.templateLines[top:end]
|
||
if m.templateError != "" {
|
||
body = append([]string{m.templateError}, body...)
|
||
}
|
||
return panel(m.width, height, headers, body, "s/Enter create · Tab graph · n/p template · v source · j/k scroll", !m.templateGraphFocus)
|
||
}
|