feat(tui): add structured filter tags to list TUI

Support key:value tag syntax in the `/` filter input for precise
skill filtering by type, group directory, and repo name:

  t:tracked g:security audit
  → type=tracked AND group contains "security" AND free text "audit"

Tags: t:/type: (tracked/remote/local/github), g:/group: (substring),
r:/repo: (substring). Multiple tags use AND logic. Unknown tags
gracefully degrade to free text. Optional `_` prefix on type values
(e.g. t:_tracked) and short alias t:track are accepted.

Filter input placeholder and help line update to show available tags
when filtering mode is active.
This commit is contained in:
Willie
2026-03-06 23:10:48 +08:00
parent 7015aaf611
commit 0e7bfea8ee
4 changed files with 443 additions and 8 deletions
+19 -8
View File
@@ -135,6 +135,7 @@ func newListTUIModel(loadFn listLoadFn, skills []skillItem, totalCount int, mode
fi.Prompt = "/ "
fi.PromptStyle = tc.Filter
fi.Cursor.Style = tc.Filter
fi.Placeholder = "filter or t:tracked g:group r:repo"
m := listTUIModel{
list: l,
@@ -164,26 +165,28 @@ func (m listTUIModel) Init() tea.Cmd {
return nil
}
// applyFilter does a case-insensitive substring match over allItems.
// When filtering, results are capped at maxListItems to keep bubbles/list fast.
// applyFilter parses tag syntax (t:type g:group r:repo) and free text,
// then matches all non-empty conditions with AND logic.
// Results are capped at maxListItems to keep bubbles/list fast.
// When filter is empty, all items are restored (full pagination).
func (m *listTUIModel) applyFilter() {
term := strings.ToLower(m.filterText)
m.detailScroll = 0
// No filter — restore full item set with group separators
if term == "" {
if m.filterText == "" {
m.matchCount = len(m.allItems)
m.list.SetItems(buildGroupedItems(m.allItems))
m.list.ResetSelected()
return
}
// Substring match, capped at maxListItems
q := parseFilterQuery(m.filterText)
// Structured match, capped at maxListItems
var matched []list.Item
count := 0
for _, item := range m.allItems {
if strings.Contains(strings.ToLower(item.FilterValue()), term) {
if matchSkillItem(item, q) {
count++
if len(matched) < maxListItems {
matched = append(matched, item)
@@ -509,7 +512,11 @@ func (m listTUIModel) viewSplit() string {
b.WriteString(m.renderFilterBar())
b.WriteString(m.renderSummaryFooter())
b.WriteString("\n")
help := appendScrollInfo("↑↓ navigate ←→ page / filter Ctrl+d/u detail Enter view A audit U update X uninstall q quit", scrollInfo)
helpText := "↑↓ navigate ←→ page / filter Ctrl+d/u detail Enter view A audit U update X uninstall q quit"
if m.filtering {
helpText = "t:type g:group r:repo Enter lock Esc clear q quit"
}
help := appendScrollInfo(helpText, scrollInfo)
b.WriteString(tc.Help.Render(help))
b.WriteString("\n")
@@ -544,7 +551,11 @@ func (m listTUIModel) viewVertical() string {
b.WriteString(m.renderSummaryFooter())
b.WriteString("\n")
help := appendScrollInfo("↑↓ navigate ←→ page / filter Ctrl+d/u detail Enter view A audit U update X uninstall q quit", scrollInfo)
helpText := "↑↓ navigate ←→ page / filter Ctrl+d/u detail Enter view A audit U update X uninstall q quit"
if m.filtering {
helpText = "t:type g:group r:repo Enter lock Esc clear q quit"
}
help := appendScrollInfo(helpText, scrollInfo)
b.WriteString(tc.Help.Render(help))
b.WriteString("\n")
+143
View File
@@ -0,0 +1,143 @@
package main
import (
"strings"
)
// filterQuery holds the parsed result of a structured filter string.
// All fields are lowercased for case-insensitive matching.
type filterQuery struct {
TypeTag string // "tracked", "remote", "local" (empty = any)
GroupTag string // substring match against group segment
RepoTag string // substring match against RepoName
FreeText string // remaining text after removing known tags
}
// parseFilterQuery tokenizes the raw filter string and extracts known tags.
// Unknown key:value tokens are treated as free text.
func parseFilterQuery(raw string) filterQuery {
var q filterQuery
var freeTokens []string
for _, token := range strings.Fields(raw) {
lower := strings.ToLower(token)
if val, ok := cutTag(lower, "t:", "type:"); ok {
q.TypeTag = normalizeTypeValue(val)
continue
}
if val, ok := cutTag(lower, "g:", "group:"); ok {
q.GroupTag = val
continue
}
if val, ok := cutTag(lower, "r:", "repo:"); ok {
q.RepoTag = val
continue
}
freeTokens = append(freeTokens, strings.ToLower(token))
}
q.FreeText = strings.Join(freeTokens, " ")
return q
}
// cutTag checks if token starts with any of the prefixes and returns the value after.
func cutTag(token string, prefixes ...string) (string, bool) {
for _, p := range prefixes {
if strings.HasPrefix(token, p) {
val := token[len(p):]
if val != "" {
return val, true
}
}
}
return "", false
}
// normalizeTypeValue maps aliases to canonical type names.
// Strips optional leading "_" (tracked repos use "_" prefix on disk).
func normalizeTypeValue(val string) string {
val = strings.TrimPrefix(val, "_")
switch val {
case "github":
return "remote"
case "track":
return "tracked"
default:
return val
}
}
// matchSkillItem returns true if the skill item matches all non-empty conditions in the query (AND logic).
func matchSkillItem(item skillItem, q filterQuery) bool {
e := item.entry
// Type tag — exact match on skill type category
if q.TypeTag != "" {
skillType := skillTypeCategory(e)
if skillType != q.TypeTag {
return false
}
}
// Group tag — substring match on group segment
if q.GroupTag != "" {
group := strings.ToLower(skillGroup(e))
if !strings.Contains(group, q.GroupTag) {
return false
}
}
// Repo tag — substring match on RepoName
if q.RepoTag != "" {
repo := strings.ToLower(e.RepoName)
if !strings.Contains(repo, q.RepoTag) {
return false
}
}
// Free text — substring match on FilterValue
if q.FreeText != "" {
fv := strings.ToLower(item.FilterValue())
if !strings.Contains(fv, q.FreeText) {
return false
}
}
return true
}
// skillTypeCategory returns "tracked", "remote", or "local" for a skill entry.
func skillTypeCategory(e skillEntry) string {
switch {
case e.RepoName != "":
return "tracked"
case e.Source != "":
return "remote"
default:
return "local"
}
}
// skillGroup extracts the group directory segment from a skill entry's RelPath.
// For tracked repos: the second path segment (after the repo dir prefix).
// For non-tracked: the first path segment.
// Root-level skills (no subdirectory) return "".
func skillGroup(e skillEntry) string {
parts := strings.Split(e.RelPath, "/")
if len(parts) <= 1 {
return ""
}
if e.RepoName != "" {
// Tracked: first segment is repo dir, second is group
if len(parts) >= 3 {
return parts[1]
}
return ""
}
// Non-tracked: first segment is group
return parts[0]
}
+220
View File
@@ -0,0 +1,220 @@
package main
import (
"testing"
)
func TestParseFilterQuery_Empty(t *testing.T) {
q := parseFilterQuery("")
if q.TypeTag != "" || q.GroupTag != "" || q.RepoTag != "" || q.FreeText != "" {
t.Fatalf("expected empty query, got %+v", q)
}
}
func TestParseFilterQuery_PureText(t *testing.T) {
q := parseFilterQuery("hello world")
if q.FreeText != "hello world" {
t.Fatalf("FreeText = %q, want %q", q.FreeText, "hello world")
}
if q.TypeTag != "" || q.GroupTag != "" || q.RepoTag != "" {
t.Fatalf("expected no tags, got %+v", q)
}
}
func TestParseFilterQuery_TypeTag(t *testing.T) {
tests := []struct {
input string
want string
}{
{"t:tracked", "tracked"},
{"t:remote", "remote"},
{"t:local", "local"},
{"type:tracked", "tracked"},
{"t:github", "remote"}, // alias
{"t:track", "tracked"}, // short alias
{"t:_tracked", "tracked"}, // optional _ prefix
{"t:_track", "tracked"}, // _ prefix + short alias
{"T:Tracked", "tracked"},
}
for _, tt := range tests {
q := parseFilterQuery(tt.input)
if q.TypeTag != tt.want {
t.Errorf("parseFilterQuery(%q).TypeTag = %q, want %q", tt.input, q.TypeTag, tt.want)
}
}
}
func TestParseFilterQuery_GroupTag(t *testing.T) {
q := parseFilterQuery("g:security")
if q.GroupTag != "security" {
t.Fatalf("GroupTag = %q, want %q", q.GroupTag, "security")
}
q = parseFilterQuery("group:frontend")
if q.GroupTag != "frontend" {
t.Fatalf("GroupTag = %q, want %q", q.GroupTag, "frontend")
}
}
func TestParseFilterQuery_RepoTag(t *testing.T) {
q := parseFilterQuery("r:team")
if q.RepoTag != "team" {
t.Fatalf("RepoTag = %q, want %q", q.RepoTag, "team")
}
q = parseFilterQuery("repo:my-skills")
if q.RepoTag != "my-skills" {
t.Fatalf("RepoTag = %q, want %q", q.RepoTag, "my-skills")
}
}
func TestParseFilterQuery_MultipleTags(t *testing.T) {
q := parseFilterQuery("t:tracked g:security audit")
if q.TypeTag != "tracked" {
t.Fatalf("TypeTag = %q, want %q", q.TypeTag, "tracked")
}
if q.GroupTag != "security" {
t.Fatalf("GroupTag = %q, want %q", q.GroupTag, "security")
}
if q.FreeText != "audit" {
t.Fatalf("FreeText = %q, want %q", q.FreeText, "audit")
}
}
func TestParseFilterQuery_UnknownTagTreatedAsFreeText(t *testing.T) {
q := parseFilterQuery("x:foo bar")
if q.FreeText != "x:foo bar" {
t.Fatalf("FreeText = %q, want %q", q.FreeText, "x:foo bar")
}
}
func TestParseFilterQuery_EmptyValueIgnored(t *testing.T) {
q := parseFilterQuery("t: hello")
// "t:" has no value, treated as free text
if q.TypeTag != "" {
t.Fatalf("TypeTag = %q, want empty (empty value ignored)", q.TypeTag)
}
if q.FreeText != "t: hello" {
t.Fatalf("FreeText = %q, want %q", q.FreeText, "t: hello")
}
}
func TestSkillGroup_TrackedNested(t *testing.T) {
e := skillEntry{RelPath: "_team-repo/security/audit", RepoName: "team/repo"}
if got := skillGroup(e); got != "security" {
t.Fatalf("skillGroup() = %q, want %q", got, "security")
}
}
func TestSkillGroup_TrackedRoot(t *testing.T) {
e := skillEntry{RelPath: "_team-repo/my-skill", RepoName: "team/repo"}
if got := skillGroup(e); got != "" {
t.Fatalf("skillGroup() = %q, want empty for tracked root-level", got)
}
}
func TestSkillGroup_LocalNested(t *testing.T) {
e := skillEntry{RelPath: "frontend/react-tips"}
if got := skillGroup(e); got != "frontend" {
t.Fatalf("skillGroup() = %q, want %q", got, "frontend")
}
}
func TestSkillGroup_LocalRoot(t *testing.T) {
e := skillEntry{RelPath: "my-skill"}
if got := skillGroup(e); got != "" {
t.Fatalf("skillGroup() = %q, want empty for local root-level", got)
}
}
func TestMatchSkillItem_TypeTracked(t *testing.T) {
item := skillItem{entry: skillEntry{Name: "audit", RelPath: "_repo/audit", RepoName: "org/repo"}}
q := filterQuery{TypeTag: "tracked"}
if !matchSkillItem(item, q) {
t.Fatal("expected tracked item to match type:tracked")
}
q.TypeTag = "local"
if matchSkillItem(item, q) {
t.Fatal("expected tracked item NOT to match type:local")
}
}
func TestMatchSkillItem_TypeRemote(t *testing.T) {
item := skillItem{entry: skillEntry{Name: "remote-skill", RelPath: "remote-skill", Source: "github.com/foo/bar"}}
q := filterQuery{TypeTag: "remote"}
if !matchSkillItem(item, q) {
t.Fatal("expected remote item to match type:remote")
}
}
func TestMatchSkillItem_TypeLocal(t *testing.T) {
item := skillItem{entry: skillEntry{Name: "local-skill", RelPath: "local-skill"}}
q := filterQuery{TypeTag: "local"}
if !matchSkillItem(item, q) {
t.Fatal("expected local item to match type:local")
}
}
func TestMatchSkillItem_GroupMatch(t *testing.T) {
item := skillItem{entry: skillEntry{Name: "audit", RelPath: "security/audit"}}
q := filterQuery{GroupTag: "secur"}
if !matchSkillItem(item, q) {
t.Fatal("expected group substring match")
}
q.GroupTag = "frontend"
if matchSkillItem(item, q) {
t.Fatal("expected group mismatch")
}
}
func TestMatchSkillItem_RepoMatch(t *testing.T) {
item := skillItem{entry: skillEntry{Name: "x", RelPath: "_team-repo/x", RepoName: "team/my-skills"}}
q := filterQuery{RepoTag: "my-skills"}
if !matchSkillItem(item, q) {
t.Fatal("expected repo substring match")
}
q.RepoTag = "other"
if matchSkillItem(item, q) {
t.Fatal("expected repo mismatch")
}
}
func TestMatchSkillItem_FreeTextMatch(t *testing.T) {
item := skillItem{entry: skillEntry{Name: "react-tips", RelPath: "frontend/react-tips"}}
q := filterQuery{FreeText: "react"}
if !matchSkillItem(item, q) {
t.Fatal("expected free text match")
}
q.FreeText = "golang"
if matchSkillItem(item, q) {
t.Fatal("expected free text mismatch")
}
}
func TestMatchSkillItem_ANDLogic(t *testing.T) {
item := skillItem{entry: skillEntry{
Name: "audit",
RelPath: "_team-repo/security/audit",
RepoName: "team/repo",
}}
// All conditions match
q := filterQuery{TypeTag: "tracked", GroupTag: "security", FreeText: "audit"}
if !matchSkillItem(item, q) {
t.Fatal("expected AND match when all conditions are true")
}
// One condition fails
q.TypeTag = "local"
if matchSkillItem(item, q) {
t.Fatal("expected AND to fail when type doesn't match")
}
}
func TestMatchSkillItem_EmptyQueryMatchesAll(t *testing.T) {
item := skillItem{entry: skillEntry{Name: "anything", RelPath: "anything"}}
q := filterQuery{}
if !matchSkillItem(item, q) {
t.Fatal("empty query should match all items")
}
}
+61
View File
@@ -123,3 +123,64 @@ func TestListViewSplit_HeaderKeepsSkillNameWhenDetailScrolled(t *testing.T) {
t.Fatalf("expected skill name before date; output: %q", got)
}
}
func TestApplyFilter_WithTags(t *testing.T) {
items := []skillItem{
{entry: skillEntry{Name: "local-skill", RelPath: "local-skill"}},
{entry: skillEntry{Name: "react-tips", RelPath: "frontend/react-tips"}},
{entry: skillEntry{Name: "audit", RelPath: "_team-repo/security/audit", RepoName: "team/repo"}},
{entry: skillEntry{Name: "lint", RelPath: "_team-repo/lint", RepoName: "team/repo"}},
{entry: skillEntry{Name: "remote-a", RelPath: "remote-a", Source: "github.com/foo/bar"}},
}
m := newListTUIModel(nil, items, len(items), "global", t.TempDir(), nil)
// Filter by type:tracked — should match 2 items
m.filterText = "t:tracked"
m.applyFilter()
if m.matchCount != 2 {
t.Fatalf("type:tracked matchCount = %d, want 2", m.matchCount)
}
// Filter by type:local — should match 2 items (local-skill + react-tips)
m.filterText = "t:local"
m.applyFilter()
if m.matchCount != 2 {
t.Fatalf("type:local matchCount = %d, want 2", m.matchCount)
}
// Filter by group:security — should match 1 item
m.filterText = "g:security"
m.applyFilter()
if m.matchCount != 1 {
t.Fatalf("group:security matchCount = %d, want 1", m.matchCount)
}
// Filter by repo:team — should match 2 tracked items
m.filterText = "r:team"
m.applyFilter()
if m.matchCount != 2 {
t.Fatalf("repo:team matchCount = %d, want 2", m.matchCount)
}
// Combined: type:tracked + group:security — should match 1
m.filterText = "t:tracked g:security"
m.applyFilter()
if m.matchCount != 1 {
t.Fatalf("combined tag matchCount = %d, want 1", m.matchCount)
}
// Free text only — should match react-tips
m.filterText = "react"
m.applyFilter()
if m.matchCount != 1 {
t.Fatalf("free text matchCount = %d, want 1", m.matchCount)
}
// Clear filter — should restore all
m.filterText = ""
m.applyFilter()
if m.matchCount != len(items) {
t.Fatalf("cleared matchCount = %d, want %d", m.matchCount, len(items))
}
}