102 lines
2.8 KiB
Go
102 lines
2.8 KiB
Go
package tui
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/sahilm/fuzzy"
|
|
"strings"
|
|
)
|
|
|
|
// PickPipeline is the same fuzzy selection interaction used for CLI authoring.
|
|
func PickPipeline(ctx context.Context, items []protocol.PipelineSummary) (protocol.PipelineSummary, error) {
|
|
if len(items) == 0 {
|
|
return protocol.PipelineSummary{}, fmt.Errorf("no registered pipelines; use pipelines new")
|
|
}
|
|
m := picker{items: items, input: newInput()}
|
|
m.input.Placeholder = "Search pipelines…"
|
|
m.input.Focus()
|
|
result, err := tea.NewProgram(m, tea.WithContext(ctx), tea.WithAltScreen()).Run()
|
|
if err != nil {
|
|
return protocol.PipelineSummary{}, err
|
|
}
|
|
p := result.(picker)
|
|
if p.selected == nil {
|
|
return protocol.PipelineSummary{}, fmt.Errorf("selection canceled")
|
|
}
|
|
return *p.selected, nil
|
|
}
|
|
|
|
type picker struct {
|
|
items []protocol.PipelineSummary
|
|
input textinput.Model
|
|
index, width, height int
|
|
selected *protocol.PipelineSummary
|
|
}
|
|
|
|
func (p picker) Init() tea.Cmd { return textinput.Blink }
|
|
func (p picker) matches() []protocol.PipelineSummary {
|
|
if strings.TrimSpace(p.input.Value()) == "" {
|
|
return p.items
|
|
}
|
|
names := make([]string, len(p.items))
|
|
for i, item := range p.items {
|
|
names[i] = item.Name
|
|
}
|
|
var result []protocol.PipelineSummary
|
|
for _, m := range fuzzy.Find(p.input.Value(), names) {
|
|
result = append(result, p.items[m.Index])
|
|
}
|
|
return result
|
|
}
|
|
func (p picker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
items := p.matches()
|
|
switch msg := msg.(type) {
|
|
case tea.WindowSizeMsg:
|
|
p.width = msg.Width
|
|
p.height = msg.Height
|
|
case tea.KeyMsg:
|
|
switch msg.String() {
|
|
case "esc", "ctrl+c":
|
|
return p, tea.Quit
|
|
case "down", "ctrl+n":
|
|
p.index = min(p.index+1, max(0, len(items)-1))
|
|
return p, nil
|
|
case "up", "ctrl+p":
|
|
p.index = max(0, p.index-1)
|
|
return p, nil
|
|
case "enter":
|
|
if len(items) > 0 {
|
|
item := items[min(p.index, len(items)-1)]
|
|
p.selected = &item
|
|
return p, tea.Quit
|
|
}
|
|
return p, nil
|
|
}
|
|
p.index = 0
|
|
}
|
|
var cmd tea.Cmd
|
|
p.input, cmd = p.input.Update(msg)
|
|
return p, cmd
|
|
}
|
|
func (p picker) View() string {
|
|
input := p.input
|
|
input.Width = max(1, min(72, p.width-8))
|
|
lines := []string{"Select pipeline to edit", input.View(), "↑/↓ choose · Enter select · Esc cancel", ""}
|
|
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 = "▸ "
|
|
}
|
|
lines = append(lines, fmt.Sprintf("%s%s · v%d", prefix, items[i].Name, items[i].Version))
|
|
}
|
|
if len(items) == 0 {
|
|
lines = append(lines, "No matching pipelines")
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
}
|