Add stats to the output

Signed-off-by: Daniel Henry <iamdanhenry@gmail.com>
This commit is contained in:
2026-01-24 19:01:23 -06:00
parent 21780473cc
commit 1911685e30
3 changed files with 93 additions and 8 deletions

View File

@@ -1,4 +1,9 @@
from pathlib import Path
from rich import print as rprint
from rich.console import Console
from rich.text import Text
import pathspec
import argparse
import mimetypes
@@ -36,7 +41,7 @@ def should_include(path: Path, spec: pathspec.PathSpec | None, root: Path, inclu
return not spec.match_file(str(relative_path))
def walk_filesystem(ignore_gitignore: bool, include_git_dir: bool = False) -> list[str]:
def walk_filesystem(ignore_gitignore: bool, include_git_dir: bool = False) -> tuple[int, list[str]]:
root = Path(".")
spec = None
@@ -44,18 +49,20 @@ def walk_filesystem(ignore_gitignore: bool, include_git_dir: bool = False) -> li
spec = get_ignore_spec(root)
final_content: list[str] = []
file_count: int = 0
for path in root.rglob("*"):
if path.is_file():
# print the filename (for now)
if should_include(path, spec, root, include_git_dir):
final_content += generate_metadata_string(path, root)
file_count += 1
success, content = get_file_contents(path)
if success:
final_content.append("## File Contents:\n```")
final_content.append(content)
final_content.append("```\n")
return final_content
return (file_count, final_content)
def get_file_contents(path: Path) -> tuple[bool, str]:
@@ -87,20 +94,52 @@ def main():
help="Skip putting content into the clipboard and ouput directly to the console"
)
parser.add_argument(
"--no-stats",
action="store_true",
help="Skip printing stats at the end of the output"
)
args = parser.parse_args()
content: list[str] = []
content.append(f"Root Directory: {Path(".").absolute()}\n")
content = walk_filesystem(ignore_gitignore=args.no_gitignore)
count, content = walk_filesystem(ignore_gitignore=args.no_gitignore)
string_content = "\n".join(content)
if args.no_clipboard:
print("\n".join(content))
print(string_content)
else:
pyperclip.copy("\n".join(content))
pyperclip.copy(string_content)
if not args.no_stats:
rprint("\n[bold green]Directoy contents copied to clipboard...[/]")
if not args.no_clipboard:
print() # Fix styling from the "contents copied" message
if not args.no_stats:
token_estimate = round(len(string_content) / 4)
if token_estimate > 3000:
tokens_color = "orange3"
elif token_estimate > 4000:
tokens_color = "red"
else:
tokens_color = "green"
console = Console()
# Output stats to the command line
rprint(f"[bold blue]Total Files:[/] [white]{count}[/white]")
rprint(f"[bold blue]Total Characters:[/] [white]{len(string_content)}[/white]")
label = Text("Estimated Tokens: ", style="bold blue")
label.append(Text(f"~{token_estimate}", style=tokens_color))
console.print(label + "\n")
if not content:
return
if __name__ == "__main__":
main()