Skip to content

mission control for agent skills

Optimize TUI rendering performance and cache search text

alvinreal8830ec4c3897
diff --git a/internal/tui/filter.go b/internal/tui/filter.go
index ed66ab3..1be1358 100644
--- a/internal/tui/filter.go
+++ b/internal/tui/filter.go
@@ -6,7 +6,6 @@ import (
"github.com/alvinunreal/lazyskills/internal/agents"
"github.com/alvinunreal/lazyskills/internal/compat"
- "github.com/alvinunreal/lazyskills/internal/display"
"github.com/alvinunreal/lazyskills/internal/model"
)
@@ -24,8 +23,7 @@ func (m appModel) filteredSkills() []*model.Skill {
continue
}
if query != "" {
- view := display.Skill(sk)
- haystack := strings.ToLower(view.Name + " " + view.Description)
+ haystack := m.cachedSkillSearchText(sk)
if !strings.Contains(haystack, query) {
continue
}
@@ -42,8 +40,8 @@ func sortSkills(skills []*model.Skill) {
if leftGroup != rightGroup {
return leftGroup < rightGroup
}
- left := strings.ToLower(display.Skill(skills[i]).Name)
- right := strings.ToLower(display.Skill(skills[j]).Name)
+ left := strings.ToLower(compat.SanitizeMetadata(skills[i].Name))
+ right := strings.ToLower(compat.SanitizeMetadata(skills[j].Name))
if left != right {
return left < right
}
@@ -51,6 +49,33 @@ func sortSkills(skills []*model.Skill) {
})
}
+func (m *appModel) rebuildSkillSearchText() {
+ if len(m.result.Skills) == 0 {
+ m.skillSearchText = nil
+ return
+ }
+ m.skillSearchText = make(map[*model.Skill]string, len(m.result.Skills))
+ for _, sk := range m.result.Skills {
+ m.skillSearchText[sk] = buildSkillSearchText(sk)
+ }
+}
+
+func (m appModel) cachedSkillSearchText(sk *model.Skill) string {
+ if m.skillSearchText != nil {
+ if text, ok := m.skillSearchText[sk]; ok {
+ return text
+ }
+ }
+ return buildSkillSearchText(sk)
+}
+
+func buildSkillSearchText(sk *model.Skill) string {
+ if sk == nil {
+ return ""
+ }
+ return strings.ToLower(compat.SanitizeMetadata(sk.Name) + " " + compat.SanitizeMetadata(sk.Description))
+}
+
func (m appModel) agentFilters() []string {
var detected []string
if len(m.result.Agents) == 0 {
diff --git a/internal/tui/helpers.go b/internal/tui/helpers.go
index 193f8c5..74ea00c 100644
--- a/internal/tui/helpers.go
+++ b/internal/tui/helpers.go
@@ -321,12 +321,16 @@ func truncateTitle(s string, width int) string {
return string(runes[:width-1]) + "…"
}
-func (m appModel) isSkillInstalled(name string, group string) bool {
- norm := compat.NormalizeName(name)
+func (m appModel) installedSkillNames(group string) map[string]bool {
+ installed := make(map[string]bool)
for _, sk := range m.result.Skills {
- if listGroupLabel(sk) == group && compat.NormalizeName(sk.Name) == norm {
- return true
+ if listGroupLabel(sk) == group {
+ installed[compat.NormalizeName(sk.Name)] = true
}
}
- return false
+ return installed
+}
+
+func isSkillNameInstalled(name string, installed map[string]bool) bool {
+ return installed[compat.NormalizeName(name)]
}
diff --git a/internal/tui/state.go b/internal/tui/state.go
index 62392bf..fac0195 100644
--- a/internal/tui/state.go
+++ b/internal/tui/state.go
@@ -40,12 +40,13 @@ func (m *appModel) syncViewport() {
// For metadata viewport:
m.metadataViewport.Width = max(1, rightWidth-4)
m.metadataViewport.Height = max(1, topHeight-2)
- m.metadataViewport.SetContent(strings.Join(m.metadataLines(rightWidth-4), "\n"))
+ rows := m.visibleRows()
+ m.metadataViewport.SetContent(strings.Join(m.metadataLinesForRows(rows, rightWidth-4), "\n"))
// For preview viewport:
m.previewViewport.Width = max(1, rightWidth-4)
m.previewViewport.Height = max(1, bottomHeight-2)
- m.previewViewport.SetContent(strings.Join(m.previewLines(rightWidth-4), "\n"))
+ m.previewViewport.SetContent(strings.Join(m.previewLinesForRows(rows, rightWidth-4), "\n"))
}
m.clampViewportOffset()
}
@@ -91,9 +92,10 @@ func (m appModel) availableCount(groupName string) int {
if !ok || disc.Status != DiscoveryReady {
return 0
}
+ installed := m.installedSkillNames(groupName)
n := 0
for _, ds := range disc.Skills {
- if !m.isSkillInstalled(ds.Name, groupName) {
+ if !isSkillNameInstalled(ds.Name, installed) {
n++
}
}
diff --git a/internal/tui/tui.go b/internal/tui/tui.go
index 06b8a60..574927a 100644
--- a/internal/tui/tui.go
+++ b/internal/tui/tui.go
@@ -81,6 +81,7 @@ type appModel struct {
focus focusState
collapsedGroups map[string]bool
discovery map[string]SourceDiscovery
+ skillSearchText map[*model.Skill]string
modalSelected int
modalSource string
pendingG bool // saw a lone "g"; a second "g" jumps to top
diff --git a/internal/tui/update.go b/internal/tui/update.go
index f72dbb3..0a22571 100644
--- a/internal/tui/update.go
+++ b/internal/tui/update.go
@@ -25,6 +25,7 @@ func (m appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case snapshotMsg:
m.result = msg.result
sortSkills(m.result.Skills)
+ m.rebuildSkillSearchText()
m.err = msg.err
if m.agent != "" {
detected := false
@@ -672,8 +673,9 @@ func (m appModel) modalChildRows(groupName string) []modalChildRow {
// 2. Available skills
disc, ok := m.discovery[groupName]
if ok && disc.Status == DiscoveryReady {
+ installed := m.installedSkillNames(groupName)
for i, ds := range disc.Skills {
- if !m.isSkillInstalled(ds.Name, groupName) {
+ if !isSkillNameInstalled(ds.Name, installed) {
rows = append(rows, modalChildRow{
isAvailable: true,
discoveredSkill: &disc.Skills[i],
diff --git a/internal/tui/view.go b/internal/tui/view.go
index 4a2fd26..13d4c21 100644
--- a/internal/tui/view.go
+++ b/internal/tui/view.go
@@ -22,6 +22,22 @@ func (m appModel) View() string {
return smallTerminalView(layout.Width, layout.Height)
}
+ if m.detailModal {
+ return m.detailModalOverlay(layout)
+ }
+ if m.helpOpen {
+ return m.helpModalOverlay(layout)
+ }
+ if m.running {
+ return m.runningOverlay(layout)
+ }
+ if m.confirming {
+ return m.confirmationOverlay(layout)
+ }
+ if m.commands {
+ return m.commandsOverlay(layout)
+ }
+
// Keep View pure for callers: sync a local copy so render-time fallback
// sizing does not mutate the model stored by Bubble Tea.
viewModel := m
@@ -51,21 +67,6 @@ func (m appModel) View() string {
rightSide := lipgloss.JoinVertical(lipgloss.Left, metadata, preview)
view := lipgloss.JoinHorizontal(lipgloss.Top, list, rightSide)
- if viewModel.detailModal {
- return viewModel.detailModalOverlay(layout)
- }
- if viewModel.helpOpen {
- return viewModel.helpModalOverlay(layout)
- }
- if viewModel.running {
- return viewModel.runningOverlay(layout)
- }
- if viewModel.confirming {
- return viewModel.confirmationOverlay(layout)
- }
- if viewModel.commands {
- return viewModel.commandsOverlay(layout)
- }
footer := viewModel.footerText(layout.Width)
return view + "\n" + footer
}
@@ -115,9 +116,9 @@ func (m appModel) listTitle() string {
}
func (m appModel) listPane(height, width int) string {
- items := m.filteredSkills()
+ vRows := m.visibleRows()
var lines []string
- if len(items) == 0 {
+ if len(vRows) == 0 {
var detail []string
if m.result.Preflight != nil && !m.result.Preflight.CanRunSkills {
detail = append(detail,
@@ -179,11 +180,15 @@ func (m appModel) listPane(height, width int) string {
}
visible := max(1, height-len(lines))
- vRows := m.visibleRows()
- var renderedRows []string
selectedRow := m.selected
+ start := 0
+ if selectedRow >= visible {
+ start = selectedRow - visible + 1
+ }
+ end := min(len(vRows), start+visible)
- for idx, row := range vRows {
+ for idx, row := range vRows[start:end] {
+ rowIndex := start + idx
var line string
if row.isHeader {
affordance := "- "
@@ -195,7 +200,7 @@ func (m appModel) listPane(height, width int) string {
if n := m.availableCount(row.groupName); n > 0 {
hint = fmt.Sprintf(" +%d available", n)
}
- if idx == selectedRow {
+ if rowIndex == selectedRow {
line = selectedStyle.Render(truncate(headerText+hint, width))
} else {
line = dimStyle.Render(truncate(headerText, width-lipgloss.Width(hint))) + dimStyle.Render(hint)
@@ -229,7 +234,7 @@ func (m appModel) listPane(height, width int) string {
nameCore := truncate(mark+view.Name, max(1, width-lipgloss.Width(tail)))
switch {
- case idx == selectedRow:
+ case rowIndex == selectedRow:
line = selectedStyle.Render(nameCore + tail)
case issueErrors > 0:
line = errorStyle.Render(nameCore + tail)
@@ -240,15 +245,6 @@ func (m appModel) listPane(height, width int) string {
}
}
}
- renderedRows = append(renderedRows, line)
- }
-
- start := 0
- if selectedRow >= visible {
- start = selectedRow - visible + 1
- }
- end := min(len(renderedRows), start+visible)
- for _, line := range renderedRows[start:end] {
lines = append(lines, line)
}
return strings.Join(lines, "\n")
@@ -308,7 +304,10 @@ func (m appModel) detailText(width int) string {
}
func (m appModel) metadataLines(width int) []string {
- rows := m.visibleRows()
+ return m.metadataLinesForRows(m.visibleRows(), width)
+}
+
+func (m appModel) metadataLinesForRows(rows []skillsRow, width int) []string {
if len(rows) == 0 {
var lines []string
@@ -557,7 +556,10 @@ func (m appModel) metadataLines(width int) []string {
}
func (m appModel) previewLines(width int) []string {
- rows := m.visibleRows()
+ return m.previewLinesForRows(m.visibleRows(), width)
+}
+
+func (m appModel) previewLinesForRows(rows []skillsRow, width int) []string {
if len(rows) == 0 {
if m.result.Preflight != nil && !m.result.Preflight.CanRunSkills {
return []string{
@@ -607,8 +609,9 @@ func (m appModel) previewLines(width int) []string {
lines = append(lines, sectionHeaderStyle.Render("Available"), errorStyle.Render(" couldn't scan: "+disc.Error))
default: // DiscoveryReady
var avail []string
+ installed := m.installedSkillNames(row.groupName)
for _, ds := range disc.Skills {
- if !m.isSkillInstalled(ds.Name, row.groupName) {
+ if !isSkillNameInstalled(ds.Name, installed) {
avail = append(avail, ds.Name)
}
}