mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
feat(analyze): add TUI list delegate with color dots and bar chart
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/bubbles/list"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
// analyzeSkillItem wraps analyzeSkillEntry for the bubbles/list delegate.
|
||||
type analyzeSkillItem struct {
|
||||
entry analyzeSkillEntry
|
||||
maxTokens int // largest desc tokens in current target (for bar scaling)
|
||||
}
|
||||
|
||||
func (i analyzeSkillItem) FilterValue() string { return i.entry.Name }
|
||||
|
||||
// computeThresholds returns P25 and P75 percentile values from a sorted token slice.
|
||||
func computeThresholds(tokens []int) (low, high int) {
|
||||
n := len(tokens)
|
||||
if n == 0 {
|
||||
return 0, 0
|
||||
}
|
||||
sorted := make([]int, n)
|
||||
copy(sorted, tokens)
|
||||
sort.Ints(sorted)
|
||||
if n == 1 {
|
||||
return sorted[0], sorted[0]
|
||||
}
|
||||
low = sorted[n/4]
|
||||
high = sorted[n*3/4]
|
||||
return low, high
|
||||
}
|
||||
|
||||
// tokenColorCode returns lipgloss color code based on thresholds.
|
||||
func tokenColorCode(tokens, low, high int) string {
|
||||
if tokens >= high {
|
||||
return "1" // red
|
||||
}
|
||||
if tokens >= low {
|
||||
return "3" // yellow
|
||||
}
|
||||
return "2" // green
|
||||
}
|
||||
|
||||
// renderTokenBar renders a proportional bar chart using block characters.
|
||||
func renderTokenBar(tokens, maxTokens, maxWidth int) string {
|
||||
if maxTokens <= 0 || tokens <= 0 || maxWidth <= 0 {
|
||||
return ""
|
||||
}
|
||||
width := tokens * maxWidth / maxTokens
|
||||
if width < 1 {
|
||||
width = 1
|
||||
}
|
||||
return strings.Repeat("█", width)
|
||||
}
|
||||
|
||||
// analyzeSkillDelegate renders each skill row in the list.
|
||||
type analyzeSkillDelegate struct {
|
||||
thresholdLow int
|
||||
thresholdHigh int
|
||||
}
|
||||
|
||||
func (d analyzeSkillDelegate) Height() int { return 1 }
|
||||
func (d analyzeSkillDelegate) Spacing() int { return 0 }
|
||||
func (d analyzeSkillDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
|
||||
|
||||
func (d analyzeSkillDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
|
||||
item, ok := listItem.(analyzeSkillItem)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
isSelected := index == m.Index()
|
||||
name := truncateName(item.entry.Name, analyzeNameMaxLen)
|
||||
tokenStr := formatTokensStr(item.entry.DescriptionChars)
|
||||
|
||||
colorCode := tokenColorCode(item.entry.DescriptionTokens, d.thresholdLow, d.thresholdHigh)
|
||||
dot := lipgloss.NewStyle().Foreground(lipgloss.Color(colorCode)).Render("●")
|
||||
|
||||
// Bar chart: use remaining width after name + token columns
|
||||
barMaxWidth := 12
|
||||
bar := renderTokenBar(item.entry.DescriptionTokens, item.maxTokens, barMaxWidth)
|
||||
barStyled := lipgloss.NewStyle().Foreground(lipgloss.Color(colorCode)).Render(bar)
|
||||
|
||||
line := fmt.Sprintf(" %s %-32s %8s %s", dot, name, tokenStr, barStyled)
|
||||
|
||||
if isSelected {
|
||||
line = tc.ListRowSelected.Render(line)
|
||||
}
|
||||
|
||||
fmt.Fprint(w, line)
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestComputeThresholds(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tokens []int
|
||||
wantLow int
|
||||
wantHigh int
|
||||
}{
|
||||
{"single", []int{100}, 100, 100},
|
||||
{"two", []int{10, 90}, 10, 90},
|
||||
{"four even", []int{10, 20, 30, 40}, 20, 40},
|
||||
{"empty", []int{}, 0, 0},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
low, high := computeThresholds(tt.tokens)
|
||||
if low != tt.wantLow {
|
||||
t.Errorf("low: got %d, want %d", low, tt.wantLow)
|
||||
}
|
||||
if high != tt.wantHigh {
|
||||
t.Errorf("high: got %d, want %d", high, tt.wantHigh)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTokenColor(t *testing.T) {
|
||||
tests := []struct {
|
||||
tokens int
|
||||
low, high int
|
||||
wantColor string
|
||||
}{
|
||||
{100, 20, 80, "1"},
|
||||
{50, 20, 80, "3"},
|
||||
{10, 20, 80, "2"},
|
||||
{80, 20, 80, "1"},
|
||||
{20, 20, 80, "3"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
color := tokenColorCode(tt.tokens, tt.low, tt.high)
|
||||
if color != tt.wantColor {
|
||||
t.Errorf("tokenColorCode(%d, %d, %d) = %q, want %q",
|
||||
tt.tokens, tt.low, tt.high, color, tt.wantColor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderBar(t *testing.T) {
|
||||
bar := renderTokenBar(50, 100, 10)
|
||||
if len([]rune(bar)) != 5 {
|
||||
t.Errorf("bar length: got %d, want 5", len([]rune(bar)))
|
||||
}
|
||||
bar = renderTokenBar(0, 100, 10)
|
||||
if bar != "" {
|
||||
t.Errorf("0 tokens bar: got %q, want empty", bar)
|
||||
}
|
||||
bar = renderTokenBar(50, 0, 10)
|
||||
if bar != "" {
|
||||
t.Errorf("maxTokens 0: got %q, want empty", bar)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user