Skip to content

Commit

Permalink
Added more args and cross platform support
Browse files Browse the repository at this point in the history
  • Loading branch information
FilippoLeone committed Apr 16, 2024
1 parent 56a1d1a commit ac04937
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 19 deletions.
77 changes: 61 additions & 16 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import click
import win32clipboard
import logging
import platform
from pathlib import Path

from .file_reader import read_file
from .structure_generator import create_prompt, list_directory_structure

Expand All @@ -24,36 +23,44 @@ def print_ascii_art():

@click.command()
@click.argument('target_folder', type=click.Path(exists=True, file_okay=False, dir_okay=True))
def main(target_folder: str):
@click.argument('file_pattern', type=str, default="*.py")
@click.argument('exclude_pattern', type=str, default="")
@click.argument('platform', type=str, default="")
def main(target_folder: str, file_pattern: str, exclude_pattern: str, platform: str):
print_ascii_art()
folder_path = Path(target_folder)

# Start with optional files if they exist
full_prompt = ''
full_prompt = append_optional_contents(folder_path, full_prompt)

# Append README.md and project structure
readme_content = read_file(folder_path / 'README.md')
structure_snapshot = list_directory_structure(folder_path)
full_prompt += f"# README.md\n{readme_content}\n\n# Project structure\n{structure_snapshot}\n"

# Append content of each Python file in the directory
for file_path in folder_path.rglob('*.py'):
for file_path in folder_path.rglob(file_pattern):
if exclude_pattern and any(exclude_path in str(file_path) for exclude_path in exclude_pattern.split(",")):
continue
file_content = read_file(file_path)
full_prompt += f"# {file_path.name}\n{file_content}\n\n"

prompt, token_count = create_prompt(full_prompt, folder_path)
logging.info(f"Total number of tokens: {token_count}")

try:
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(prompt, win32clipboard.CF_TEXT)
win32clipboard.CloseClipboard()
print("\033[92mSuccessfully copied to clipboard.\033[0m")
except Exception as e:
logging.error(f"Failed to copy to clipboard: {e}")

if not platform:
platform = platform_name()

if platform.lower() == 'win32':
copy_to_clipboard_win32(prompt)
elif platform.lower() == 'linux':
copy_to_clipboard_linux(prompt)
elif platform.lower() == 'darwin':
copy_to_clipboard_macos(prompt)
else:
logging.error(f"Unsupported platform: {platform}")

print("\nFinal Prompt for GPT-4 has been copied to the clipboard. You can paste it anywhere using Ctrl+V.\n")
print(prompt)

Expand All @@ -67,5 +74,43 @@ def append_optional_contents(folder_path, full_prompt):
full_prompt = f"# {filename}\n{content}\n\n" + full_prompt
return full_prompt

def platform_name():
system = platform.system().lower()
if system == 'windows':
return 'win32'
elif system == 'linux':
return 'linux'
elif system == 'darwin':
return 'darwin'
else:
return ''

def copy_to_clipboard_win32(text):
import win32clipboard
try:
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(text, win32clipboard.CF_TEXT)
win32clipboard.CloseClipboard()
print("\033[92mSuccessfully copied to clipboard.\033[0m")
except Exception as e:
logging.error(f"Failed to copy to clipboard: {e}")

def copy_to_clipboard_linux(text):
try:
import subprocess
subprocess.run(['xclip', '-selection', 'clipboard'], input=text.encode(), check=True)
print("\033[92mSuccessfully copied to clipboard.\033[0m")
except (subprocess.CalledProcessError, ImportError) as e:
logging.error(f"Failed to copy to clipboard: {e}")

def copy_to_clipboard_macos(text):
try:
import subprocess
subprocess.run(['pbcopy'], input=text.encode(), check=True)
print("\033[92mSuccessfully copied to clipboard.\033[0m")
except subprocess.CalledProcessError as e:
logging.error(f"Failed to copy to clipboard: {e}")

if __name__ == "__main__":
main()
main()
29 changes: 26 additions & 3 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,38 @@ def setup_test_files(test_folder):
readme_file = test_folder / "README.md"
readme_file.write_text("Project Name: Example Project")

# Add more setup here if your main function expects more files or specific content
# Add some Python files
python_file1 = test_folder / "example1.py"
python_file1.write_text("print('Hello, World!')")

python_file2 = test_folder / "example2.py"
python_file2.write_text("x = 42\nprint(x)")

def test_main_cli(tmp_path):
runner = CliRunner()
test_folder = tmp_path / "test_folder"
test_folder.mkdir()
setup_test_files(test_folder) # Set up necessary test files
setup_test_files(test_folder)

# Test with default arguments
result = runner.invoke(main, [str(test_folder)])
assert result.exit_code == 0, f"Unexpected exit code: {result.exit_code}. Output: {result.output}"
assert "Successfully copied to clipboard" in result.output

# Test with file_pattern argument
result = runner.invoke(main, [str(test_folder), "*.py"])
assert result.exit_code == 0, f"Unexpected exit code: {result.exit_code}. Output: {result.output}"
assert "Successfully copied to clipboard" in result.output

# Test with exclude_pattern argument
result = runner.invoke(main, [str(test_folder), "*.py", "example2.py"])
assert result.exit_code == 0, f"Unexpected exit code: {result.exit_code}. Output: {result.output}"
assert "Successfully copied to clipboard" in result.output

# Test with platform argument
result = runner.invoke(main, [str(test_folder), "*.py", "", "win32"])
assert result.exit_code == 0, f"Unexpected exit code: {result.exit_code}. Output: {result.output}"
assert "Successfully copied to clipboard" in result.output

# Additional assertions can be made here based on expected output
if __name__ == "__main__":
pytest.main()

0 comments on commit ac04937

Please sign in to comment.