Bare skm opens a dashboard with live upstream status per source; add uses a filterable picker and update a side-by-side diff review when run in a terminal. Non-interactive paths are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
150 lines
3.5 KiB
Go
150 lines
3.5 KiB
Go
package tui
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"strings"
|
||
|
||
tea "charm.land/bubbletea/v2"
|
||
|
||
"skill-manager/internal/skills"
|
||
)
|
||
|
||
// picker is a filterable multi-select over a source's skills. Typing filters;
|
||
// already-installed skills are shown but can't be picked.
|
||
type picker struct {
|
||
title string
|
||
items []skills.Skill
|
||
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, items []skills.Skill, installed map[string]bool) *picker {
|
||
return &picker{title: title, items: items, installed: installed, picked: map[int]bool{}}
|
||
}
|
||
|
||
// Pick lets the user choose skills from items. It returns ErrCanceled on escape.
|
||
func Pick(ctx context.Context, title string, items []skills.Skill, installed map[string]bool) ([]skills.Skill, error) {
|
||
p := newPicker(title, items, installed)
|
||
if err := run(ctx, p); err != nil {
|
||
return nil, err
|
||
}
|
||
if p.canceled {
|
||
return nil, ErrCanceled
|
||
}
|
||
return p.selection(), 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
|
||
}
|
||
|
||
func (p *picker) selection() []skills.Skill {
|
||
var out []skills.Skill
|
||
for i, s := range p.items {
|
||
if p.picked[i] {
|
||
out = append(out, s)
|
||
}
|
||
}
|
||
return out
|
||
}
|
||
|
||
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 skill.
|
||
if len(p.selection()) == 0 && p.cursor < len(vis) {
|
||
p.toggle(vis[p.cursor])
|
||
}
|
||
p.done = len(p.selection()) > 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", len(p.selection()))))
|
||
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]")
|
||
}
|
||
line := fmt.Sprintf("%s %s %s", box, pad(s.Name, nameWidth), 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(" no skills match") + "\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")
|
||
}
|