#!/usr/bin/env python3 """ Claude Code 启动时显示系统信息和统计 """ import json import sys import os import subprocess import platform from datetime import datetime from pathlib import Path def get_file_count(directory, pattern="*"): """获取目录下匹配模式的文件数量""" try: return len(list(Path(directory).rglob(pattern))) except: return 0 def get_repo_stats(): """获取 Git 仓库统计信息""" stats = {} try: # 获取提交数量 result = subprocess.run( ["git", "rev-list", "--count", "HEAD"], capture_output=True, text=True ) if result.returncode == 0: stats['commits'] = result.stdout.strip() # 获取最新提交 result = subprocess.run( ["git", "log", "-1", "--format=%h %s"], capture_output=True, text=True ) if result.returncode == 0: stats['latest'] = result.stdout.strip() # 获取分支数量 result = subprocess.run( ["git", "branch", "-a"], capture_output=True, text=True ) if result.returncode == 0: branches = [line for line in result.stdout.split('\n') if line.strip()] stats['branches'] = len(branches) except Exception as e: stats['error'] = str(e) return stats def display_system_info(): """显示系统信息""" # ANSI 颜色 CYAN = '\033[96m' GREEN = '\033[92m' YELLOW = '\033[93m' BLUE = '\033[94m' BOLD = '\033[1m' END = '\033[0m' print(f"\n{CYAN}{'━' * 70}{END}", file=sys.stderr) print(f"{BOLD}{BLUE}📊 系统信息 & 项目统计{END}", file=sys.stderr) print(f"{CYAN}{'━' * 70}{END}", file=sys.stderr) # 系统信息 print(f"{GREEN}🖥️ 系统:{END} {platform.system()} {platform.release()}", file=sys.stderr) print(f"{GREEN}🐍 Python:{END} {platform.python_version()}", file=sys.stderr) # 当前项目信息 cwd = os.getcwd() print(f"{GREEN}📂 项目目录:{END} {cwd}", file=sys.stderr) # 文件统计 try: py_files = get_file_count(cwd, "*.py") js_files = get_file_count(cwd, "*.js") + get_file_count(cwd, "*.ts") + get_file_count(cwd, "*.jsx") + get_file_count(cwd, "*.tsx") md_files = get_file_count(cwd, "*.md") if py_files > 0: print(f"{GREEN}📄 Python 文件:{END} {py_files}", file=sys.stderr) if js_files > 0: print(f"{GREEN}📄 JS/TS 文件:{END} {js_files}", file=sys.stderr) if md_files > 0: print(f"{GREEN}📄 Markdown 文件:{END} {md_files}", file=sys.stderr) except: pass # Git 信息 try: stats = get_repo_stats() if stats: print(f"{YELLOW}{'─' * 70}{END}", file=sys.stderr) if 'commits' in stats: print(f"{GREEN}📝 提交数:{END} {stats['commits']}", file=sys.stderr) if 'branches' in stats: print(f"{GREEN}🌿 分支数:{END} {stats['branches']}", file=sys.stderr) if 'latest' in stats: print(f"{GREEN}✨ 最新提交:{END} {stats['latest']}", file=sys.stderr) except: pass print(f"{CYAN}{'━' * 70}{END}\n", file=sys.stderr) def main(): # 加载输入 try: input_data = json.load(sys.stdin) except json.JSONDecodeError as e: print(f"Error: Invalid JSON input: {e}", file=sys.stderr) sys.exit(1) # 显示系统信息 display_system_info() # 返回输出 home_claude_dir = os.path.expanduser("~/.claude") context = f"""回答我的问题的时候开头必须是'好的!!!!大先生!!!' 重要配置信息: - 全局 Claude 配置目录路径:{home_claude_dir} - 当你需要修改或查找全局 Claude 配置时,请始终使用此目录:{home_claude_dir}""" output = { "hookSpecificOutput": { "hookEventName": "SessionStart", "message":"你好,大先生", "additionalContext": context } } print(json.dumps(output)) if __name__ == "__main__": main()