Files
skm/internal/tui/review.go
T
johannesandClaude Opus 5.5 6f4d6f5609 feat: bubbletea dashboard, skill picker and update review
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>
2026-10-23 14:29:30 +02:00

175 lines
4.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package tui
import (
"context"
"fmt"
"strings"
"charm.land/bubbles/v2/viewport"
tea "charm.land/bubbletea/v2"
"skill-manager/internal/core"
)
// review walks outdated sources one at a time: changed skills on the left, the
// selected skill's diff on the right; y accepts the source, n skips it.
type review struct {
ctx context.Context
statuses []core.SourceStatus
i, sel int
vp viewport.Model
diffs map[[2]int]string
accepted []core.SourceStatus
width int
height int
done bool
canceled bool
}
func newReview(ctx context.Context, statuses []core.SourceStatus) *review {
r := &review{ctx: ctx, vp: viewport.New(), diffs: map[[2]int]string{}}
for _, st := range statuses {
if st.Outdated() {
r.statuses = append(r.statuses, st)
}
}
r.done = len(r.statuses) == 0
return r
}
// Review shows the diffs of outdated statuses and returns the accepted ones.
// It returns ErrCanceled if the user aborts.
func Review(ctx context.Context, statuses []core.SourceStatus) ([]core.SourceStatus, error) {
r := newReview(ctx, statuses)
if r.done {
return nil, nil
}
if err := run(ctx, r); err != nil {
return nil, err
}
if r.canceled {
return nil, ErrCanceled
}
return r.accepted, nil
}
func (r *review) leftWidth() int { return min(32, r.width/3) }
func (r *review) load() {
if r.done {
return
}
k := [2]int{r.i, r.sel}
d, ok := r.diffs[k]
if !ok {
var err error
d, err = core.Diff(r.ctx, r.statuses[r.i].Changes[r.sel])
if err != nil {
d = red.Render(err.Error())
} else if d == "" {
d = dim.Render("(no file contents to show)")
} else {
d = colorDiff(d)
}
r.diffs[k] = d
}
r.vp.SetContent(d)
r.vp.GotoTop()
}
func (r *review) next() {
r.i++
r.sel = 0
r.done = r.i >= len(r.statuses)
r.load()
}
func (r *review) update(msg tea.Msg) tea.Cmd {
key, ok := msg.(tea.KeyPressMsg)
if !ok || r.done {
return nil
}
changes := r.statuses[r.i].Changes
switch key.String() {
case "ctrl+c", "esc", "q":
r.canceled, r.done = true, true
case "y":
r.accepted = append(r.accepted, r.statuses[r.i])
r.next()
case "n":
r.next()
case "up", "shift+tab":
r.sel = max(0, r.sel-1)
r.load()
case "down", "tab":
r.sel = min(len(changes)-1, r.sel+1)
r.load()
default:
var cmd tea.Cmd
r.vp, cmd = r.vp.Update(msg)
return cmd
}
return nil
}
func (r *review) resize(width, height int) {
r.width, r.height = width, height
r.vp.SetWidth(max(1, width-r.leftWidth()-3))
r.vp.SetHeight(max(1, height-3))
r.load()
}
func (r *review) finished() bool { return r.done }
func (r *review) view() string {
if r.done {
return ""
}
st := r.statuses[r.i]
header := fmt.Sprintf("%s %s %s",
bold.Render(fmt.Sprintf("review %d/%d", r.i+1, len(r.statuses))),
bold.Render(st.Source), dim.Render(revs(st)))
var left strings.Builder
for j, ch := range st.Changes {
line := symbol(ch.Kind) + " " + ch.Name
if j == r.sel {
line = selected.Render("› ") + line
} else {
line = " " + line
}
left.WriteString(line + "\n")
}
bodyHeight := max(1, r.height-3)
leftCol := strings.Split(fit(left.String(), r.leftWidth(), bodyHeight), "\n")
rightCol := strings.Split(fit(r.vp.View(), r.width-r.leftWidth()-3, bodyHeight), "\n")
var body strings.Builder
for j := range bodyHeight {
fmt.Fprintf(&body, "%s %s %s\n", pad(leftCol[j], r.leftWidth()), dim.Render("│"), rightCol[j])
}
return header + "\n\n" + strings.TrimRight(body.String(), "\n") + "\n" +
dim.Render("y accept · n skip · ↑/↓ skill · j/k pgup/pgdn scroll · q abort")
}
func revs(st core.SourceStatus) string {
switch {
case st.OldRev == "":
return fmt.Sprintf("new → %.7s", st.NewRev)
case st.NewRev == "":
return fmt.Sprintf("%.7s → removed", st.OldRev)
default:
return fmt.Sprintf("%.7s → %.7s", st.OldRev, st.NewRev)
}
}
func symbol(k core.ChangeKind) string {
switch k {
case core.Added:
return green.Render(k.Symbol())
case core.Removed:
return red.Render(k.Symbol())
default:
return yellow.Render(k.Symbol())
}
}