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>
409 lines
9.9 KiB
Go
409 lines
9.9 KiB
Go
package tui
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"maps"
|
||
"slices"
|
||
"strings"
|
||
|
||
"charm.land/bubbles/v2/spinner"
|
||
"charm.land/bubbles/v2/textinput"
|
||
tea "charm.land/bubbletea/v2"
|
||
|
||
"skill-manager/internal/core"
|
||
"skill-manager/internal/skills"
|
||
)
|
||
|
||
type mode int
|
||
|
||
const (
|
||
modeList mode = iota
|
||
modeConfirm
|
||
modeURL
|
||
modeBusy
|
||
modeSub
|
||
)
|
||
|
||
// row is a source header (skill == "") or one of its installed skills.
|
||
type row struct{ source, skill, path string }
|
||
|
||
type statusMsg struct {
|
||
source string
|
||
st core.SourceStatus
|
||
}
|
||
|
||
type availableMsg struct {
|
||
url string
|
||
found []skills.Skill
|
||
installed map[string]bool
|
||
err error
|
||
}
|
||
|
||
// doneMsg reports an add, remove or update that changed the manifest or lock.
|
||
type doneMsg struct {
|
||
summary string
|
||
recheck []string
|
||
err error
|
||
}
|
||
|
||
type dashboard struct {
|
||
ctx context.Context
|
||
store core.Store
|
||
rows []row
|
||
revs map[string]string // source -> locked rev
|
||
status map[string]core.SourceStatus
|
||
checking map[string]bool
|
||
cursor int
|
||
spin spinner.Model
|
||
input textinput.Model
|
||
|
||
mode mode
|
||
sub component
|
||
onSubDone func(component) tea.Cmd
|
||
question string
|
||
onYes func() tea.Cmd
|
||
|
||
note string
|
||
summary []string
|
||
width int
|
||
height int
|
||
}
|
||
|
||
// Dashboard runs the full-screen dashboard and returns one line per change made.
|
||
func Dashboard(ctx context.Context, store core.Store) ([]string, error) {
|
||
d := newDashboard(ctx, store)
|
||
if err := d.reload(); err != nil {
|
||
return nil, err
|
||
}
|
||
_, err := tea.NewProgram(d, tea.WithContext(ctx)).Run()
|
||
return d.summary, err
|
||
}
|
||
|
||
func newDashboard(ctx context.Context, store core.Store) *dashboard {
|
||
in := textinput.New()
|
||
in.Placeholder = "github:owner/repo"
|
||
return &dashboard{
|
||
ctx: ctx,
|
||
store: store,
|
||
status: map[string]core.SourceStatus{},
|
||
checking: map[string]bool{},
|
||
spin: spinner.New(spinner.WithSpinner(spinner.MiniDot)),
|
||
input: in,
|
||
width: 80,
|
||
height: 24,
|
||
}
|
||
}
|
||
|
||
func (d *dashboard) reload() error {
|
||
installed, err := d.store.List()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
bySource := map[string][]core.Installed{}
|
||
d.revs = map[string]string{}
|
||
for _, i := range installed {
|
||
bySource[i.Source] = append(bySource[i.Source], i)
|
||
d.revs[i.Source] = i.Rev
|
||
}
|
||
d.rows = nil
|
||
for _, src := range slices.Sorted(maps.Keys(bySource)) {
|
||
d.rows = append(d.rows, row{source: src})
|
||
for _, i := range bySource[src] {
|
||
d.rows = append(d.rows, row{source: src, skill: i.Name, path: i.Path})
|
||
}
|
||
}
|
||
for src := range d.status {
|
||
if _, ok := d.revs[src]; !ok {
|
||
delete(d.status, src)
|
||
}
|
||
}
|
||
d.cursor = max(0, min(d.cursor, len(d.rows)-1))
|
||
return nil
|
||
}
|
||
|
||
func (d *dashboard) sources() []string { return slices.Sorted(maps.Keys(d.revs)) }
|
||
|
||
func (d *dashboard) Init() tea.Cmd {
|
||
return tea.Batch(d.spin.Tick, d.recheck(d.sources()...))
|
||
}
|
||
|
||
// recheck checks each source against upstream, one command per source so a slow
|
||
// one doesn't hold up the rest.
|
||
func (d *dashboard) recheck(sources ...string) tea.Cmd {
|
||
var cmds []tea.Cmd
|
||
for _, src := range sources {
|
||
if _, ok := d.revs[src]; !ok {
|
||
continue
|
||
}
|
||
d.checking[src] = true
|
||
ctx, store := d.ctx, d.store
|
||
cmds = append(cmds, func() tea.Msg {
|
||
sts, err := store.Check(ctx, src)
|
||
if err != nil {
|
||
return statusMsg{src, core.SourceStatus{Source: src, Err: err}}
|
||
}
|
||
return statusMsg{src, sts[0]}
|
||
})
|
||
}
|
||
return tea.Batch(cmds...)
|
||
}
|
||
|
||
func (d *dashboard) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||
switch msg := msg.(type) {
|
||
case tea.WindowSizeMsg:
|
||
d.width, d.height = msg.Width, msg.Height
|
||
if d.sub != nil {
|
||
d.sub.resize(d.width, d.height)
|
||
}
|
||
case spinner.TickMsg:
|
||
var cmd tea.Cmd
|
||
d.spin, cmd = d.spin.Update(msg)
|
||
return d, cmd
|
||
case statusMsg:
|
||
delete(d.checking, msg.source)
|
||
d.status[msg.source] = msg.st
|
||
case availableMsg:
|
||
d.mode = modeList
|
||
if msg.err != nil {
|
||
d.note = red.Render(msg.err.Error())
|
||
return d, nil
|
||
}
|
||
d.note = ""
|
||
d.open(newPicker("add from "+msg.url, msg.found, msg.installed), func(c component) tea.Cmd {
|
||
p := c.(*picker)
|
||
if p.canceled {
|
||
return nil
|
||
}
|
||
return d.add(msg.url, p.items, p.selection())
|
||
})
|
||
case doneMsg:
|
||
d.mode = modeList
|
||
if msg.err != nil {
|
||
d.note = red.Render(msg.err.Error())
|
||
return d, nil
|
||
}
|
||
d.summary = append(d.summary, msg.summary)
|
||
d.note = green.Render(msg.summary) + dim.Render(" · rebuild to install")
|
||
if err := d.reload(); err != nil {
|
||
d.note = red.Render(err.Error())
|
||
}
|
||
return d, d.recheck(msg.recheck...)
|
||
case tea.KeyPressMsg:
|
||
return d, d.key(msg)
|
||
}
|
||
return d, nil
|
||
}
|
||
|
||
func (d *dashboard) open(c component, onDone func(component) tea.Cmd) {
|
||
c.resize(d.width, d.height)
|
||
d.sub, d.onSubDone, d.mode = c, onDone, modeSub
|
||
}
|
||
|
||
func (d *dashboard) ask(question string, onYes func() tea.Cmd) {
|
||
d.question, d.onYes, d.mode = question, onYes, modeConfirm
|
||
}
|
||
|
||
func (d *dashboard) key(msg tea.KeyPressMsg) tea.Cmd {
|
||
switch d.mode {
|
||
case modeSub:
|
||
cmd := d.sub.update(msg)
|
||
if !d.sub.finished() {
|
||
return cmd
|
||
}
|
||
c, onDone := d.sub, d.onSubDone
|
||
d.sub, d.onSubDone, d.mode = nil, nil, modeList
|
||
return tea.Batch(cmd, onDone(c))
|
||
case modeConfirm:
|
||
d.mode = modeList
|
||
if msg.String() == "y" {
|
||
d.note = ""
|
||
return d.onYes()
|
||
}
|
||
d.note = "canceled"
|
||
return nil
|
||
case modeURL:
|
||
switch msg.String() {
|
||
case "esc":
|
||
d.mode = modeList
|
||
return nil
|
||
case "enter":
|
||
url := strings.TrimSpace(d.input.Value())
|
||
if url == "" {
|
||
return nil
|
||
}
|
||
d.mode, d.note = modeBusy, "fetching "+url
|
||
ctx, store := d.ctx, d.store
|
||
return func() tea.Msg {
|
||
found, installed, err := store.Available(ctx, core.AddRequest{URL: url})
|
||
return availableMsg{url, found, installed, err}
|
||
}
|
||
}
|
||
var cmd tea.Cmd
|
||
d.input, cmd = d.input.Update(msg)
|
||
return cmd
|
||
case modeBusy:
|
||
if msg.String() == "ctrl+c" {
|
||
return tea.Quit
|
||
}
|
||
return nil
|
||
}
|
||
|
||
switch msg.String() {
|
||
case "q", "esc", "ctrl+c":
|
||
return tea.Quit
|
||
case "up", "k":
|
||
d.cursor = max(0, d.cursor-1)
|
||
case "down", "j":
|
||
d.cursor = min(len(d.rows)-1, d.cursor+1)
|
||
case "r":
|
||
d.note = ""
|
||
return d.recheck(d.sources()...)
|
||
case "a":
|
||
d.mode = modeURL
|
||
d.input.SetValue("")
|
||
return d.input.Focus()
|
||
case "U":
|
||
var outdated []core.SourceStatus
|
||
for _, src := range d.sources() {
|
||
if st := d.status[src]; st.Outdated() {
|
||
outdated = append(outdated, st)
|
||
}
|
||
}
|
||
d.review(outdated)
|
||
}
|
||
if d.cursor >= len(d.rows) {
|
||
return nil
|
||
}
|
||
r := d.rows[d.cursor]
|
||
switch msg.String() {
|
||
case "u", "enter":
|
||
d.review([]core.SourceStatus{d.status[r.source]})
|
||
case "d":
|
||
if r.skill != "" {
|
||
d.ask(fmt.Sprintf("remove %s from %s?", r.skill, r.source), func() tea.Cmd { return d.remove(r.skill) })
|
||
} else {
|
||
d.ask(fmt.Sprintf("remove source %s and all its skills?", r.source), func() tea.Cmd { return d.removeSource(r.source) })
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (d *dashboard) review(statuses []core.SourceStatus) {
|
||
r := newReview(d.ctx, statuses)
|
||
if r.done {
|
||
d.note = "nothing to update"
|
||
return
|
||
}
|
||
d.open(r, func(c component) tea.Cmd {
|
||
r := c.(*review)
|
||
if r.canceled || len(r.accepted) == 0 {
|
||
return nil
|
||
}
|
||
store, accepted := d.store, r.accepted
|
||
return func() tea.Msg {
|
||
var sources []string
|
||
for _, st := range accepted {
|
||
sources = append(sources, st.Source)
|
||
}
|
||
head, _, _ := strings.Cut(core.CommitMessage(accepted), "\n")
|
||
return doneMsg{strings.TrimPrefix(head, "skills: "), sources, store.Apply(accepted)}
|
||
}
|
||
})
|
||
}
|
||
|
||
func (d *dashboard) add(url string, found, chosen []skills.Skill) tea.Cmd {
|
||
req := core.AddRequest{URL: url, Skills: core.Select(found, chosen)}
|
||
var names []string
|
||
for _, c := range chosen {
|
||
names = append(names, c.Name)
|
||
}
|
||
ctx, store := d.ctx, d.store
|
||
return func() tea.Msg {
|
||
key, err := store.Add(ctx, req)
|
||
return doneMsg{fmt.Sprintf("added %s from %s", strings.Join(names, ", "), key), []string{key}, err}
|
||
}
|
||
}
|
||
|
||
func (d *dashboard) remove(skill string) tea.Cmd {
|
||
store := d.store
|
||
return func() tea.Msg {
|
||
key, err := store.Remove(skill)
|
||
return doneMsg{fmt.Sprintf("removed %s from %s", skill, key), []string{key}, err}
|
||
}
|
||
}
|
||
|
||
func (d *dashboard) removeSource(source string) tea.Cmd {
|
||
store := d.store
|
||
return func() tea.Msg {
|
||
return doneMsg{"removed source " + source, nil, store.RemoveSource(source)}
|
||
}
|
||
}
|
||
|
||
func (d *dashboard) View() tea.View {
|
||
v := tea.NewView(d.render())
|
||
v.AltScreen = true
|
||
return v
|
||
}
|
||
|
||
func (d *dashboard) render() string {
|
||
if d.mode == modeSub {
|
||
return d.sub.view()
|
||
}
|
||
var b strings.Builder
|
||
b.WriteString(bold.Render("skm") + " " + dim.Render(d.store.Dir) + "\n\n")
|
||
|
||
start, end := window(len(d.rows), d.cursor, d.height-5)
|
||
for i := start; i < end; i++ {
|
||
r := d.rows[i]
|
||
cursor := " "
|
||
if i == d.cursor {
|
||
cursor = selected.Render("› ")
|
||
}
|
||
st, checked := d.status[r.source]
|
||
if r.skill == "" {
|
||
fmt.Fprintf(&b, "%s%s %s %s\n", cursor, bold.Render(r.source), dim.Render(fmt.Sprintf("%.7s", d.revs[r.source])), d.sourceState(r.source, st, checked))
|
||
continue
|
||
}
|
||
mark := " "
|
||
for _, ch := range st.Changes {
|
||
if ch.Name == r.skill {
|
||
mark = symbol(ch.Kind)
|
||
}
|
||
}
|
||
fmt.Fprintf(&b, "%s %s %s %s\n", cursor, mark, r.skill, dim.Render(r.path))
|
||
}
|
||
if len(d.rows) == 0 {
|
||
b.WriteString(dim.Render(" no skills yet; press a to add a source") + "\n")
|
||
}
|
||
|
||
var footer string
|
||
switch d.mode {
|
||
case modeURL:
|
||
footer = "add source: " + d.input.View()
|
||
case modeConfirm:
|
||
footer = yellow.Render(d.question + " [y/N]")
|
||
case modeBusy:
|
||
footer = d.spin.View() + " " + d.note
|
||
default:
|
||
footer = d.note + "\n" + dim.Render("↑/↓ move · u/enter review update · U review all · a add · d remove · r refresh · q quit")
|
||
}
|
||
return fit(b.String(), d.width, d.height-2) + "\n" + fit(footer, d.width, 2)
|
||
}
|
||
|
||
func (d *dashboard) sourceState(src string, st core.SourceStatus, checked bool) string {
|
||
switch {
|
||
case d.checking[src]:
|
||
return d.spin.View() + dim.Render(" checking")
|
||
case !checked:
|
||
return ""
|
||
case st.Err != nil:
|
||
msg, _, _ := strings.Cut(st.Err.Error(), "\n")
|
||
return red.Render("✗ " + msg)
|
||
case st.Outdated():
|
||
return yellow.Render(fmt.Sprintf("↑ %d changed upstream", len(st.Changes)))
|
||
default:
|
||
return green.Render("✓ up to date")
|
||
}
|
||
}
|