package tui import ( "strings" "git.a-wan.net/alan/pipelines/internal/draft" "git.a-wan.net/alan/pipelines/internal/harness" "git.a-wan.net/alan/pipelines/internal/pipeline" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "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 configured harnesses or nvim…" m.editorDraft = &edit m.editorDebugRunID = 0 return m, m.editorInput.Focus() } func (m Model) newPipeline(source string) (tea.Model, tea.Cmd) { return m.openAuthorPicker(editedMsg{source: pipeline.WithDefaults(source, m.defaultModel, m.defaultHarness), createOnly: true}) } func (m Model) editorChoices() []string { configured, _, _ := harness.Choices() choices := []string{} for _, name := range configured { if name == "opencode" { choices = append(choices, "OpenCode") } else { choices = append(choices, "Pi") } } choices = append(choices, "nvim") 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.editorDebugRunID = 0 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 && m.editorDebugRunID == 0) { return m, nil } choice := strings.ToLower(choices[min(m.editorIndex, len(choices)-1)]) if m.editorDebugRunID > 0 { id := m.editorDebugRunID m.editorDebugRunID = 0 m.editorOpen = false return m.debugUsing(id, choice) } d := m.editorDraft m.editorOpen = false m.editorDraft = nil return m.confirmMutation("Open "+choice+" to create/edit a draft?", func() tea.Cmd { return 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)) muted := lipgloss.NewStyle().Foreground(themeMuted) lines := []string{"Edit pipeline with…", "Filter editors: " + input.View(), ""} if m.editorDraft != nil && m.editorDraft.createOnly { lines[0] = "New pipeline with…" } if m.editorDebugRunID > 0 { lines[0] = "Investigate run with…" } choices := m.editorChoices() for i, choice := range choices { prefix := " " if i == m.editorIndex { prefix = "▸ " } lines = append(lines, menuOption(prefix+choice, i == m.editorIndex)) if choice == "nvim" { lines = append(lines, muted.Render(" Write the pipeline Markdown in a retained draft file.")) } else { lines = append(lines, muted.Render(" Open the configured harness to write it together.")) } } if len(choices) == 0 { lines = append(lines, "No matching editor.") } return popup(m.width, height, 80, lines) }