package tui import ( "context" "fmt" "strings" tea "charm.land/bubbletea/v2" "skill-manager/internal/skills" ) // entry is one row of the picker: a skill or an agent definition. type entry struct { skills.Skill agent bool } // picker is a filterable multi-select over a source's skills and agents. Typing // filters; already-installed entries are shown but can't be picked. type picker struct { title string // skillItems and agentItems are what the source offers; items is both, skills first. skillItems, agentItems []skills.Skill items []entry installed map[string]bool // by path filter string cursor int // index into visible() picked map[int]bool width int height int done bool canceled bool } func newPicker(title string, skillItems, agentItems []skills.Skill, installed map[string]bool) *picker { p := &picker{title: title, skillItems: skillItems, agentItems: agentItems, installed: installed, picked: map[int]bool{}} for _, s := range skillItems { p.items = append(p.items, entry{Skill: s}) } for _, a := range agentItems { p.items = append(p.items, entry{Skill: a, agent: true}) } return p } // Pick lets the user choose skills and agents. It returns ErrCanceled on escape. func Pick(ctx context.Context, title string, skillItems, agentItems []skills.Skill, installed map[string]bool) (chosenSkills, chosenAgents []skills.Skill, err error) { p := newPicker(title, skillItems, agentItems, installed) if err := run(ctx, p); err != nil { return nil, nil, err } if p.canceled { return nil, nil, ErrCanceled } chosenSkills, chosenAgents = p.selection() return chosenSkills, chosenAgents, nil } func (p *picker) visible() []int { var out []int f := strings.ToLower(p.filter) for i, s := range p.items { if strings.Contains(strings.ToLower(s.Name+" "+s.Path), f) { out = append(out, i) } } return out } // selection splits the picked entries into skills and agents. func (p *picker) selection() (chosenSkills, chosenAgents []skills.Skill) { for i, e := range p.items { switch { case !p.picked[i]: case e.agent: chosenAgents = append(chosenAgents, e.Skill) default: chosenSkills = append(chosenSkills, e.Skill) } } return chosenSkills, chosenAgents } func (p *picker) count() int { n := 0 for _, on := range p.picked { if on { n++ } } return n } func (p *picker) toggle(i int) { if !p.installed[p.items[i].Path] { p.picked[i] = !p.picked[i] } } func (p *picker) update(msg tea.Msg) tea.Cmd { key, ok := msg.(tea.KeyPressMsg) if !ok { return nil } vis := p.visible() switch key.String() { case "ctrl+c", "esc": p.canceled, p.done = true, true case "enter": // Enter with nothing picked takes the highlighted entry. if p.count() == 0 && p.cursor < len(vis) { p.toggle(vis[p.cursor]) } p.done = p.count() > 0 case "up", "ctrl+p": p.cursor = max(0, p.cursor-1) case "down", "ctrl+n": p.cursor = min(len(vis)-1, p.cursor+1) case "space": if p.cursor < len(vis) { p.toggle(vis[p.cursor]) } case "ctrl+a": for _, i := range vis { p.toggle(i) } case "backspace": if p.filter != "" { p.filter = p.filter[:len(p.filter)-1] p.cursor = 0 } default: if key.Text != "" { p.filter += key.Text p.cursor = 0 } } return nil } func (p *picker) resize(width, height int) { p.width, p.height = width, height } func (p *picker) finished() bool { return p.done } func (p *picker) view() string { var b strings.Builder fmt.Fprintf(&b, "%s %s\n", bold.Render(p.title), dim.Render(fmt.Sprintf("%d selected", p.count()))) fmt.Fprintf(&b, "filter: %s%s\n\n", p.filter, dim.Render("▏")) vis := p.visible() nameWidth := 0 for _, i := range vis { nameWidth = max(nameWidth, len(p.items[i].Name)) } start, end := window(len(vis), p.cursor, p.height-5) for row := start; row < end; row++ { s := p.items[vis[row]] box := "[ ]" switch { case p.installed[s.Path]: box = dim.Render("[•]") case p.picked[vis[row]]: box = green.Render("[x]") } kind := "" if s.agent { kind = cyan.Render("agent") + " " } line := fmt.Sprintf("%s %s %s%s", box, pad(s.Name, nameWidth), kind, dim.Render(s.Path)) if row == p.cursor { line = selected.Render("›") + " " + line } else { line = " " + line } b.WriteString(line + "\n") } if len(vis) == 0 { b.WriteString(dim.Render(" nothing matches") + "\n") } body := fit(b.String(), p.width, p.height-1) return body + "\n" + dim.Render("type to filter · ↑/↓ move · space toggle · ctrl+a toggle all · enter add · esc cancel") }