Files
pipelines/internal/tui/editor_picker.go
T

90 lines
2.6 KiB
Go

package tui
import (
"strings"
"git.a-wan.net/alan/pipelines/internal/draft"
tea "github.com/charmbracelet/bubbletea"
"github.com/sahilm/fuzzy"
)
func (m Model) openAuthorPicker(edit editedMsg) (tea.Model, tea.Cmd) {
m.editorOpen = true
m.editorIndex = 0
m.editorInput = newInput()
m.editorInput.Placeholder = "Search nvim or OpenCode…"
m.editorDraft = &edit
return m, m.editorInput.Focus()
}
func (m Model) newPipeline(source string) (tea.Model, tea.Cmd) {
return m.openAuthorPicker(editedMsg{source: source, createOnly: true})
}
func (m Model) editorChoices() []string {
choices := []string{"nvim", "OpenCode"}
if query := strings.TrimSpace(m.editorInput.Value()); query != "" {
var result []string
for _, match := range fuzzy.Find(query, choices) {
result = append(result, match.Str)
}
return result
}
return choices
}
func (m Model) updateEditorPicker(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
choices := m.editorChoices()
switch msg.String() {
case "esc":
m.editorOpen = false
m.editorDraft = nil
return m, nil
case "tab", "down", "ctrl+n":
if len(choices) > 0 {
m.editorIndex = (m.editorIndex + 1) % len(choices)
}
return m, nil
case "shift+tab", "up", "ctrl+p":
if len(choices) > 0 {
m.editorIndex = (m.editorIndex + len(choices) - 1) % len(choices)
}
return m, nil
case "enter":
if len(choices) == 0 || m.editorDraft == nil {
return m, nil
}
choice := strings.ToLower(choices[min(m.editorIndex, len(choices)-1)])
d := m.editorDraft
m.editorOpen = false
m.editorDraft = nil
return m, editDraftUsing(choice, d.name, d.source, true, d.createOnly, draft.Draft{Path: d.path, Name: d.name, Server: m.url, CreateOnly: d.createOnly, BaseVersion: d.baseVersion})
}
var cmd tea.Cmd
m.editorInput, cmd = m.editorInput.Update(msg)
m.editorIndex = 0
return m, cmd
}
func (m Model) editorPickerView(height int) string {
input := m.editorInput
input.Width = max(1, min(60, m.width-12))
lines := []string{"Edit pipeline with…", input.View(), "Tab/Shift+Tab choose · type to search · Enter open · Esc cancel", ""}
if m.editorDraft != nil && m.editorDraft.createOnly {
lines[0] = "New pipeline with…"
}
choices := m.editorChoices()
for i, choice := range choices {
lines = append(lines, menuOption(choice, i == m.editorIndex))
if choice == "nvim" {
lines = append(lines, " Write the pipeline Markdown in a retained draft file.")
} else {
lines = append(lines, " Open the OpenCode harness to write it together.")
}
}
if len(choices) == 0 {
lines = append(lines, "No matching editor.")
}
return popup(m.width, height, 80, lines)
}