Add direct to clipboard

Signed-off-by: Daniel Henry <iamdanhenry@gmail.com>
This commit is contained in:
2026-01-24 18:11:23 -06:00
parent fb0548b7d5
commit 21780473cc
3 changed files with 52 additions and 14 deletions

View File

@@ -2,18 +2,22 @@ from pathlib import Path
import pathspec import pathspec
import argparse import argparse
import mimetypes import mimetypes
import pyperclip
def get_file_description(path: Path): def get_file_description(path: Path):
mime_type, _ = mimetypes.guess_type(path) mime_type, _ = mimetypes.guess_type(path)
return mime_type or "Unknown mimetype" return mime_type or "Unknown mimetype"
def output_metadata(path: Path, root: Path): def generate_metadata_string(path: Path, root: Path) -> list[str]:
desc = get_file_description(path) desc = get_file_description(path)
relative_path = path.relative_to(root) relative_path = path.relative_to(root)
print("#################") return_data = []
print(f"## Filename: {relative_path}") return_data.append("#################")
print(f"## Mimetype: {desc}") return_data.append(f"## Filename: {relative_path}")
print("#################") return_data.append(f"## Mimetype: {desc}")
return_data.append("#################")
return return_data
def get_ignore_spec(root: Path): def get_ignore_spec(root: Path):
gitignore_path = root / ".gitignore" gitignore_path = root / ".gitignore"
@@ -32,24 +36,27 @@ def should_include(path: Path, spec: pathspec.PathSpec | None, root: Path, inclu
return not spec.match_file(str(relative_path)) return not spec.match_file(str(relative_path))
def walk_filesystem(ignore_gitignore: bool, include_git_dir: bool = False): def walk_filesystem(ignore_gitignore: bool, include_git_dir: bool = False) -> list[str]:
root = Path(".") root = Path(".")
spec = None spec = None
if not ignore_gitignore: if not ignore_gitignore:
spec = get_ignore_spec(root) spec = get_ignore_spec(root)
final_content: list[str] = []
for path in root.rglob("*"): for path in root.rglob("*"):
if path.is_file(): if path.is_file():
# print the filename (for now) # print the filename (for now)
if should_include(path, spec, root, include_git_dir): if should_include(path, spec, root, include_git_dir):
output_metadata(path, root) final_content += generate_metadata_string(path, root)
success, content = get_file_contents(path) success, content = get_file_contents(path)
if success: if success:
print("## File Contents:") final_content.append("## File Contents:\n```")
print(content) final_content.append(content)
print("\n") final_content.append("```\n")
return final_content
def get_file_contents(path: Path) -> tuple[bool, str]: def get_file_contents(path: Path) -> tuple[bool, str]:
try: try:
@@ -74,9 +81,26 @@ def main():
help="Include the .git directory in the output" help="Include the .git directory in the output"
) )
args = parser.parse_args() parser.add_argument(
"--no-clipboard",
action="store_true",
help="Skip putting content into the clipboard and ouput directly to the console"
)
walk_filesystem(ignore_gitignore=args.no_gitignore) args = parser.parse_args()
content: list[str] = []
content.append(f"Root Directory: {Path(".").absolute()}\n")
content = walk_filesystem(ignore_gitignore=args.no_gitignore)
if args.no_clipboard:
print("\n".join(content))
else:
pyperclip.copy("\n".join(content))
if not content:
return
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@@ -6,4 +6,5 @@ readme = "README.md"
requires-python = ">=3.14" requires-python = ">=3.14"
dependencies = [ dependencies = [
"pathspec>=1.0.3", "pathspec>=1.0.3",
"pyperclip>=1.11.0",
] ]

15
uv.lock generated
View File

@@ -8,10 +8,14 @@ version = "0.1.0"
source = { virtual = "." } source = { virtual = "." }
dependencies = [ dependencies = [
{ name = "pathspec" }, { name = "pathspec" },
{ name = "pyperclip" },
] ]
[package.metadata] [package.metadata]
requires-dist = [{ name = "pathspec", specifier = ">=1.0.3" }] requires-dist = [
{ name = "pathspec", specifier = ">=1.0.3" },
{ name = "pyperclip", specifier = ">=1.11.0" },
]
[[package]] [[package]]
name = "pathspec" name = "pathspec"
@@ -21,3 +25,12 @@ sdist = { url = "https://files.pythonhosted.org/packages/4c/b2/bb8e495d5262bfec4
wheels = [ wheels = [
{ url = "https://files.pythonhosted.org/packages/32/2b/121e912bd60eebd623f873fd090de0e84f322972ab25a7f9044c056804ed/pathspec-1.0.3-py3-none-any.whl", hash = "sha256:e80767021c1cc524aa3fb14bedda9c34406591343cc42797b386ce7b9354fb6c", size = 55021, upload-time = "2026-01-09T15:46:44.652Z" }, { url = "https://files.pythonhosted.org/packages/32/2b/121e912bd60eebd623f873fd090de0e84f322972ab25a7f9044c056804ed/pathspec-1.0.3-py3-none-any.whl", hash = "sha256:e80767021c1cc524aa3fb14bedda9c34406591343cc42797b386ce7b9354fb6c", size = 55021, upload-time = "2026-01-09T15:46:44.652Z" },
] ]
[[package]]
name = "pyperclip"
version = "1.11.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/e8/52/d87eba7cb129b81563019d1679026e7a112ef76855d6159d24754dbd2a51/pyperclip-1.11.0.tar.gz", hash = "sha256:244035963e4428530d9e3a6101a1ef97209c6825edab1567beac148ccc1db1b6", size = 12185, upload-time = "2025-09-26T14:40:37.245Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/df/80/fc9d01d5ed37ba4c42ca2b55b4339ae6e200b456be3a1aaddf4a9fa99b8c/pyperclip-1.11.0-py3-none-any.whl", hash = "sha256:299403e9ff44581cb9ba2ffeed69c7aa96a008622ad0c46cb575ca75b5b84273", size = 11063, upload-time = "2025-09-26T14:40:36.069Z" },
]