Files
pipelines/internal/tui/templates.go
T

161 lines
5.0 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"
"git.a-wan.net/alan/pipelines/internal/draft"
"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.templateError = ""
m.templateNaming = false
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.templateLines = nil
if len(m.templates) == 0 {
return
}
t := m.templates[m.templateIndex]
source := ansi.Strip(t.Source)
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 = t.Source
}
}
m.templateLines = strings.Split(source, "\n")
}
func (m Model) updateTemplates(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
if m.templateNaming {
switch msg.String() {
case "esc":
m.templateNaming = false
m.templateError = ""
return m, nil
case "enter":
name := strings.TrimSpace(m.field.Value())
if name == "" || len(name) > 200 || strings.ContainsAny(name, "\r\n\x00") {
m.templateError = "Choose a name (1–200 bytes)."
return m, nil
}
for _, p := range m.pipelines {
if p.Name == name {
m.templateError = "That pipeline already exists; choose a new name."
return m, nil
}
}
m.templateNaming = false
return m, editDraftMode(name, m.templates[m.templateIndex].Source, true, true, draft.Draft{Server: m.url})
}
var cmd tea.Cmd
m.field, cmd = m.field.Update(msg)
return m, cmd
}
switch msg.String() {
case "s":
if m.pendingDraft != nil {
m.confirmOpen = true
m.keys.confirm = true
}
case "e":
if m.pendingDraft != nil {
return m, editDraftMode(m.pendingDraft.name, m.pendingDraft.source, true, m.pendingDraft.createOnly, draft.Draft{Server: m.url, BaseVersion: m.pendingDraft.baseVersion})
}
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)
case "enter":
if len(m.templates) > 0 {
m.templateNaming = true
m.templateError = ""
m.field = newInput()
m.field.CharLimit = 200
m.field.Placeholder = "New pipeline name"
return m, m.field.Focus()
}
}
m.templateTop = max(0, min(m.templateTop, max(0, len(m.templateLines)-max(1, m.height-10))))
return m, nil
}
func (m Model) templatesView(height int) string {
if m.templateNaming {
lines := []string{"New pipeline from template", m.templates[m.templateIndex].Name, "Choose a unique name, then edit before submitting.", m.field.View(), m.templateError}
if m.templates[m.templateIndex].Scheduled {
lines = append(lines, "Scheduled template: submission activates its schedule.")
}
return popup(m.width, height, 84, lines)
}
if len(m.templates) == 0 {
return fitBlock([]string{"Templates", "Loading examples…", m.templateError, "Esc back"}, m.width, height)
}
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 · Enter creates an editable draft, not a submission."
if t.Scheduled {
note = "SCHEDULED: submission activates its schedule; edit/remove settings first."
}
rows := max(0, height-6)
top := max(0, min(m.templateTop, max(0, len(m.templateLines)-rows)))
end := min(len(m.templateLines), top+rows)
headers := []string{bar(title, mode, max(1, m.width-4), true), 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, "n/p template · v rendered/source · j/k scroll · Enter use · Esc back", true)
}