#!/usr/bin/env python3 import gc import os import re import sys import json import time import argparse import statistics import subprocess import platform from pathlib import Path from collections import defaultdict from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, wait, FIRST_COMPLETED import fnmatch VERSION = "2.1.2" # Windows Terminal Fix for ANSI codes if sys.platform == "win32": os.system("") # Ensure UTF-8 output on Windows if sys.stdout.encoding != 'utf-8': sys.stdout.reconfigure(encoding='utf-8', errors='replace') if sys.stderr.encoding != 'utf-8': sys.stderr.reconfigure(encoding='utf-8', errors='replace') HELP_TEXT = """pjstats - Enhanced project statistics with multi-threaded analysis. Usage examples: python3 pjstats.py python3 pjstats.py /path/to/project --top 10 --json > pjstats.json python3 pjstats.py --ext .py .js --min-code-lines 10 python3 pjstats.py --exclude language_model .git python3 pjstats.py --no-color --no-git python3 pjstats.py --workers 4 --no-animation Options: path Project root (default: .) --json Output JSON summary --top N Top N files to display (default: 5) --ext Limit to specific extensions (e.g. .py .js) --min-code-lines Ignore files with fewer code lines than this --exclude Directories to ignore recursively (relative to project root) --include-hidden Include hidden directories/files --no-color Disable color output --no-git Disable Git info detection --no-animation Disable startup animation and progress bars --bypass-excluded Bypass all ignore rules (.gitignore, built-in excludes, etc.) --workers N Number of threads for parallel analysis (0=auto) Notes: - Files named Dockerfile.* are treated as Dockerfiles. - Languages without a comment token (e.g., Markdown, JSON) are shown as N/A in the Code Quality Snapshot. - The installer chooses a sensible default install directory for your OS (Unix: ~/.local/bin, Windows: %LOCALAPPDATA%/Programs/pjstats or %USERPROFILE%/Scripts) if `--install-dir` is not specified. """ # -------------------------------------------------- # Configuration # -------------------------------------------------- # files to check for ignore patterns IGNORE_FILES_TO_CHECK = ['.gitignore', '.ignore', '.dockerignore', '.npmignore'] CODE_LANGUAGES = { # JavaScript / TypeScript ".js": ("JavaScript", "//", "code"), ".jsx": ("JavaScript (JSX)", "//", "code"), ".ts": ("TypeScript", "//", "code"), ".tsx": ("TypeScript (TSX)", "//", "code"), ".mjs": ("JavaScript (ESM)", "//", "code"), ".cjs": ("JavaScript (CJS)", "//", "code"), # Python ".py": ("Python", "#", "code"), ".pyw": ("Python", "#", "code"), ".pyi": ("Python Stub", "#", "code"), # Shell ".sh": ("Shell", "#", "code"), ".bash": ("Bash", "#", "code"), ".zsh": ("Zsh", "#", "code"), ".fish": ("Fish", "#", "code"), ".ksh": ("KornShell", "#", "code"), # PowerShell ".ps1": ("PowerShell", "#", "code"), ".psm1": ("PowerShell Module", "#", "code"), ".psd1": ("PowerShell Data", "#", "code"), # Batch / CMD ".bat": ("Batch", "REM", "code"), ".cmd": ("Batch", "REM", "code"), # Web ".html": ("HTML", "