mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
feat(diff): async expand with spinner and support all diff types
- Add loading spinner while computing file diffs (prevents TUI freeze) - Compute file diffs in parallel using sync.WaitGroup - Support expanding "local only" and "remove" items (not just modify/add) - Populate dstDir on remove/orphan items for file listing - Relax ensureFiles to work with dstDir-only entries - Add stale result guard when navigating during async load
This commit is contained in:
@@ -128,8 +128,12 @@ type copyDiffEntry struct {
|
||||
}
|
||||
|
||||
// ensureFiles lazily populates file-level diffs on first access.
|
||||
// Works for items with srcDir (full diff) or only dstDir (file listing).
|
||||
func (e *copyDiffEntry) ensureFiles() {
|
||||
if e.files != nil || e.srcDir == "" {
|
||||
if e.files != nil {
|
||||
return
|
||||
}
|
||||
if e.srcDir == "" && e.dstDir == "" {
|
||||
return
|
||||
}
|
||||
e.files = diffSkillFiles(e.srcDir, e.dstDir)
|
||||
@@ -541,7 +545,7 @@ func collectCopyDiff(r *targetDiffResult, targetName, targetPath string, filtere
|
||||
// Orphan managed copies
|
||||
for name := range manifest.Managed {
|
||||
if !sourceSkills[name] {
|
||||
r.items = append(r.items, copyDiffEntry{action: "remove", name: name, reason: "orphan (will be pruned)", isSync: true})
|
||||
r.items = append(r.items, copyDiffEntry{action: "remove", name: name, reason: "orphan (will be pruned)", isSync: true, dstDir: filepath.Join(targetPath, name)})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -557,7 +561,7 @@ func collectCopyDiff(r *targetDiffResult, targetName, targetPath string, filtere
|
||||
if _, isManaged := manifest.Managed[e.Name()]; isManaged {
|
||||
continue
|
||||
}
|
||||
r.items = append(r.items, copyDiffEntry{action: "remove", name: e.Name(), reason: "local only", isSync: false})
|
||||
r.items = append(r.items, copyDiffEntry{action: "remove", name: e.Name(), reason: "local only", isSync: false, dstDir: filepath.Join(targetPath, e.Name())})
|
||||
}
|
||||
|
||||
// Compute counts
|
||||
@@ -607,7 +611,7 @@ func collectMergeDiff(r *targetDiffResult, targetPath string, sourceSkills map[s
|
||||
// Skills only in target (local only)
|
||||
for skill := range targetSkills {
|
||||
if !sourceSkills[skill] && !targetSymlinks[skill] {
|
||||
r.items = append(r.items, copyDiffEntry{action: "remove", name: skill, reason: "local only", isSync: false})
|
||||
r.items = append(r.items, copyDiffEntry{action: "remove", name: skill, reason: "local only", isSync: false, dstDir: filepath.Join(targetPath, skill)})
|
||||
r.localCount++
|
||||
}
|
||||
}
|
||||
|
||||
+140
-49
@@ -5,13 +5,22 @@ import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/charmbracelet/bubbles/list"
|
||||
"github.com/charmbracelet/bubbles/spinner"
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
// diffExpandMsg is sent when async diff computation completes.
|
||||
type diffExpandMsg struct {
|
||||
skill string
|
||||
diff string
|
||||
files []fileDiffEntry
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Diff TUI — interactive diff browser: left panel target list, right panel
|
||||
// detail showing sync/local differences. Browse-only (no mutating actions).
|
||||
@@ -85,6 +94,11 @@ type diffTUIModel struct {
|
||||
// Expand state — file-level diff for a specific skill
|
||||
expandedSkill string // skill name currently expanded
|
||||
expandedDiff string // cached unified diff text
|
||||
expandedFiles []fileDiffEntry
|
||||
|
||||
// Async loading — spinner shown while computing file diffs
|
||||
loading bool
|
||||
loadSpinner spinner.Model
|
||||
|
||||
// Cached detail data — recomputed only on selection change
|
||||
cachedIdx int
|
||||
@@ -147,11 +161,16 @@ func newDiffTUIModel(results []targetDiffResult) diffTUIModel {
|
||||
fi.PromptStyle = tc.Filter
|
||||
fi.Cursor.Style = tc.Filter
|
||||
|
||||
sp := spinner.New()
|
||||
sp.Spinner = spinner.Dot
|
||||
sp.Style = tc.SpinnerStyle
|
||||
|
||||
return diffTUIModel{
|
||||
allItems: sorted,
|
||||
targetList: tl,
|
||||
matchCount: len(sorted),
|
||||
filterInput: fi,
|
||||
loadSpinner: sp,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,6 +222,25 @@ func (m diffTUIModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
|
||||
case tea.KeyMsg:
|
||||
return m.handleDiffKey(msg)
|
||||
|
||||
case spinner.TickMsg:
|
||||
if m.loading {
|
||||
var cmd tea.Cmd
|
||||
m.loadSpinner, cmd = m.loadSpinner.Update(msg)
|
||||
return m, cmd
|
||||
}
|
||||
return m, nil
|
||||
|
||||
case diffExpandMsg:
|
||||
// Discard stale result if user navigated away (loading reset on nav)
|
||||
if !m.loading {
|
||||
return m, nil
|
||||
}
|
||||
m.loading = false
|
||||
m.expandedSkill = msg.skill
|
||||
m.expandedDiff = msg.diff
|
||||
m.expandedFiles = msg.files
|
||||
return m, nil
|
||||
}
|
||||
|
||||
var cmd tea.Cmd
|
||||
@@ -248,6 +286,9 @@ func (m diffTUIModel) handleDiffKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
return m, textinput.Blink
|
||||
|
||||
case "enter":
|
||||
if m.loading {
|
||||
return m, nil // ignore while loading
|
||||
}
|
||||
item, ok := m.targetList.SelectedItem().(diffTargetItem)
|
||||
if !ok {
|
||||
return m, nil
|
||||
@@ -260,30 +301,22 @@ func (m diffTUIModel) handleDiffKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
if m.expandedSkill != "" {
|
||||
m.expandedSkill = ""
|
||||
m.expandedDiff = ""
|
||||
} else {
|
||||
for i := range r.items {
|
||||
if r.items[i].action == "modify" || r.items[i].action == "add" {
|
||||
r.items[i].ensureFiles()
|
||||
m.expandedSkill = r.items[i].name
|
||||
// Build unified diff for all modified files
|
||||
var diffBuf strings.Builder
|
||||
for _, f := range r.items[i].files {
|
||||
if f.Action == "modify" && r.items[i].srcDir != "" {
|
||||
srcFile := filepath.Join(r.items[i].srcDir, f.RelPath)
|
||||
dstFile := filepath.Join(r.items[i].dstDir, f.RelPath)
|
||||
diffText := generateUnifiedDiff(srcFile, dstFile)
|
||||
if diffText != "" {
|
||||
diffBuf.WriteString(fmt.Sprintf("--- %s\n", f.RelPath))
|
||||
diffBuf.WriteString(diffText)
|
||||
}
|
||||
}
|
||||
}
|
||||
m.expandedDiff = diffBuf.String()
|
||||
break
|
||||
}
|
||||
m.expandedFiles = nil
|
||||
m.detailScroll = 0
|
||||
return m, nil
|
||||
}
|
||||
// Find first expandable skill (any with srcDir or dstDir)
|
||||
for i := range r.items {
|
||||
entry := r.items[i]
|
||||
if entry.srcDir != "" || entry.dstDir != "" {
|
||||
m.loading = true
|
||||
m.expandedSkill = ""
|
||||
m.expandedDiff = ""
|
||||
m.expandedFiles = nil
|
||||
m.detailScroll = 0
|
||||
return m, tea.Batch(m.loadSpinner.Tick, expandDiffCmd(&entry))
|
||||
}
|
||||
}
|
||||
m.detailScroll = 0
|
||||
return m, nil
|
||||
|
||||
// Detail scroll
|
||||
@@ -308,6 +341,8 @@ func (m diffTUIModel) handleDiffKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
m.detailScroll = 0
|
||||
m.expandedSkill = ""
|
||||
m.expandedDiff = ""
|
||||
m.expandedFiles = nil
|
||||
m.loading = false // cancel in-flight diff if navigating away
|
||||
m.refreshDetailCache()
|
||||
}
|
||||
|
||||
@@ -467,8 +502,13 @@ func (m diffTUIModel) buildDiffDetail() string {
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// Use cached sorted items + categories (refreshed on selection change)
|
||||
items := m.cachedItems
|
||||
// Loading spinner
|
||||
if m.loading {
|
||||
b.WriteString(fmt.Sprintf(" %s Loading diff...\n", m.loadSpinner.View()))
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// Use cached sorted categories (refreshed on selection change)
|
||||
cats := m.cachedCats
|
||||
for _, cat := range cats {
|
||||
n := len(cat.names)
|
||||
@@ -509,33 +549,26 @@ func (m diffTUIModel) buildDiffDetail() string {
|
||||
|
||||
// File list + diff content (only shown after Enter toggle)
|
||||
if m.expandedSkill != "" {
|
||||
// File list for the expanded skill
|
||||
for _, item := range items {
|
||||
if item.name != m.expandedSkill {
|
||||
continue
|
||||
}
|
||||
if len(item.files) > 0 {
|
||||
b.WriteString("\n")
|
||||
b.WriteString(tc.Separator.Render(fmt.Sprintf("── %s files ──", item.name)))
|
||||
b.WriteString("\n")
|
||||
for _, f := range item.files {
|
||||
var icon string
|
||||
var style lipgloss.Style
|
||||
switch f.Action {
|
||||
case "add":
|
||||
icon, style = "+", tc.Green
|
||||
case "delete":
|
||||
icon, style = "-", tc.Red
|
||||
case "modify":
|
||||
icon, style = "~", tc.Cyan
|
||||
default:
|
||||
icon, style = "?", tc.Dim
|
||||
}
|
||||
b.WriteString(style.Render(fmt.Sprintf(" %s %s", icon, f.RelPath)))
|
||||
b.WriteString("\n")
|
||||
if len(m.expandedFiles) > 0 {
|
||||
b.WriteString("\n")
|
||||
b.WriteString(tc.Separator.Render(fmt.Sprintf("── %s files ──", m.expandedSkill)))
|
||||
b.WriteString("\n")
|
||||
for _, f := range m.expandedFiles {
|
||||
var icon string
|
||||
var style lipgloss.Style
|
||||
switch f.Action {
|
||||
case "add":
|
||||
icon, style = "+", tc.Green
|
||||
case "delete":
|
||||
icon, style = "-", tc.Red
|
||||
case "modify":
|
||||
icon, style = "~", tc.Cyan
|
||||
default:
|
||||
icon, style = "?", tc.Dim
|
||||
}
|
||||
b.WriteString(style.Render(fmt.Sprintf(" %s %s", icon, f.RelPath)))
|
||||
b.WriteString("\n")
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
// Unified diff content
|
||||
@@ -557,6 +590,11 @@ func (m diffTUIModel) buildDiffDetail() string {
|
||||
b.WriteString("\n")
|
||||
}
|
||||
}
|
||||
if len(m.expandedFiles) == 0 && m.expandedDiff == "" {
|
||||
b.WriteString("\n")
|
||||
b.WriteString(tc.Dim.Render(" (No file-level diff available)"))
|
||||
b.WriteString("\n")
|
||||
}
|
||||
}
|
||||
|
||||
// Next Steps
|
||||
@@ -589,6 +627,59 @@ func (m diffTUIModel) buildDiffDetail() string {
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// --- Async diff ---
|
||||
|
||||
// expandDiffCmd returns a tea.Cmd that computes file diffs in a goroutine.
|
||||
// For items with srcDir, also generates parallel unified diffs for modified files.
|
||||
// For items with only dstDir (local only), populates file list without diff.
|
||||
func expandDiffCmd(entry *copyDiffEntry) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
entry.ensureFiles()
|
||||
|
||||
// For remove/local-only items without srcDir, just return file list (no diff)
|
||||
if entry.srcDir == "" {
|
||||
return diffExpandMsg{skill: entry.name, files: entry.files}
|
||||
}
|
||||
|
||||
// Collect modified files for diff
|
||||
var modFiles []fileDiffEntry
|
||||
for _, f := range entry.files {
|
||||
if f.Action == "modify" {
|
||||
modFiles = append(modFiles, f)
|
||||
}
|
||||
}
|
||||
|
||||
// Parallel diff computation
|
||||
results := make([]string, len(modFiles))
|
||||
var wg sync.WaitGroup
|
||||
for i, f := range modFiles {
|
||||
wg.Add(1)
|
||||
go func(idx int, fe fileDiffEntry) {
|
||||
defer wg.Done()
|
||||
src := filepath.Join(entry.srcDir, fe.RelPath)
|
||||
dst := filepath.Join(entry.dstDir, fe.RelPath)
|
||||
results[idx] = generateUnifiedDiff(src, dst)
|
||||
}(i, f)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// Combine results preserving order
|
||||
var buf strings.Builder
|
||||
for i, f := range modFiles {
|
||||
if results[i] != "" {
|
||||
buf.WriteString(fmt.Sprintf("--- %s\n", f.RelPath))
|
||||
buf.WriteString(results[i])
|
||||
}
|
||||
}
|
||||
|
||||
return diffExpandMsg{
|
||||
skill: entry.name,
|
||||
diff: buf.String(),
|
||||
files: entry.files,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Entry point ---
|
||||
|
||||
func runDiffTUI(results []targetDiffResult) error {
|
||||
|
||||
Reference in New Issue
Block a user