diff --git a/cmd/skillshare/analyze.go b/cmd/skillshare/analyze.go index 8abc45164..e6f1c319b 100644 --- a/cmd/skillshare/analyze.go +++ b/cmd/skillshare/analyze.go @@ -407,7 +407,3 @@ func printAnalyzeVerbose(entries []analyzeTargetEntry) { } } } - -func runAnalyzeTUI(loadFn func() analyzeLoadResult, modeLabel string) error { - return fmt.Errorf("analyze TUI not yet implemented") -} diff --git a/cmd/skillshare/analyze_tui.go b/cmd/skillshare/analyze_tui.go new file mode 100644 index 000000000..d92bb3633 --- /dev/null +++ b/cmd/skillshare/analyze_tui.go @@ -0,0 +1,572 @@ +package main + +import ( + "fmt" + "sort" + "strings" + + "github.com/charmbracelet/bubbles/list" + "github.com/charmbracelet/bubbles/spinner" + "github.com/charmbracelet/bubbles/textinput" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" +) + +type analyzeTUIModel struct { + list list.Model + allItems []analyzeSkillItem + filterText string + filterInput textinput.Model + filtering bool + matchCount int + + targets []analyzeTargetEntry + targetIdx int + + sortBy string // "tokens" | "name" + sortAsc bool + + thresholdHigh int + thresholdLow int + + detailScroll int + + termWidth int + termHeight int + quitting bool + loading bool + loadSpinner spinner.Model + loadFn func() analyzeLoadResult + loadErr error + modeLabel string +} + +type analyzeDataLoadedMsg struct { + result analyzeLoadResult +} + +func newAnalyzeTUIModel(loadFn func() analyzeLoadResult, modeLabel string) analyzeTUIModel { + sp := spinner.New() + sp.Spinner = spinner.Dot + sp.Style = tc.SpinnerStyle + + fi := textinput.New() + fi.Prompt = "/ " + fi.PromptStyle = tc.Filter + fi.Cursor.Style = tc.Filter + fi.Placeholder = "filter skills" + + return analyzeTUIModel{ + loading: true, + loadFn: loadFn, + loadSpinner: sp, + modeLabel: modeLabel, + sortBy: "tokens", + filterInput: fi, + } +} + +func (m analyzeTUIModel) Init() tea.Cmd { + if m.loading && m.loadFn != nil { + fn := m.loadFn + return tea.Batch(m.loadSpinner.Tick, func() tea.Msg { + return analyzeDataLoadedMsg{result: fn()} + }) + } + return nil +} + +func (m *analyzeTUIModel) switchTarget() { + if len(m.targets) == 0 { + return + } + entry := m.targets[m.targetIdx] + maxTokens := 0 + for _, s := range entry.Skills { + if s.DescriptionTokens > maxTokens { + maxTokens = s.DescriptionTokens + } + } + items := make([]analyzeSkillItem, len(entry.Skills)) + for i, s := range entry.Skills { + items[i] = analyzeSkillItem{entry: s, maxTokens: maxTokens} + } + m.allItems = items + m.recomputeThresholds() + m.list.SetDelegate(analyzeSkillDelegate{ + thresholdLow: m.thresholdLow, + thresholdHigh: m.thresholdHigh, + }) + m.applyFilter() +} + +func (m *analyzeTUIModel) recomputeThresholds() { + tokens := make([]int, len(m.allItems)) + for i, item := range m.allItems { + tokens[i] = item.entry.DescriptionTokens + } + m.thresholdLow, m.thresholdHigh = computeThresholds(tokens) +} + +func (m *analyzeTUIModel) applyFilter() { + m.detailScroll = 0 + + items := m.allItems + if m.filterText != "" { + lower := strings.ToLower(m.filterText) + var filtered []analyzeSkillItem + for _, item := range m.allItems { + if strings.Contains(strings.ToLower(item.entry.Name), lower) { + filtered = append(filtered, item) + } + } + items = filtered + } + + m.sortItems(items) + m.matchCount = len(items) + + listItems := make([]list.Item, len(items)) + for i, item := range items { + listItems[i] = item + } + m.list.SetItems(listItems) + m.list.ResetSelected() +} + +func (m *analyzeTUIModel) sortItems(items []analyzeSkillItem) { + switch m.sortBy { + case "tokens": + if m.sortAsc { + sortAnalyzeItems(items, func(a, b analyzeSkillItem) bool { + return a.entry.DescriptionTokens < b.entry.DescriptionTokens + }) + } else { + sortAnalyzeItems(items, func(a, b analyzeSkillItem) bool { + return a.entry.DescriptionTokens > b.entry.DescriptionTokens + }) + } + case "name": + if m.sortAsc { + sortAnalyzeItems(items, func(a, b analyzeSkillItem) bool { + return a.entry.Name < b.entry.Name + }) + } else { + sortAnalyzeItems(items, func(a, b analyzeSkillItem) bool { + return a.entry.Name > b.entry.Name + }) + } + } +} + +func sortAnalyzeItems(items []analyzeSkillItem, less func(a, b analyzeSkillItem) bool) { + sort.Slice(items, func(i, j int) bool { return less(items[i], items[j]) }) +} + +func (m *analyzeTUIModel) cycleSort() { + switch { + case m.sortBy == "tokens" && !m.sortAsc: + m.sortAsc = true // tokens ↑ + case m.sortBy == "tokens" && m.sortAsc: + m.sortBy = "name" + m.sortAsc = true // name A→Z + case m.sortBy == "name" && m.sortAsc: + m.sortAsc = false // name Z→A + default: + m.sortBy = "tokens" + m.sortAsc = false // tokens ↓ (default) + } + m.applyFilter() +} + +func (m analyzeTUIModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case tea.WindowSizeMsg: + m.termWidth = msg.Width + m.termHeight = msg.Height + m.syncListSize() + return m, nil + + case spinner.TickMsg: + if m.loading { + var cmd tea.Cmd + m.loadSpinner, cmd = m.loadSpinner.Update(msg) + return m, cmd + } + + case analyzeDataLoadedMsg: + m.loading = false + m.loadFn = nil + if msg.result.err != nil { + m.loadErr = msg.result.err + m.quitting = true + return m, tea.Quit + } + if len(msg.result.targets) == 0 { + m.quitting = true + return m, tea.Quit + } + m.targets = msg.result.targets + m.targetIdx = 0 + delegate := analyzeSkillDelegate{} + l := list.New(nil, delegate, 0, 0) + l.Title = m.listTitle() + l.Styles.Title = tc.ListTitle + l.SetShowStatusBar(false) + l.SetFilteringEnabled(false) + l.SetShowHelp(false) + l.SetShowPagination(false) + m.list = l + m.switchTarget() + m.list.SetDelegate(analyzeSkillDelegate{ + thresholdLow: m.thresholdLow, + thresholdHigh: m.thresholdHigh, + }) + m.syncListSize() + return m, nil + + case tea.MouseMsg: + if listSplitActive(m.termWidth) && !m.loading { + leftWidth := listPanelWidth(m.termWidth) + if msg.X > leftWidth { + switch msg.Button { + case tea.MouseButtonWheelUp: + if m.detailScroll > 0 { + m.detailScroll-- + } + return m, nil + case tea.MouseButtonWheelDown: + m.detailScroll++ + return m, nil + } + } + } + + case tea.KeyMsg: + if m.loading { + if msg.String() == "q" || msg.String() == "ctrl+c" { + m.quitting = true + return m, tea.Quit + } + return m, nil + } + + if m.filtering { + switch msg.String() { + case "esc": + m.filtering = false + m.filterText = "" + m.filterInput.SetValue("") + m.applyFilter() + return m, nil + case "enter": + m.filtering = false + return m, nil + } + var cmd tea.Cmd + m.filterInput, cmd = m.filterInput.Update(msg) + newVal := m.filterInput.Value() + if newVal != m.filterText { + m.filterText = newVal + m.applyFilter() + } + return m, cmd + } + + switch msg.String() { + case "q", "ctrl+c": + m.quitting = true + return m, tea.Quit + case "tab": + if len(m.targets) > 1 { + m.targetIdx = (m.targetIdx + 1) % len(m.targets) + m.switchTarget() + m.list.Title = m.listTitle() + } + return m, nil + case "shift+tab": + if len(m.targets) > 1 { + m.targetIdx = (m.targetIdx - 1 + len(m.targets)) % len(m.targets) + m.switchTarget() + m.list.Title = m.listTitle() + } + return m, nil + case "s": + m.cycleSort() + return m, nil + case "/": + m.filtering = true + m.filterInput.Focus() + return m, textinput.Blink + case "ctrl+d": + m.detailScroll += 8 + return m, nil + case "ctrl+u": + m.detailScroll -= 8 + if m.detailScroll < 0 { + m.detailScroll = 0 + } + return m, nil + } + } + + var cmd tea.Cmd + prevSelected := m.selectedKey() + m.list, cmd = m.list.Update(msg) + if m.selectedKey() != prevSelected { + m.detailScroll = 0 + } + return m, cmd +} + +func (m analyzeTUIModel) selectedKey() string { + item, ok := m.list.SelectedItem().(analyzeSkillItem) + if !ok { + return "" + } + return item.entry.Name +} + +func (m *analyzeTUIModel) syncListSize() { + if listSplitActive(m.termWidth) { + panelHeight := m.termHeight - 7 + if panelHeight < 6 { + panelHeight = 6 + } + m.list.SetSize(listPanelWidth(m.termWidth), panelHeight) + return + } + listHeight := m.termHeight - 20 + if listHeight < 6 { + listHeight = 6 + } + m.list.SetSize(m.termWidth, listHeight) +} + +func (m analyzeTUIModel) listTitle() string { + if len(m.targets) == 0 { + return "Context Analysis" + } + e := m.targets[m.targetIdx] + return fmt.Sprintf("%s · %d skills · %s tokens", e.Name, e.SkillCount, + formatTokensStr(e.AlwaysLoaded.Chars)) +} + +func (m analyzeTUIModel) View() string { + if m.quitting { + return "" + } + if m.loading { + return fmt.Sprintf("\n %s Loading skills...\n", m.loadSpinner.View()) + } + if listSplitActive(m.termWidth) { + return m.viewSplit() + } + return m.viewVertical() +} + +func (m analyzeTUIModel) viewSplit() string { + var b strings.Builder + + panelHeight := m.termHeight - 7 + if panelHeight < 6 { + panelHeight = 6 + } + + leftWidth := listPanelWidth(m.termWidth) + rightWidth := listDetailPanelWidth(m.termWidth) + + var detailStr, scrollInfo string + if item, ok := m.list.SelectedItem().(analyzeSkillItem); ok { + header := m.renderDetailHeader(item.entry, rightWidth-1) + bodyHeight := panelHeight - lipgloss.Height(header) - 2 + if bodyHeight < 4 { + bodyHeight = 4 + } + body, bodyScrollInfo := wrapAndScroll(m.renderDetailBody(item.entry, rightWidth-1), rightWidth-1, m.detailScroll, bodyHeight) + scrollInfo = bodyScrollInfo + detailStr = "\n" + header + "\n\n" + body + } + + body := renderHorizontalSplit(m.list.View(), detailStr, leftWidth, rightWidth, panelHeight) + b.WriteString(body) + b.WriteString("\n\n") + b.WriteString(m.renderFilterBar()) + b.WriteString(m.renderTargetBar()) + b.WriteString(m.renderStatsLine()) + b.WriteString("\n") + help := m.helpText() + help = appendScrollInfo(help, scrollInfo) + b.WriteString(tc.Help.Render(help)) + b.WriteString("\n") + + return b.String() +} + +func (m analyzeTUIModel) viewVertical() string { + var b strings.Builder + b.WriteString(m.list.View()) + b.WriteString("\n\n") + b.WriteString(m.renderFilterBar()) + + var scrollInfo string + if item, ok := m.list.SelectedItem().(analyzeSkillItem); ok { + detailHeight := m.termHeight - m.termHeight*2/5 - 10 + if detailHeight < 6 { + detailHeight = 6 + } + header := m.renderDetailHeader(item.entry, m.termWidth) + bodyHeight := detailHeight - lipgloss.Height(header) - 1 + if bodyHeight < 4 { + bodyHeight = 4 + } + body, bodyScrollInfo := wrapAndScroll(m.renderDetailBody(item.entry, m.termWidth), m.termWidth, m.detailScroll, bodyHeight) + scrollInfo = bodyScrollInfo + b.WriteString(header) + b.WriteString("\n\n") + b.WriteString(body) + } + + b.WriteString(m.renderTargetBar()) + b.WriteString(m.renderStatsLine()) + b.WriteString("\n") + help := m.helpText() + help = appendScrollInfo(help, scrollInfo) + b.WriteString(tc.Help.Render(help)) + b.WriteString("\n") + + return b.String() +} + +func (m analyzeTUIModel) renderFilterBar() string { + return renderTUIFilterBar( + m.filterInput.View(), m.filtering, m.filterText, + m.matchCount, len(m.allItems), 0, + "skills", renderPageInfoFromPaginator(m.list.Paginator), + ) +} + +func (m analyzeTUIModel) renderTargetBar() string { + if len(m.targets) <= 1 { + return "" + } + var parts []string + for i, t := range m.targets { + label := fmt.Sprintf("%s (%d)", t.Name, t.SkillCount) + if i == m.targetIdx { + parts = append(parts, tc.Cyan.Render("► "+label)) + } else { + parts = append(parts, tc.Dim.Render(" "+label)) + } + } + return strings.Join(parts, tc.Dim.Render(" │ ")) + "\n" +} + +func (m analyzeTUIModel) renderStatsLine() string { + if len(m.targets) == 0 { + return "" + } + e := m.targets[m.targetIdx] + return tc.Help.Render(fmt.Sprintf("Always: %s tokens On-demand: %s tokens %s", + formatTokensStr(e.AlwaysLoaded.Chars), + formatTokensStr(e.OnDemandMax.Chars), + tc.Dim.Render("(1 token ≈ 4 chars)"), + )) + "\n" +} + +func (m analyzeTUIModel) renderDetailHeader(e analyzeSkillEntry, width int) string { + title := tc.ListTitle.Render(e.Name) + return lipgloss.NewStyle().Width(width).Render(title) +} + +func (m analyzeTUIModel) renderDetailBody(e analyzeSkillEntry, width int) string { + var b strings.Builder + + // Token breakdown + target := m.targets[m.targetIdx] + pct := 0.0 + if target.AlwaysLoaded.Chars > 0 { + pct = float64(e.DescriptionChars) / float64(target.AlwaysLoaded.Chars) * 100 + } + tokenRows := []string{ + renderFactRow("Desc tokens", fmt.Sprintf("%s (%.0f%%)", formatTokensStr(e.DescriptionChars), pct)), + renderFactRow("Body tokens", formatTokensStr(e.BodyChars)), + renderFactRow("Total", formatTokensStr(e.DescriptionChars+e.BodyChars)), + } + b.WriteString(renderDetailSection("Tokens", strings.Join(tokenRows, "\n"), width)) + b.WriteString("\n\n") + + // Metadata + var metaRows []string + if e.relPath != "" { + metaRows = append(metaRows, renderFactRow("Path", tc.Cyan.Render(e.relPath))) + } + if e.isTracked { + metaRows = append(metaRows, renderFactRow("Tracked", tc.Green.Render("✓"))) + } + if len(e.targetNames) > 0 { + metaRows = append(metaRows, renderFactRow("Targets", strings.Join(e.targetNames, ", "))) + } else { + metaRows = append(metaRows, renderFactRow("Targets", tc.Dim.Render("all"))) + } + if len(metaRows) > 0 { + b.WriteString(renderDetailSection("Details", strings.Join(metaRows, "\n"), width)) + b.WriteString("\n\n") + } + + // Description preview + if e.description != "" { + maxWidth := width - 4 + if maxWidth < 32 { + maxWidth = 32 + } + lines := wordWrapLines(e.description, maxWidth) + const maxLines = 6 + if len(lines) > maxLines { + lines = lines[:maxLines] + lines[len(lines)-1] += "..." + } + body := strings.Join(lines, "\n") + b.WriteString(renderDetailSection("Description", tc.Value.Render(body), width)) + } + + return b.String() +} + +func (m analyzeTUIModel) helpText() string { + if m.filtering { + return "Enter lock Esc clear q quit" + } + sortLabel := "tokens↓" + switch { + case m.sortBy == "tokens" && m.sortAsc: + sortLabel = "tokens↑" + case m.sortBy == "name" && m.sortAsc: + sortLabel = "name↑" + case m.sortBy == "name" && !m.sortAsc: + sortLabel = "name↓" + } + help := fmt.Sprintf("↑↓ navigate ←→ page / filter s sort(%s) Ctrl+d/u detail", sortLabel) + if len(m.targets) > 1 { + help += " Tab target" + } + help += " q quit" + return help +} + +func runAnalyzeTUI(loadFn func() analyzeLoadResult, modeLabel string) error { + model := newAnalyzeTUIModel(loadFn, modeLabel) + p := tea.NewProgram(model, tea.WithAltScreen(), tea.WithMouseCellMotion()) + finalModel, err := p.Run() + if err != nil { + return err + } + m, ok := finalModel.(analyzeTUIModel) + if !ok { + return nil + } + if m.loadErr != nil { + return m.loadErr + } + return nil +}