File size: 1,777 Bytes
9c105b7 | 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 | """
statistics.py
Project statistics
"""
from __future__ import annotations
from pathlib import Path
from utils import (
estimate_tokens,
count_lines,
human_size,
language_from_extension,
)
def build_statistics(files: list[Path], contents: list[tuple[Path, str]]) -> dict:
total_size = 0
total_lines = 0
total_tokens = 0
languages = {}
largest = None
smallest = None
for path, text in contents:
try:
size = path.stat().st_size
except Exception:
size = len(text.encode())
total_size += size
total_lines += count_lines(text)
total_tokens += estimate_tokens(text)
lang = language_from_extension(path.suffix)
languages[lang] = languages.get(lang, 0) + 1
if largest is None or size > largest[1]:
largest = (path.name, size)
if smallest is None or size < smallest[1]:
smallest = (path.name, size)
return {
"files": len(files),
"lines": total_lines,
"tokens": total_tokens,
"size": total_size,
"size_human": human_size(total_size),
"languages": languages,
"largest": largest,
"smallest": smallest,
"average_size": total_size // max(len(files), 1),
}
def statistics_markdown(stats: dict) -> str:
langs = "\n".join(
f"- {k}: {v}"
for k, v in sorted(stats["languages"].items())
)
return f"""
## Project Statistics
Files: {stats["files"]}
Lines: {stats["lines"]}
Approx Tokens: {stats["tokens"]}
Project Size: {stats["size_human"]}
Average File Size: {human_size(stats["average_size"])}
Largest File:
{stats["largest"]}
Smallest File:
{stats["smallest"]}
### Languages
{langs}
""".strip() |