c42a19f7d3
Make OpenCode authoring responsive and expand the managed skill references. Add conflict-aware import/export, real-provider reference workflows, and tracked V2 generation for reliable budget accounting.
112 lines
2.8 KiB
Go
112 lines
2.8 KiB
Go
package tui
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/x/ansi"
|
|
"strings"
|
|
)
|
|
|
|
// PickPipelines retains selections while filtering, keyed by server-local ID.
|
|
func PickPipelines(ctx context.Context, items []protocol.PipelineSummary, title string) ([]protocol.PipelineSummary, error) {
|
|
if len(items) == 0 {
|
|
return nil, fmt.Errorf("no pipelines available")
|
|
}
|
|
p := multiPicker{picker: picker{items: items, input: newInput()}, title: title, checked: map[int64]bool{}}
|
|
p.input.Placeholder = "Search pipelines…"
|
|
p.input.Focus()
|
|
result, err := tea.NewProgram(p, tea.WithContext(ctx), tea.WithAltScreen()).Run()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
done := result.(multiPicker)
|
|
if !done.accepted {
|
|
return nil, fmt.Errorf("selection canceled")
|
|
}
|
|
var selected []protocol.PipelineSummary
|
|
for _, item := range items {
|
|
if done.checked[item.ID] {
|
|
selected = append(selected, item)
|
|
}
|
|
}
|
|
return selected, nil
|
|
}
|
|
|
|
type multiPicker struct {
|
|
picker
|
|
title string
|
|
checked map[int64]bool
|
|
accepted bool
|
|
}
|
|
|
|
func (p multiPicker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
if key, ok := msg.(tea.KeyMsg); ok {
|
|
switch key.String() {
|
|
case "esc", "ctrl+c":
|
|
return p, tea.Quit
|
|
case " ":
|
|
items := p.matches()
|
|
if len(items) > 0 {
|
|
id := items[min(p.index, len(items)-1)].ID
|
|
if p.checked[id] {
|
|
delete(p.checked, id)
|
|
} else {
|
|
p.checked[id] = true
|
|
}
|
|
}
|
|
return p, nil
|
|
case "ctrl+a":
|
|
items := p.matches()
|
|
all := len(items) > 0
|
|
for _, item := range items {
|
|
all = all && p.checked[item.ID]
|
|
}
|
|
for _, item := range items {
|
|
if all {
|
|
delete(p.checked, item.ID)
|
|
} else {
|
|
p.checked[item.ID] = true
|
|
}
|
|
}
|
|
return p, nil
|
|
case "enter":
|
|
if len(p.checked) > 0 {
|
|
p.accepted = true
|
|
return p, tea.Quit
|
|
}
|
|
return p, nil
|
|
}
|
|
}
|
|
next, cmd := p.picker.Update(msg)
|
|
p.picker = next.(picker)
|
|
return p, cmd
|
|
}
|
|
func (p multiPicker) View() string {
|
|
width := max(1, p.width-2)
|
|
input := p.input
|
|
input.Width = max(1, min(72, width-4))
|
|
lines := []string{p.title, input.View(), fmt.Sprintf("%d selected · Space toggle · Ctrl+A toggle filtered · Enter continue · Esc cancel", len(p.checked)), ""}
|
|
items := p.matches()
|
|
start, end := selectionWindow(len(items), p.index, max(1, p.height-7))
|
|
for i := start; i < end; i++ {
|
|
prefix := " "
|
|
if i == p.index {
|
|
prefix = "▸ "
|
|
}
|
|
check := "[ ]"
|
|
if p.checked[items[i].ID] {
|
|
check = "[x]"
|
|
}
|
|
lines = append(lines, fmt.Sprintf("%s%s %s · v%d", prefix, check, ansi.Strip(items[i].Name), items[i].Version))
|
|
}
|
|
if len(items) == 0 {
|
|
lines = append(lines, "No matching pipelines")
|
|
}
|
|
for i := range lines {
|
|
lines[i] = ansi.Truncate(lines[i], width, "…")
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
}
|