114 lines
3.1 KiB
Go
114 lines
3.1 KiB
Go
package tui
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/sahilm/fuzzy"
|
|
"pipelines/internal/protocol"
|
|
"strings"
|
|
)
|
|
|
|
type RunOptions struct {
|
|
Pipeline, Prompt, Workspace string
|
|
Yes, AllowTools bool
|
|
WritePaths []string
|
|
}
|
|
|
|
func NewRun(ctx context.Context, url string, options RunOptions) Model {
|
|
m := New(ctx, url)
|
|
m.cliRun = true
|
|
m.cliOptions = options
|
|
m.runWorkspace = options.Workspace
|
|
m.runPicker = true
|
|
m.runPickerInput = newInput()
|
|
m.runPickerInput.Placeholder = "Search registered pipelines…"
|
|
m.runPickerInput.SetValue(options.Pipeline)
|
|
m.runPickerInput.Focus()
|
|
return m
|
|
}
|
|
|
|
func (m Model) runChoices() []protocol.PipelineSummary {
|
|
query := strings.TrimSpace(m.runPickerInput.Value())
|
|
if query == "" {
|
|
return m.pipelines
|
|
}
|
|
names := make([]string, len(m.pipelines))
|
|
for i, p := range m.pipelines {
|
|
names[i] = p.Name
|
|
}
|
|
var result []protocol.PipelineSummary
|
|
for _, match := range fuzzy.Find(query, names) {
|
|
result = append(result, m.pipelines[match.Index])
|
|
}
|
|
return result
|
|
}
|
|
|
|
func (m Model) updateRunPicker(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|
items := m.runChoices()
|
|
switch msg.String() {
|
|
case "esc":
|
|
return m, tea.Quit
|
|
case "down", "ctrl+n":
|
|
m.runPickerIndex = min(m.runPickerIndex+1, max(0, len(items)-1))
|
|
return m, nil
|
|
case "up", "ctrl+p":
|
|
m.runPickerIndex = max(0, m.runPickerIndex-1)
|
|
return m, nil
|
|
case "enter":
|
|
if len(items) > 0 {
|
|
m.runPicker = false
|
|
return m.requestPipeline(items[m.runPickerIndex].ID, items[m.runPickerIndex].Version)
|
|
}
|
|
return m, nil
|
|
}
|
|
var cmd tea.Cmd
|
|
m.runPickerInput, cmd = m.runPickerInput.Update(msg)
|
|
m.runPickerIndex = 0
|
|
return m, cmd
|
|
}
|
|
|
|
func (m Model) runPickerView(height int) string {
|
|
input := m.runPickerInput
|
|
input.Width = max(1, min(72, m.width-8))
|
|
lines := []string{"Run a pipeline in this repository", input.View(), "Repository: " + m.runWorkspace, "Enter select · ↑/↓ choose · Esc exit", ""}
|
|
items := m.runChoices()
|
|
start, end := selectionWindow(len(items), m.runPickerIndex, max(1, height-8))
|
|
if !m.loaded {
|
|
lines = append(lines, "Connecting to the pipeline service…")
|
|
} else if len(items) == 0 {
|
|
lines = append(lines, "No matching pipelines. Open pipelines and press b to create one from a template.")
|
|
}
|
|
for i := start; i < end; i++ {
|
|
prefix := " "
|
|
if i == m.runPickerIndex {
|
|
prefix = "▸ "
|
|
}
|
|
lines = append(lines, menuOption(fmt.Sprintf("%s%s · v%d · %d stages", prefix, items[i].Name, items[i].Version, items[i].Stages), i == m.runPickerIndex))
|
|
}
|
|
if m.detailError != "" {
|
|
lines = append(lines, m.detailError)
|
|
}
|
|
return popup(m.width, height, 100, lines)
|
|
}
|
|
|
|
func (m Model) configureCLIRun() (tea.Model, tea.Cmd) {
|
|
m.cliStarted = true
|
|
m.runPicker = false
|
|
updated, cmd := m.beginRun()
|
|
m = updated.(Model)
|
|
m.runPaths = append([]string(nil), m.cliOptions.WritePaths...)
|
|
if m.cliOptions.Prompt != "" {
|
|
m.runTask = m.cliOptions.Prompt
|
|
m.runForm = 3
|
|
m.runInput = newInput()
|
|
m.runInput.Placeholder = "Type RUN to confirm"
|
|
cmd = m.runInput.Focus()
|
|
if m.cliOptions.Yes {
|
|
cmd = m.startSelectedRun()
|
|
return m, cmd
|
|
}
|
|
}
|
|
return m, cmd
|
|
}
|