zhou zhou
9 天以前 fec285d150b377d004e47f0973d298b92fe4c711
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
"""
Art Design Pro 组件搜索工具
 
用法:
  python search.py "表格"           # 搜索中文关键词
  python search.py "table"          # 搜索英文关键词
  python search.py "form" --category forms  # 按分类搜索
"""
 
import csv
import sys
import argparse
from pathlib import Path
 
if hasattr(sys.stdout, "reconfigure"):
    sys.stdout.reconfigure(encoding="utf-8")
if hasattr(sys.stderr, "reconfigure"):
    sys.stderr.reconfigure(encoding="utf-8")
 
# 组件数据库路径
COMPONENTS_DB = Path(__file__).parent.parent / "data" / "components.csv"
 
 
def load_components():
    """加载所有组件"""
    components = []
    with open(COMPONENTS_DB, "r", encoding="utf-8") as f:
        reader = csv.DictReader(f)
        for row in reader:
            components.append(row)
    return components
 
 
def search_components(keyword: str, category: str = None):
    """搜索组件"""
    components = load_components()
    keyword = keyword.lower()
 
    results = []
 
    for comp in components:
        # 如果指定了分类,先过滤
        if category and comp["category"] != category:
            continue
 
        # 搜索范围:中文名称、英文名称、描述、常见用法
        search_fields = [
            comp.get("name_cn") or "",
            comp.get("name_en") or "",
            comp.get("description") or "",
            comp.get("component") or "",
            comp.get("common_usage") or "",
        ]
 
        # 任何字段匹配即命中
        if any(keyword in field.lower() for field in search_fields):
            results.append(comp)
 
    return results
 
 
def format_component(comp: dict):
    """格式化单个组件信息"""
    return f"""
┌─ {comp['component']} ({comp['name_cn']})
│  📝 {comp['description']}
│  📦 导入: {comp['import_path']}
│  🔧 Props: {comp['props']}
│  🎯 Slots: {comp['slots']}
│  💡 常见场景: {comp['common_usage']}
└──────────────────────────────────────────────"""
 
 
def print_results(results: list):
    """打印搜索结果"""
    if not results:
        print("❌ 未找到匹配的组件")
        print("\n💡 提示:尝试使用更通用的关键词")
        print("   例如:'表格'、'表单'、'图表'、'卡片'、'布局'")
        return
 
    print(f"\n✅ 找到 {len(results)} 个组件:\n")
 
    for i, comp in enumerate(results, 1):
        print(f"{i}. {comp['component']} - {comp['name_cn']}")
        print(f"   {comp['description']}")
        print(f"   用途: {comp['common_usage']}")
        print()
 
    # 如果结果超过5个,询问是否查看详情
    if len(results) > 5:
        print("💡 使用 --detail 参数查看组件详细信息\n")
    else:
        print("\n详细信息:")
        print("=" * 60)
        for comp in results:
            print(format_component(comp))
            print()
 
 
def print_detailed_results(results: list):
    """打印详细搜索结果"""
    if not results:
        print("❌ 未找到匹配的组件")
        return
 
    print(f"\n✅ 找到 {len(results)} 个组件:\n")
    print("=" * 60)
 
    for i, comp in enumerate(results, 1):
        print(f"\n【{i}】{comp['component']} - {comp['name_cn']}")
        print(format_component(comp))
 
 
def list_all_categories():
    """列出所有分类"""
    components = load_components()
    categories = sorted(set(c["category"] for c in components))
 
    print("\n📁 组件分类:\n")
    for cat in categories:
        count = sum(1 for c in components if c["category"] == cat)
        print(f"  • {cat}: {count} 个组件")
 
    print("\n💡 使用 --category <分类名> 搜索特定分类的组件")
 
 
def main():
    parser = argparse.ArgumentParser(
        description="Art Design Pro 组件搜索工具",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
示例:
  python search.py "表格"           # 搜索中文关键词
  python search.py "table"          # 搜索英文关键词
  python search.py "form" --category forms  # 按分类搜索
  python search.py --list           # 列出所有分类
  python search.py "chart" --detail # 查看详细信息
        """
    )
 
    parser.add_argument("keyword", nargs="?", help="搜索关键词")
    parser.add_argument("--category", "-c", help="按分类过滤 (tables, forms, cards, charts, layouts, etc.)")
    parser.add_argument("--detail", "-d", action="store_true", help="显示详细信息")
    parser.add_argument("--list", "-l", action="store_true", help="列出所有分类")
 
    args = parser.parse_args()
 
    # 列出所有分类
    if args.list:
        list_all_categories()
        return
 
    # 必须提供关键词
    if not args.keyword:
        parser.print_help()
        return
 
    # 搜索组件
    results = search_components(args.keyword, args.category)
 
    # 打印结果
    if args.detail:
        print_detailed_results(results)
    else:
        print_results(results)
 
 
if __name__ == "__main__":
    main()