Files
pipelines/internal/tui/templates.go
T

123 lines
3.8 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.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) {
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 {
return m.newPipeline(m.templates[m.templateIndex].Source)
}
}
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 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)
}