Add direct to clipboard
Signed-off-by: Daniel Henry <iamdanhenry@gmail.com>
This commit is contained in:
50
dumpy.py
50
dumpy.py
@@ -2,18 +2,22 @@ from pathlib import Path
|
||||
import pathspec
|
||||
import argparse
|
||||
import mimetypes
|
||||
import pyperclip
|
||||
|
||||
def get_file_description(path: Path):
|
||||
mime_type, _ = mimetypes.guess_type(path)
|
||||
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)
|
||||
relative_path = path.relative_to(root)
|
||||
print("#################")
|
||||
print(f"## Filename: {relative_path}")
|
||||
print(f"## Mimetype: {desc}")
|
||||
print("#################")
|
||||
return_data = []
|
||||
return_data.append("#################")
|
||||
return_data.append(f"## Filename: {relative_path}")
|
||||
return_data.append(f"## Mimetype: {desc}")
|
||||
return_data.append("#################")
|
||||
|
||||
return return_data
|
||||
|
||||
def get_ignore_spec(root: Path):
|
||||
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))
|
||||
|
||||
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(".")
|
||||
|
||||
spec = None
|
||||
if not ignore_gitignore:
|
||||
spec = get_ignore_spec(root)
|
||||
|
||||
final_content: list[str] = []
|
||||
|
||||
for path in root.rglob("*"):
|
||||
if path.is_file():
|
||||
# print the filename (for now)
|
||||
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)
|
||||
if success:
|
||||
print("## File Contents:")
|
||||
print(content)
|
||||
print("\n")
|
||||
|
||||
final_content.append("## File Contents:\n```")
|
||||
final_content.append(content)
|
||||
final_content.append("```\n")
|
||||
return final_content
|
||||
|
||||
def get_file_contents(path: Path) -> tuple[bool, str]:
|
||||
|
||||
try:
|
||||
@@ -74,9 +81,26 @@ def main():
|
||||
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__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user